Skip to main content

Branching & Merging

  • git branch → List branches
  • git branch <name> → Create a new branch
  • git checkout <branch> → Switch to a branch
  • git checkout -b <branch> → Create & switch to a new branch
  • git merge <branch> → Merge a branch into current branch
  • git branch -d <branch> → Delete a branch
  • git branch -D <branch> → Force delete a branch

How Branches Work

  • Branch = pointer: A branch is simply a movable pointer to a commit
  • HEAD: Points to the current branch you’re working on
  • Default branch: Usually main (or master in older repos)
  • Local vs. remote branches: Local branches exist in your repo; remote-tracking branches (like origin/main) track branches from remotes

git branch

git branch [options] [<branch-name>] [<start-point>]
# List all local branches:
git branch

# List local and remote branches:
git branch -a

# Create a new branch:
git branch feature-login

# Switch to a branch (with checkout or switch):
git checkout feature-login
git switch feature-login

# Create and switch in one step:
git checkout -b feature-login
git switch -c feature-login

# Delete a branch:
git branch -d feature-login
git branch -D feature-login   # force delete

# Rename a branch:
git branch -m old-name new-name

# Set upstream branch:
git branch --set-upstream-to=origin/main
  • Description: Manages branches in a Git repository.
  • Usage: It shows existing branches, makes new branches pointing to a commit, remove local branches, change branch names, shows details about branch tracking and upstream configuration,
  • Think Tank: Branches are lightweight pointers to commits.
Option Description
-a, --all List both local and remote branches
-r, --remotes List only remote-tracking branches
-d, --delete Delete a branch (safe, refuses if not merged)
-D Force delete a branch
-m, --move Rename a branch
-M Force rename a branch
--set-upstream-to=/ Set upstream branch for tracking
--unset-upstream Remove upstream tracking info
-vv Show branches with detailed tracking info
--show-current Print the name of the current branch
  • Best Practices:
    • Use descriptive branch names (e.g., feature/loginbugfix/payment)
    • Regularly prune merged branches to keep repo clean
    • Always check with git branch -vv to confirm tracking info
    • Avoid force-deleting unless you’re certain
    • Coordinate branch naming conventions in team projects
Git - git-branch Documentation

git checkout

git checkout [options] [<branch>]
# Switch to an existing branch:
git checkout feature/login

# Create and switch to a new branch:
git checkout -b feature/login main

# Restore a file to last committed state on current branch:
git checkout -- path/to/file

# Restore a file from a specific commit or branch:
git checkout <commit-or-branch> -- path/to/file
  • Description: Switches branches, creates new branches, or restores files to a specific state from a commit or branch. It can also put you in a detached HEAD state to inspect past commits.
  • Usage: Move between branches
  • Think Tank: Checkout works with below options:
Option Description
-b  Create and switch to a new branch starting at current commit or given start-point
-B  Like -b but resets the branch if it already exists
--orphan  Create a new branch with no parent commit (empty history)
--detach Check out a commit without attaching to a branch (detached HEAD)
-- Separator indicating following args are pathspecs (file restore mode)
-m Resolve conflicts during checkout with merge-like behavior in some scenarios
-f Force checkout, discarding local changes in the working tree for the paths being checked out
  • Best Practices:
    • Prefer git switch and git restore for clarity: Use git switch to change branches and git restore to revert files; they’re less ambiguous than checkout.
    • Create a branch when exploring history: After checking out a commit, run git switch -c temp-work to avoid losing work.
    • Be explicit with pathspecs: Use -- before file paths to avoid ambiguity with branch/commit names.
    • Check status before force: Run git status and consider stashing changes before -f or file restores.
Git - git-checkout Documentation

git merge

git merge [options] <branch>
# Merge a feature branch into main:
git checkout main
git merge feature-branch

# Fast-forward merge (default when possible):
git merge feature-branch

# Squash merge (single commit):
git merge --squash feature-branch
git commit -m "Squash merge feature-branch"

# Abort a merge in progress:
git merge --abort

# Continue after resolving conflicts:
git merge --continue
  • Description: The git merge command is essential for combining work from different branches into a unified history. 
  • Usage: It takes the commits from the specified branch and integrates them into the current branch, preserves the commit history of both branches, creates a new merge commit if histories have diverged, performs a fast-forward merge if no divergence exists.
  • Think Tank: Merge has below types:
Type Description
Fast-forward merge Moves the branch pointer forward when no new commits exist on the current branch. No merge commit is created
Three-way merge Creates a new merge commit when both branches have diverged. Combines changes from both histories
Squash merge Combines all changes from the merged branch into a single commit on the target branch
Octopus merge Merges multiple branches at once (used for integrating many feature branches)
Option Description
--no-ff Always create a merge commit, even if fast-forward is possible
--squash Squash all changes into a single commit
--abort Cancel a merge in progress and restore pre-merge state
--continue Resume merge after conflicts are resolved
--stat Show diffstat of changes being merged
--no-commit Perform merge but don’t commit automatically
-m  Provide a custom merge commit message
-s  Specify merge strategy (e.g., recursive, octopus)
-X  Pass options to merge strategy (e.g., -X ours)
  • Best Practices:
    • Use fast-forward merges for simple updates
    • Use --no-ff merges to preserve branch history when tracking feature development
    • Resolve conflicts carefully and test thoroughly before committing
    • Consider squash merges for feature branches to keep history clean
    • Delete merged branches after integration to keep repository tidy
Git - git-merge Documentation

Tips, Tricks, Roadmaps, Resources, Networking, Motivation, Guidance, and Cool Stuff ♥

Updated on Dec 31, 2025