> For the complete documentation index, see [llms.txt](https://yu-sun.gitbook.io/essential-git-for-scientists/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yu-sun.gitbook.io/essential-git-for-scientists/cookbook/branches.md).

# Branches

### Notes

* A branch is a reference to an existing commit (not as intuitively thought as a chain of commits).
* A branch diverges from another branch only if the referenced commit shares an ancestor with another commit.

### Commands

* Show branches
  * `git branch --list`, or simply `git branch`
  * `git branch -r` to list remote branches
  * `git show-branch -a` show a detailed list
  * `git log --oneline --decorate --graph --all` to show a visual diagram
* Create a branch
  * `git branch <branch-name>`
* Change to a branch, both are equivalent. Using `switch` is more unambiguous.
  * `git checkout <branch-name>`
  * `git switch <branch-name>`
* Rename a branch
  * `git branch -m <new-name>` rename current branch
  * `git branch -m <old-name> <new-name>` other branch
* Delete a local branch
  * `git branch -d <branch-name>` if fully merged
  * `git branch -D <branch-name>` if not fully merged, could result in data loss. Use with caution.
* Delete a remote branch
  * `git push origin --delete <branch-name>`
