4. Working with Branches
What are branches? Why use them?
Section titled “What are branches? Why use them?”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:
main has two commits. This is our stable line of development.
To list existing branches you can execute:
git branchYou can also check the branches on the remote with:
git branch -rOr view all branches from local and remote:
git branch -aCreating and switching branches
Section titled “Creating and switching branches”Creating a new branch
Section titled “Creating a new branch”To create a new branch the command is:
git branch featureThis creates a new branch called feature, but you will remain on your current branch.
To create a branch and switch to it directly:
git checkout -b featureAnd this is the modern alternative to checkout:
git switch -c featureSwitching to an existing branch
Section titled “Switching to an existing branch”To move between branches:
git checkout featureOr using the modern way:
git switch featureMerging branches (git merge)
Section titled “Merging branches (git merge)”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.
Steps to merge a branch
Section titled “Steps to merge a branch”1 - Switch to the target branch (usually main or develop):
git switch main2 - Merge the feature branch into the current branch:
git merge featureIf the branches have diverged significantly, Git might encounter merge conflicts. In that case, you’ll need to manually resolve conflicts before completing the merge.
Fast-forward vs. merge commit
Section titled “Fast-forward vs. merge commit”How a merge looks depends on whether main moved on while you worked:
- If
mainhas no new commits since you branched, Git just slides themainpointer 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:
git merge --no-ff featureResolving merge conflicts
Section titled “Resolving merge conflicts”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.
<<<<<<< HEADconst greeting = "Hello team";=======const greeting = "Hello world";>>>>>>> featureWhen a conflict occurs, Git pauses the merge and tells you exactly which files are affected:
git merge featureAuto-merging app.jsCONFLICT (content): Merge conflict in app.jsAutomatic merge failed; fix conflicts and then commit the result.You can always check which files are still in conflict with:
git statusReading the conflict markers
Section titled “Reading the conflict markers”Open the conflicted file and you’ll find Git has inserted special markers around the disputed section:
<<<<<<< HEADconst greeting = "Hello team";=======const greeting = "Hello world";>>>>>>> featureHere is how to read them:
- Everything between
<<<<<<< HEADand=======is the version from your current branch (the one you merged into). - Everything between
=======and>>>>>>> featureis 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.
Finishing the merge
Section titled “Finishing the merge”Once you’ve cleaned up every conflicted file, stage them to tell Git the conflict is resolved, then commit to complete the merge:
git add app.jsgit commitGit already prepares a merge commit message for you, so in most cases you can just save and close the editor.
Aborting a merge
Section titled “Aborting a merge”If things get messy and you’d rather start over, you can cancel the merge entirely and return to the state before you started:
git merge --abortThis 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.
Deleting branches
Section titled “Deleting branches”After merging your changes into the main branch, the feature branch is no longer needed and can be deleted to keep your repository clean.
Delete a local branch
Section titled “Delete a local branch”To delete a local branch after merging:
git branch -d featureHowever, 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:
git branch -D featureDeleting a remote branch
Section titled “Deleting a remote branch”If you want to delete a branch on the remote you can execute:
git push origin --delete featureAlternatively, you can use:
git fetch -p # Prune deleted remote branches from local referencesThis helps keep your local references clean after deleting remote branches.
Summary
Section titled “Summary”| Command | Description |
|---|---|
git branch | List local branches |
git branch -a | List all branches (local & remote) |
git branch new-branch | Create a new branch |
git switch new-branch | Switch to an existing branch |
git checkout -b new-branch | Create and switch to a new branch |
git merge branch-name | Merge a branch into the current branch |
git branch -d branch-name | Delete a branch (only if merged) |
git branch -D branch-name | Force delete a branch |
git push origin --delete branch-name | Delete a remote branch |