Back to Git Course

Git Course

What is a Branch?

A branch is a pointer to one of the commits in your repository. It allows you to work on a feature or fix separately from the main codebase. You can create, switch between, and merge branches with Git.

Default Branch

When you create a new Git repository, the default branch is called main (previously master).

Creating a new branch

To create a new branch we need to use below command.

git branch <branch-name>

Let's create branch called dev/praveen in sample1 repo.

D:\praveenoruganti\codeworkspace\gitworkout\sample1>git branch dev/praveen

Switching between branches

To switch to an existing branch we need to use below command.

git checkout <branch-name>

or we can combine creating and switching in one command

git checkout -b <branch-name>

Let's switch to dev/praveen branch in sample1 repo.

D:\praveenoruganti\codeworkspace\gitworkout\sample1>git checkout dev/praveen Switched to branch 'dev/praveen'

Merging Branches

Once you have completed work on a branch and want to incorporate it into another, use the git merge command.

git checkout main # Ensure you're on the branch you want to merge into git merge <branch-name> # Merges the changes from <branch-name> into main

Merge Conflicts

Sometimes Git can't automatically merge changes, and you'll have to resolve conflicts manually. After fixing the conflicts, mark them as resolved.

git add <filename> git commit

Branch Management

Once you're done with a branch, you can delete it.

git branch -d <branch-name>

This deletes the local branch, not the remote one.