Git branching allows you to create independent lines of development within a repository. This reference provides commands and examples for managing branches, including creating, switching, merging, and rebasing branches, as well as resolving merge conflicts and working with remote branches.
Command | Description |
---|---|
git branch |
List all local branches. |
git branch -a |
List all branches, including remote branches. |
git branch <branch_name> |
Create a new branch. |
git checkout <branch_name> |
Switch to an existing branch. |
git checkout -b <branch_name> |
Create and switch to a new branch. |
git branch -d <branch_name> |
Delete a local branch (only if merged). |
git branch -D <branch_name> |
Force delete a local branch. |
git merge <branch_name> |
Merge a branch into the current branch. |
git log --oneline --graph --decorate --all |
Visualize the branch structure. |
# Create a new branch called 'feature'
git branch feature
# Create and switch to 'feature' branch
git checkout -b feature
# Switch to 'develop' branch
git checkout develop
When the current branch has not diverged from the target branch:
# Switch to 'main' branch
git checkout main
# Merge 'feature' into 'main'
git merge feature
When branches have diverged:
# Switch to 'main' branch
git checkout main
# Merge 'feature' into 'main'
git merge feature
If there are conflicts, Git will prompt you to resolve them.
Rebasing moves the entire branch to begin on the tip of another branch, creating a linear history.
# Switch to 'feature' branch
git checkout feature
# Rebase onto 'main'
git rebase main
Modify commits during a rebase:
# Rebase the last 5 commits interactively
git rebase -i HEAD~5
When merging or rebasing, conflicts may occur if changes overlap.
git add <file>
# For merge
git commit
# For rebase
git rebase --continue
# Delete 'feature' branch (only if merged)
git branch -d feature
# Force delete 'feature' branch
git branch -D feature
# Delete 'feature' branch on remote 'origin'
git push origin --delete feature
git branch -r
git fetch
# Create a local tracking branch for 'feature' from remote 'origin'
git checkout -b feature origin/feature
# Push 'feature' branch to 'origin'
git push -u origin feature
The -u
option sets the upstream tracking reference.
A branching model that uses separate branches for features, releases, and hotfixes.
main
, develop
feature/*
, release/*
, hotfix/*
Use dedicated branches for each feature or bug fix:
# Create a feature branch
git checkout -b feature/awesome-feature
After completing the feature:
# Switch to 'develop' branch
git checkout develop
# Merge the feature branch
git merge feature/awesome-feature
# Delete the feature branch
git branch -d feature/awesome-feature
Temporarily save changes that are not ready to commit.
# Save changes with a message
git stash save "Work in progress"
git stash list
# Apply the most recent stash
git stash apply
# Apply a specific stash
git stash apply stash@{1}
Apply and remove from stash:
git stash pop
Apply a specific commit from one branch to another.
# Switch to target branch
git checkout develop
# Cherry-pick a commit by hash
git cherry-pick <commit_hash>
Soft Reset: Moves HEAD to a specific commit, keeps changes staged.
git reset --soft <commit_hash>
Mixed Reset (default): Moves HEAD, unstage changes, keeps working directory.
git reset <commit_hash>
Hard Reset: Moves HEAD, discards changes in working directory and staging area.
git reset --hard <commit_hash>
Create a new commit that undoes the changes from a previous commit:
git revert <commit_hash>
Use binary search to find the commit that introduced a bug.
# Start bisecting
git bisect start
# Mark the current commit as bad
git bisect bad
# Mark a known good commit
git bisect good <commit_hash>
Git will checkout commits between the good and bad commits for testing.
# If the commit is bad
git bisect bad
# If the commit is good
git bisect good
# After identifying the bad commit
git bisect reset
Create tags to mark specific points in history, such as releases.
git tag v1.0
git tag -a v1.0 -m "Version 1.0 release"
# Push a specific tag
git push origin v1.0
# Push all tags
git push origin --tags
Include other Git repositories within a repository.
# Add a submodule
git submodule add <repository_url> <path>
# Initialize submodules
git submodule init
# Update submodules
git submodule update
# Clone and initialize submodules
git clone --recurse-submodules <repository_url>