• git branch -a View local and remote branches
  • git branch View all local branches
  • git branch -r View all remote branches
  • git branch -vv Show local branches associated with remote repositories
  • git checkout -b <branch-name> <existing-branch-name> Create a new branch and switch to the new branch
  • git checkout <commit-id> -b <new-branch-name> Create a new branch from an existing commit and switch to the new branch
  • git branch [-f] <branch-name> Create a new branch, but don’t switch
  • git branch [branch-name] [commit-id] Create a new branch that points to the specified commit
  • git branch <branch-name> <tag-name> Create a new branch based on the tag
  • git branch <-m|-M> <old-branch-name> <new-branch-name> Rename the branch
    #Will not overwrite the same named branch
    git branch -m old-branch-name new-branch-name
    #Forced rename, will overwrite the same branch
    git branch -M old-branch-name new-branch-name
    
  • git branch <-d|-D> <branch-name> Delete the local branch
    #Branch will fail to delete if it is not merged
    git branch -d branch-name 
    #Branch will also delete successfully if it is not merged
    git branch -D branch-name
    
  • git branch <-d|-D> -r <branch-name> Delete the remote branch
  • git checkout <branch-name> switch branches
    # Switch to the previous branch
    git checkout - 
    
  • Delete the remote branch directly (1)
    git branch -d -r branch-name
    #Don't forget to commit the changes to the remote repository
    git push origin :branch-name
    
  • Delete the remote branch directly (2)
    git push origin --delete branch-name 
    
  • Create a branch and associate it with the remote branch
    git checkout -b test
    git push -u origin test
    ...
    #Don't forget to commit your changes to the remote repository
    git push