A branch is a lightweight, movable name for a line of commits. These commands create branches, move between them, and bring their work together.
Lists your local branches and marks the one you currently have checked out.
Add -a to include remote-tracking branches, which reflect the remote as of your last fetch rather than its live state. The same command renames branches with -m.
Local branches, with the current one starred.
Local and remote-tracking branches together.
Show each branch's latest commit, its upstream, and whether it is ahead or behind.
Shell
git branch --merged main
List branches whose commits are all already in main.
- -a
- Include remote-tracking branches.
- -vv
- Add the last commit, upstream branch, and ahead/behind counts.
- -m <old> <new>
- Rename a branch.
- --merged / --no-merged
- Show only branches that are, or are not, merged into the given commit.
Creates a new branch at the current commit without switching to it.
You remain on your current branch afterwards, which is easy to forget if you commit straight away. To create a branch and move onto it together, use git switch -c instead. Naming a commit, tag, or branch after the new name starts the branch from there.
Shell
git branch feature/login
Create a branch at the current commit.
Shell
git branch hotfix v1.2.0
Start a branch from a tag.
Shell
git branch experiment a1b2c3d
Start a branch from a specific commit.
Deletes a local branch, but only when its work is already merged.
Git checks that the branch is merged into its upstream, or into your current branch if it has no upstream, and refuses otherwise, which protects commits that exist nowhere else. It never touches the copy of the branch on a remote, and you cannot delete the branch you are currently on.
Shell
git branch -d feature/login
Remove a finished branch.
Shell
git branch -d feature/login feature/signup
Remove several merged branches at once.
Deletes a local branch even when its commits are not merged anywhere.
It is shorthand for --delete --force and skips the merge check that -d performs. Use it for abandoned experiments you are certain you no longer need.
Careful: Commits that lived only on that branch stop being reachable from any branch. If you had them checked out recently, git reflog can usually help you find and restore them, but they vanish from normal history and are eventually cleaned up for good.
Shell
git branch -D spike/old-idea
Delete an unmerged branch.
Moves you onto another existing branch and updates your files to match it.
Uncommitted changes come along when they do not clash with the target branch; if they would be overwritten, Git refuses, so commit or stash first. When a branch exists only on the remote, switching to its name creates a local branch that tracks it, as long as exactly one remote has a branch by that name.
Jump back to the branch you were on before.
Shell
git switch --detach v1.0.0
Look at a tagged release without being on any branch.
- -c <new-branch>
- Create the branch first, then switch to it.
- --detach
- Check out a commit or tag directly instead of a branch.
Creates a new branch and switches to it in a single step.
The branch starts at your current commit unless you give a different starting point. Uncommitted changes move across with you, which helps when you realise halfway through an edit that the work deserves its own branch. The older equivalent is git checkout -b.
Shell
git switch -c feature/search
Create a branch and move onto it.
Shell
git switch -c hotfix origin/main
Start the new branch from the remote's copy of main.
Switches branches or restores files, handling both jobs that newer Git splits between switch and restore.
Because one command covered unrelated tasks, Git 2.23 added git switch for branches and git restore for files; checkout still works and is common in older tutorials and scripts. Given a commit ID it puts you in detached HEAD mode, where new commits belong to no branch until you create one.
Careful: When you pass a file path, as in git checkout -- file, it overwrites your uncommitted edits to that file without asking, and those edits cannot be recovered.
Switch branches, like git switch main.
Shell
git checkout -b feature/search
Create and switch to a branch, like git switch -c.
Shell
git checkout -- src/app.ts
Discard edits to a file, like git restore.
Shell
git checkout a1b2c3d
Inspect an old commit in detached HEAD mode.
Brings the commits from another branch into the branch you are on.
If your branch has not moved since the other one split off, Git just slides your branch forward (a fast-forward); otherwise it creates a merge commit with two parents. When both sides changed the same lines it stops with conflicts, which you fix, stage, and finish with git commit or git merge --continue.
Shell
git merge feature/search
Merge feature/search into the current branch.
Shell
git merge --no-ff feature/search
Always create a merge commit, even when a fast-forward is possible.
Abandon a conflicted merge and return to where you started.
- --no-ff
- Record a merge commit even if a fast-forward would work.
- --ff-only
- Refuse to merge unless it can be a fast-forward.
- --squash
- Stage the combined changes without creating a merge commit, leaving you to commit them as one.
- --abort
- Cancel a merge that stopped because of conflicts.
Replays your branch's commits on top of another commit so that history forms a straight line.
Reach for it to bring a feature branch up to date with main without adding a merge commit. Every replayed commit receives a new ID. The interactive form, git rebase -i, lets you reorder, reword, squash, or drop commits along the way.
Careful: Rebasing replaces commits with rewritten copies. Do not rebase commits other people have already pulled, such as a shared main or a branch teammates are building on, because their history will no longer match yours and they will have to untangle duplicate commits. Keep it to commits only you have, or agree on it with your team before force-pushing.
Move the current branch's commits onto the tip of main.
Shell
git rebase -i HEAD~3
Reword, squash, reorder, or drop the last three commits.
Shell
git rebase --continue
Carry on after fixing and staging a conflict.
Stop and put the branch back exactly as it was.
- -i
- Open a to-do list in your editor to pick, reword, squash, fixup, or drop each commit.
- --onto <newbase>
- Move a range of commits onto a different base than the one they grew from.
- --autosquash
- Place commits made with git commit --fixup next to the commits they correct.
- --skip
- Leave out the commit that is causing a conflict and continue.
Copies the changes from one or more existing commits onto the current branch as new commits.
Handy for carrying a single bug fix over to a release branch without merging everything else. The copy gets its own ID and has no link to the original, so merging both branches later can still produce conflicts if the same lines changed again afterwards.
Shell
git cherry-pick a1b2c3d
Apply one commit to the current branch.
Shell
git cherry-pick -x a1b2c3d
Note the original commit ID in the new commit's message.
Shell
git cherry-pick a1b2c3d~1..e4f5a6b
Apply every commit from a1b2c3d through e4f5a6b, including both ends.
Shell
git cherry-pick --abort
Cancel a cherry-pick that stopped on a conflict.
- -x
- Add a line to the message naming the commit it was copied from.
- -n
- Apply the changes to your files and staging area without committing.
- --continue
- Resume after resolving a conflict.