Git Notes
Branching is one of the most powerful features in Git, and understanding how to work with branches effectively is what separates a beginner from a...
Branching is one of the most powerful features in Git, and understanding how to work with branches effectively is what separates a beginner from a proficient developer. Think of branches as parallel universes for your code — each branch lets you work on something different without affecting the main timeline of your project.
Understanding Branches Conceptually
In traditional file management, if you wanted to try something new with your project, you might copy the entire folder and work on the copy. Git branches accomplish the same goal but in an incredibly efficient way. Instead of duplicating files, Git simply creates a new pointer to the current commit. This means creating a branch is nearly instantaneous, regardless of your project size.
A branch in Git is essentially a lightweight movable pointer to a commit. The default branch in most repositories is called main (or master in older repositories). Every time you make a commit, the branch pointer moves forward automatically to point to your latest commit.
Creating Branches
The most straightforward way to create a new branch is using the git branch command:
git branch feature-loginThis creates a new branch called feature-login that points to the same commit you are currently on. However, it does not switch you to that branch. You remain on whatever branch you were on before running this command.
To create a branch and immediately switch to it, you can combine the commands:
git checkout -b feature-loginOr using the newer syntax introduced in Git 2.23:
git switch -c feature-loginBoth of these commands create the branch and move your HEAD pointer to the new branch in one step.
Listing Branches
To see all local branches in your repository:
git branchThe output might look like this:
The asterisk indicates your current branch. To also see remote-tracking branches:
git branch -aThis shows both local and remote branches:
If you want to see branches along with their last commit message:
git branch -vRenaming Branches
Sometimes you realize a branch name does not clearly describe what you are working on. You can rename your current branch:
git branch -m new-branch-nameTo rename a branch you are not currently on:
git branch -m old-name new-nameDeleting Branches
After merging a feature branch, you typically want to clean it up:
git branch -d feature-loginThe lowercase -d flag only deletes the branch if it has been fully merged. If you want to force-delete an unmerged branch (and accept losing that work):
git branch -D feature-loginHow Branches Work Internally
Under the hood, a Git branch is simply a file stored in .git/refs/heads/ that contains the 40-character SHA-1 hash of the commit it points to. When you create a branch, Git creates this small file. When you make a new commit on that branch, Git updates this file with the new commit hash.
The special pointer called HEAD tells Git which branch you are currently on. It is stored in .git/HEAD and typically contains a reference like ref: refs/heads/main.
Practical Branch Naming Conventions
In professional environments, teams adopt naming conventions for branches. Common patterns include:
These prefixes make it immediately clear what type of work a branch contains and help with automation in CI/CD pipelines.
Common Branching Scenarios
Scenario 1: Starting new feature work
You are on main and need to build a new dashboard:
git switch -c feature/dashboard
# Work on your feature, make commits
git add .
git commit -m "Add dashboard layout component"Scenario 2: Quick bug fix while working on a feature
You are mid-way through a feature when a critical bug is reported:
git stash # Save current work
git switch main # Go back to main
git switch -c hotfix/critical-bug
# Fix the bug
git commit -am "Fix null pointer in payment processing"
git switch main
git merge hotfix/critical-bug
git switch feature/dashboard # Return to feature
git stash pop # Restore your workBranch Tracking and Upstream
When you push a local branch to a remote for the first time, you set up tracking:
git push -u origin feature-loginThe -u flag sets the upstream, so future git push and git pull commands know which remote branch to interact with. You can check tracking relationships with:
git branch -vvCommon Mistakes and How to Avoid Them
One frequent mistake is creating a branch from the wrong starting point. Always verify which branch you are on before creating a new one:
git status # Check current branch
git switch main # Ensure you are on main
git pull # Get latest changes
git switch -c feature/new-work # Now create your branchAnother common issue is accumulating stale branches. Periodically clean up merged branches:
git branch --merged main | grep -v main | xargs git branch -dThis command lists all branches merged into main, excludes main itself, and deletes them.
Key Takeaways
Branches in Git are cheap to create and essential for organized development. They let you isolate work, experiment safely, and collaborate without stepping on each other's code. Master branch management, and you will find your development workflow becomes significantly smoother and more productive.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Git Branch.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, branching, and, merging, branch
Related Git & GitHub Topics