Git 2 cheatsheet

Git 2: Branches

9 common Git 2 tasks.

List branches

Show local branches and mark the current one.

git branch

Create and switch to a branch

Create a new branch from the current commit and switch to it.

git switch -c feature/login

Switch branch

Move to an existing branch.

git switch main

Rename the current branch

Rename the branch you are on.

git branch -m new-name

Delete a merged branch

Delete a local branch that has been merged. Git refuses if it has unmerged work.

git branch -d feature/login

Delete an unmerged branch

Delete a local branch even if its commits are not merged.

git branch -D feature/login

Caution: Unmerged commits become hard to find. You can usually recover them with git reflog.

Delete a remote branch

Remove a branch from the remote.

git push origin --delete feature/login

Caution: The branch disappears for everyone who uses the remote. Check that nobody needs it first.

Merge a branch

Merge another branch into the one you are on.

git merge feature/login

Rebase onto another branch

Replay your commits on top of main.

git rebase main

Caution: Rebasing rewrites commits. Don't rebase branches other people have pulled.

Was this page helpful?