git commands


git branch

List, create, rename and delete branches, local and remote.

git branch
Save

Angle brackets are yours to fill in. Windows, Linux, macOS.

Examples for git branch

  • Delete a local branch

    git branch -d <branch>
    Save

    Refuses if the branch has commits that are not merged, which protects work you have not kept. Use -D to delete it anyway. This removes only your copy: the branch on the remote stays.

    gittoolbranchsubcommand-donly if merged<branch>argument
  • Delete a branch on the remote

    git push origin --delete <branch>
    Save

    git branch cannot delete a remote branch; a push does. After this, other clones still show the branch until they run git fetch --prune.

    gittoolpushsubcommandoriginargument--deleteflag<branch>argument
  • Rename the current branch

    git branch -m <new-name>
    Save

    Renames the branch you are on. To rename another one, give both names: git branch -m <old> <new>. A branch already on the remote keeps its old name there: push the new name with -u, then delete the old one.

    gittoolbranchsubcommand-mmove, meaning rename<new-name>argument
  • List all branches, including remote ones

    git branch -a
    Save

    Shows local branches and the remote-tracking ones, as remotes/origin/<name>. The list is as recent as your last fetch, so run git fetch first to see new branches.

    gittoolbranchsubcommand-alocal and remote
  • See which branches are ahead or behind the remote

    git branch -vv
    Save

    Each line shows the branch's upstream in brackets with ahead and behind counts, such as [origin/main: ahead 2]. A branch marked gone had its remote branch deleted.

    gittoolbranchsubcommand-vvupstream and ahead or behind
  • Find branches that are safe to delete

    git branch --merged main
    Save

    Lists branches whose commits are all in main, so deleting them loses nothing. The list includes main itself and the branch you are on; leave those.

    gittoolbranchsubcommand--mergedflag mainmerged into this branch

Flags

--delete -d
Delete a branch, but only if it is merged into its upstream or HEAD.
-D
Delete a branch whether it is merged or not.Commits that exist only on that branch are no longer on any branch. git reflog can still find them for a while.
--move -m
Rename a branch.
--all -a
List remote-tracking branches as well as local ones.
--remotes -r
List only remote-tracking branches.
-vv
Show each branch's last commit, its upstream, and how far ahead or behind it is.
--merged
List only branches already merged into the current one.
--show-current
Print the current branch's name only.