Creating, Managing, and Navigating Branches¶
Master the art of Git branching to organize parallel streams of development work.
Understanding Branches¶
What is a Branch?¶
A branch in Git is simply a lightweight movable pointer to a commit. The default branch name in Git is main (or master in older repositories).
When you create a branch, you're creating a new pointer to the same commit you're currently on:
gitGraph
commit id: "C1"
commit id: "C2"
commit id: "C3"
branch feature
checkout main
When you add a new commit to the feature branch, it moves forward while the main branch stays at the same commit:
gitGraph
commit id: "C1"
commit id: "C2"
commit id: "C3"
branch feature
checkout feature
commit id: "C4"
checkout main
Branch Visualization¶
To visualize your branch structure, use:
Show branch structure with decorations¶
Create a colorful visualization alias¶
git config --global alias.visual "log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(auto)%d%C(reset)' --all"
Then use:
Creating Branches¶
Creating a New Branch¶
Create a branch that points to your current commit:
Create a branch without switching¶
Example:
Create and Switch in One Command¶
Create a new branch and switch to it immediately:
Using checkout (traditional approach)¶
Using switch (modern approach, Git 2.23+)¶
Examples:
Create Branch from a Specific Commit¶
Create a branch starting from a specified commit:
Create branch from a commit hash¶
Create and switch in one command¶
Examples:
Create Branch from a Tag¶
Create a branch starting from a tagged version:
Create and switch to a branch from a tag¶
Example:
Viewing Branches¶
List Branches¶
View the branches in your repository:
List local branches¶
List all branches (local and remote)¶
List remote branches only¶
List branches with more details¶
Show last commit on each branch:
Also show upstream branches:
List merged branches¶
Branches merged into HEAD:
Branches not merged into HEAD
View Branch Details¶
Get more information about a specific branch:
Show the commit hash that the branch points to¶
See what remote branch a local branch is tracking¶
Show commit history for a specific branch¶
Show the most recent commit on a branch¶
Switching Between Branches¶
Switch to a Different Branch¶
Move to a different branch in your repository:
Traditional way (still widely used)¶
Modern way (Git 2.23+)¶
Examples:
💡 Tip: Git won't let you switch branches if you have uncommitted changes that conflict with the target branch. Either commit, stash, or discard your changes first.
The Modern switch Command¶
Git 2.23+ introduced switch as a clearer alternative to the overloaded checkout command:
Basic branch switching¶
Create and switch to a new branch¶
Return to the previous branch¶
Switch to Previous Branch¶
Quickly jump back to the branch you were on before:
Switch to the last branch you were on¶
Or:
Deleting Branches¶
Delete a Fully Merged Branch¶
Remove a branch that has been fully merged into the current branch:
Safe delete (will fail if branch has unmerged changes)¶
Example:
Force Delete an Unmerged Branch¶
Delete a branch even if it contains unmerged changes:
Force delete¶
Example:
⚠️ Warning: This permanently discards all commits unique to this branch if they haven't been merged elsewhere.
Delete Remote Branch¶
Remove a branch from a remote repository:
Delete a remote branch¶
Alternate syntax:
Examples:
Renaming Branches¶
Rename Current Branch¶
Change the name of the branch you're currently on:
Rename the current branch¶
Example:
Rename Any Branch¶
Change the name of a branch while on a different branch:
Rename any branch¶
Example:
Renaming the Main Branch¶
To rename your default branch (e.g., from master to main):
Create a new branch called main from master¶
Push the new main branch and set up tracking¶
Change default branch on GitHub/GitLab/etc. (via web interface)¶
Delete the old master branch from remote¶
Working with Remote Branches¶
Pushing a Branch to Remote¶
Share your local branch with others by pushing it to a remote repository:
Push a branch to the remote for the first time¶
Or:
Subsequent pushes after tracking is established¶
Force push (use with caution!)¶
Or:
⚠️ Warning: Force pushing rewrites history on the remote. Never force push to shared branches unless you're absolutely certain it's safe.
Setting Upstream Branch¶
Connect your local branch to a remote branch for tracking:
Set upstream branch when pushing¶
Set upstream for an existing branch¶
Or shorter:
Tracking Remote Branches¶
Create a local branch that tracks a remote branch:
Create a local branch from remote¶
Newer syntax (Git 2.23+)¶
Easiest way - creates a local branch with the same name¶
Or even shorter if branch names match:
List Remote Branches¶
View branches that exist on the remote server:
Show remote branches¶
Show remote branches with more details¶
Fetch Remote Branches¶
Update your local references to remote branches:
Fetch updates from all remotes¶
Fetch from a specific remote¶
Fetch a specific branch¶
Branch Management Strategies¶
Branch Naming Conventions¶
A good branch naming strategy helps team members understand branch purpose:
Common types include:
feature/- New functionalitybugfix/- Bug fixeshotfix/- Urgent fixes for productionrelease/- Release preparationrefactor/- Code refactoringdocs/- Documentation updatestest/- Test additions or changes
Examples:
Common Branching Models¶
GitFlow: A structured branching model with dedicated branches for development, features, releases, and hotfixes.
gitGraph
commit
branch develop
checkout develop
commit
branch feature1
checkout feature1
commit
commit
commit
checkout develop
merge feature1
branch feature2
checkout feature2
commit
commit
commit
checkout develop
merge feature2
commit
checkout main
merge develop
checkout develop
commit
checkout main
merge develop
GitHub Flow: A simpler workflow based on feature branches and pull requests.
gitGraph
commit
branch feature1
checkout feature1
commit
commit
checkout main
merge feature1
branch feature2
checkout feature2
commit
commit
checkout main
merge feature2
branch feature3
checkout feature3
commit
commit
checkout main
merge feature3
Trunk-Based Development: Features are developed in very short-lived branches or directly on the main branch.
gitGraph
commit
branch feature1
checkout feature1
commit
checkout main
merge feature1
commit
branch feature2
checkout feature2
commit
checkout main
merge feature2
commit
branch feature3
checkout feature3
commit
checkout main
merge feature3
Branch Operations¶
Find Branches Containing a Specific Commit¶
Locate which branches include a particular commit:
List all branches containing a commit¶
Include remote branches¶
Find Last Commit on a Branch¶
View the most recent commit on a branch:
Show the tip commit of a branch¶
Just the commit hash¶
Show commit message only¶
Compare Branches¶
See what commits differ between branches:
Commits in branch-A that aren't in branch-B¶
Just the commit summaries¶
Show the diff between two branches¶
Show only the file names that differ¶
Command Summary¶
| Operation | Command | Description |
|---|---|---|
| Create | git branch <name> | Create new branch |
| Create & Switch | git checkout -b <name> or git switch -c <name> | Create and switch to branch |
| List | git branch or git branch -a | List branches (local/all) |
| Switch | git checkout <name> or git switch <name> | Move to branch |
| Delete | git branch -d <name> or git branch -D <name> | Delete branch (safe/force) |
| Rename | git branch -m <name> or git branch -m <old> <new> | Rename branch |
| Push | git push -u origin <name> | Push and track branch |
| Compare | git diff branch1..branch2 | Compare branches |
| Merge | git merge <branch> | Merge branch into current |