Here are some commonly used Git commands related to branches:

1. Branch Creation & Listing

  • git branch → List all local branches (current branch is highlighted with *).
  • git branch <branch_name> → Create a new branch (does not switch to it).
  • git branch -a → List all branches (local + remote).
  • git branch -r → List only remote branches.
  • git branch -v → List branches with the latest commit info.
  • git branch -m <old_name> <new_name> → Rename a local branch.

2. Switching Branches

  • git switch <branch_name> → Switch to an existing branch (Git 2.23+).
  • git switch -c <branch_name> → Create and switch to a new branch (Git 2.23+).

3. Deleting Branches

  • git branch -d <branch_name> → Delete a local branch (safe, checks merge status).
  • git branch -D <branch_name> → Force delete a local branch (unmerged changes will be lost).
  • git push origin --delete <branch_name> → Delete a remote branch.

4. Merging & Rebasing

  • git merge <branch_name> → Merge a branch into the current branch.
  • git rebase <branch_name> → Rebase the current branch onto another branch.
  • git rebase --abort → Abort an ongoing rebase.
  • git rebase --continue → Continue after resolving rebase conflicts.

5. Remote Branches

  • git fetch --all → Fetch all remote branches.
  • git checkout -b <local_branch> origin/<remote_branch> → Create & track a remote branch locally.
  • git push -u origin <branch_name> → Push a local branch and set upstream tracking.

6. Branch Tracking & Upstream

  • git branch --set-upstream-to=origin/<remote_branch> <local_branch> → Set upstream tracking.
  • git branch -vv → View tracked remote branches.
  • git pull origin <branch_name> → Pull changes from a remote branch.

7. Stashing & Branching

  • git stash → Temporarily save uncommitted changes.
  • git stash pop → Reapply stashed changes.
  • git stash branch <branch_name> → Create a new branch from stashed changes.