Git Branches, Log, and Merge
This document covers standard branch operations for daily Git usage including: reading the commit log, listing branches, creating and switching branches, pushing to remote, pulling remote branches locally, and merging a feature branch into main.
Quick reference
Section titled “Quick reference”# loggit log --oneline -20git log --oneline --graph --all
# branchesgit branchgit branch -agit fetch origin
# create, switch, pushgit switch -c feature-branchgit push -u origin feature-branch
# pull remote branchgit fetch origingit switch -c feature-branch --track origin/feature-branch
# merge to maingit switch maingit pull origin maingit merge feature-branchgit push origin main
# delete branchesgit branch -d feature-branchgit branch -D feature-branchgit push origin --delete feature-branch
# prune stale remote-tracking refsgit branch -rgit ls-remote --heads origingit fetch origin --prunegit remote prune origingit config --global fetch.prune trueView commit log
Section titled “View commit log”The log shows commit history for the current branch. The oneline format keeps it scannable.
# last 20 commits, one line eachgit log --oneline -20
# full detail on last 5git log -5
# all branches as a graphgit log --oneline --graph --allList branches
Section titled “List branches”Local branches are shown by default. Use -a to include remote branches. Fetch first to make sure the remote list is current.
git branch # local onlygit branch -a # local + remotegit branch --show-current # active branchgit fetch origin # update remote refsCreate a new branch and push
Section titled “Create a new branch and push”Create a branch from the current HEAD, switch to it, and push with upstream tracking so future pushes need no arguments.
git switch -c feature-branchgit push -u origin feature-branchPull a remote branch locally
Section titled “Pull a remote branch locally”When a branch exists on the remote but not locally, fetch first then create it with tracking.
git fetch origingit switch -c feature-branch --track origin/feature-branchSwitch between branches
Section titled “Switch between branches”Use switch to move between existing local branches. Working tree must be clean or changes will block the switch.
git switch maingit switch feature-branchMerge a branch into main
Section titled “Merge a branch into main”Switch to main, pull the latest, merge the feature branch, push, and clean up. This assumes no conflicts.
git switch maingit pull origin maingit merge feature-branchgit push origin mainDelete merged branch
Section titled “Delete merged branch”After a successful merge, remove the branch locally. Delete the remote branch separately only when the branch should also be removed from the remote repository.
# check current local branchesgit branch
# delete local after mergegit branch -d feature-branch
# force delete local even if Git does not see it as mergedgit branch -D feature-branch
# delete the real branch from the remote repositorygit push origin --delete feature-branch- git branch -d deletes a local branch only when Git considers it merged.
- git branch -D force-deletes a local branch. Use it only when you are sure the work is no longer needed.
- git push origin –delete branch_name deletes the branch from the remote repository, not just from your local branch list.
Prune stale remote branches
Section titled “Prune stale remote branches”Remote-tracking branches such as origin/old-feature can remain in your local repo after the real remote branch is deleted. Check what your local repo remembers, compare it with the remote, then prune stale origin/* references.
# check current remote-tracking branches remembered locallygit branch -r
# check which branches actually exist on the remotegit ls-remote --heads origin
# remove stale origin/* references that no longer exist on GitHubgit fetch origin --prune
# alternative direct prune command for the origin remotegit remote prune origin
# check again after cleanupgit branch -rgit fetch origin –prune is usually the most useful command because it updates from origin and prunes stale remote-tracking refs in one step. git remote prune origin only prunes stale refs for origin.
git fetch origin --prune
# Expected output when stale remote-tracking branches are removed:# - [deleted] (none) -> origin/old-feature-branch# - [deleted] (none) -> origin/old-cleanup-branch# - [deleted] (none) -> origin/old-test-branchMake pruning automatic if you want future fetches to clean stale remote-tracking branches by default.
git config --global fetch.prune trueDo not use git fetch -r. The fetch command does not use -r as a prune shortcut.
git fetch -r
# Expected error:# error: unknown switch `r`# usage: git fetch [<options>] [<repository> [<refspec>...]]Confirm remote branch list
Section titled “Confirm remote branch list”List remote-tracking branches after pruning. This should only show branches that still exist on the remote.
git branch -r
# Expected output:# origin/HEAD -> origin/main# origin/active-feature-branch# origin/main