Skip to content

4. Working with Branches

Branching is one of Git’s most powerful features. It allows you to create a separate workspace where you can experiment, develop new features, or fix bugs without affecting the main project.

From the official Git documentation:

“Branching means you diverge from the main line of development and continue to do work without messing with that main line.”

With branches, you can:

  • Work on new features without disrupting the stable version of your project.
  • Fix bugs without interfering with ongoing development.
  • Experiment safely, knowing you can switch back to a previous state anytime.

Step through a full branch-and-merge cycle below:

1 / 5
Step 1 of 5

main has two commits. This is our stable line of development.

To list existing branches you can execute:

Terminal window
git branch

You can also check the branches on the remote with:

Terminal window
git branch -r

Or view all branches from local and remote:

Terminal window
git branch -a

To create a new branch the command is:

Terminal window
git branch feature

This creates a new branch called feature, but you will remain on your current branch.

To create a branch and switch to it directly:

Terminal window
git checkout -b feature

And this is the modern alternative to checkout:

Terminal window
git switch -c feature

To move between branches:

Terminal window
git checkout feature

Or using the modern way:

Terminal window
git switch feature

Once your feature is complete, you’ll want to integrate it into the main line of development (typically the main or develop branch). Merging allows you to combine changes from different branches into a single branch.

1 - Switch to the target branch (usually main or develop):

Terminal window
git switch main

2 - Merge the feature branch into the current branch:

Terminal window
git merge feature

If the branches have diverged significantly, Git might encounter merge conflicts. In that case, you’ll need to manually resolve conflicts before completing the merge.

How a merge looks depends on whether main moved on while you worked:

  • If main has no new commits since you branched, Git just slides the main pointer forward to your branch tip. This is a fast-forward: no extra commit, perfectly linear history.
  • If both branches gained commits, Git performs a three-way merge and records a dedicated merge commit with two parents, tying the two lines of history together.

Sometimes you want a merge commit even when a fast-forward would be possible, because it keeps a visible record of when a feature landed. Force one with:

Terminal window
git merge --no-ff feature

A merge conflict happens when two branches changed the same lines of the same file in different ways, and Git cannot decide on its own which version to keep. This is not an error you did something wrong, it’s just Git asking for your help to make a decision.

Try resolving one yourself. Pick which side to keep and watch Git clean up the markers:

Git marked the conflict. Pick which side to keep, and watch it strip out the markers for you.

app.js
<<<<<<< HEADconst greeting = "Hello team";=======const greeting = "Hello world";>>>>>>> feature

When a conflict occurs, Git pauses the merge and tells you exactly which files are affected:

Terminal window
git merge feature
Auto-merging app.js
CONFLICT (content): Merge conflict in app.js
Automatic merge failed; fix conflicts and then commit the result.

You can always check which files are still in conflict with:

Terminal window
git status

Open the conflicted file and you’ll find Git has inserted special markers around the disputed section:

<<<<<<< HEAD
const greeting = "Hello team";
=======
const greeting = "Hello world";
>>>>>>> feature

Here is how to read them:

  • Everything between <<<<<<< HEAD and ======= is the version from your current branch (the one you merged into).
  • Everything between ======= and >>>>>>> feature is the version coming from the branch you are merging in (here, feature).

Your job is to edit the file so it contains the final version you want, then remove all three markers (<<<<<<<, =======, >>>>>>>). You can keep one side, keep the other, or combine them.

Once you’ve cleaned up every conflicted file, stage them to tell Git the conflict is resolved, then commit to complete the merge:

Terminal window
git add app.js
git commit

Git already prepares a merge commit message for you, so in most cases you can just save and close the editor.

If things get messy and you’d rather start over, you can cancel the merge entirely and return to the state before you started:

Terminal window
git merge --abort

This restores your branch to exactly how it was before the merge, with no changes applied. Nothing is lost, so don’t hesitate to use it when in doubt.

After merging your changes into the main branch, the feature branch is no longer needed and can be deleted to keep your repository clean.

To delete a local branch after merging:

Terminal window
git branch -d feature

However, if the branch contains unmerged changes, Git will prevent its deletion to avoid accidental data loss. If you are sure you want to forcefully delete the branch:

Terminal window
git branch -D feature

If you want to delete a branch on the remote you can execute:

Terminal window
git push origin --delete feature

Alternatively, you can use:

Terminal window
git fetch -p # Prune deleted remote branches from local references

This helps keep your local references clean after deleting remote branches.

CommandDescription
git branchList local branches
git branch -aList all branches (local & remote)
git branch new-branchCreate a new branch
git switch new-branchSwitch to an existing branch
git checkout -b new-branchCreate and switch to a new branch
git merge branch-nameMerge a branch into the current branch
git branch -d branch-nameDelete a branch (only if merged)
git branch -D branch-nameForce delete a branch
git push origin --delete branch-nameDelete a remote branch