git commands
nicholas chen · december 21, 2025 · 4 min read

through various work experiences and side projects, i've picked up a lot of git commands that have saved my life more than once. i wanted to create a central place to store all these commands for easy reference.
mental model
git is a distributed version control system (local). github is a hosting platform (online). most of us memorize commands without understanding the graph model underneath.
core concepts
snapshots, not diffs
git stores a full snapshot of your project with every commit, not just the differences. if a file hasn't changed, it stores a pointer to the previous version.
the three trees
- working directory: files you see/edit.
- staging area (index): changes ready for commit.
- HEAD: pointer to the last commit.
setup & config
git config --global user.name "name"set your username for commits.
git config --global user.email "email"set your email for commits.
git config --global color.ui autoenable helpful color output.
git config --listshow all configuration settings.
git config --global alias.co checkoutcreate a shortcut: type 'git co' instead of 'git checkout'.
git config --global core.editor "code --wait"set vs code as default editor for commit messages.
getting & creating projects
git initinitialize a new repo in current directory.
git clone <url>download a repo and its entire history.
git clone --depth=1 <url>shallow clone (latest snapshot only, faster).
basic snapshotting
git commit --amend --no-editadd staged changes to last commit without changing message.
git statusshow modified, staged, and untracked files.
git add <file>stage a specific file for the next commit.
git add .stage all changes in current directory.
git add -pinteractively choose chunks of code to stage.
git commit -m "msg"save staged changes as a new snapshot.
git commit -am "msg"stage tracked files and commit in one step.
git rm <file>remove a file from working tree and index.
git mv <old> <new>move or rename a file.
branching & merging
git switch -quickly switch back to the previous branch.
git branch --mergedlist branches already merged into current.
git merge-base A Bfind the common ancestor of two branches.
git branchlist all local branches.
git branch <name>create a new branch (pointer).
git branch -d <name>delete a merged branch safely.
git branch -D <name>force delete a branch (even if unmerged).
git switch <name>switch to a branch.
git switch -c <name>create and switch to a new branch.
git merge <branch>combine history of another branch into current.
git merge --abortcancel a merge in progress and return to pre-merge state.
git tag <name>mark the current commit with a tag.
git tag -a <name> -m "msg"create an annotated tag with metadata.
sharing & updating
git remote rename <old> <new>rename a remote connection.
git push origin :<branch>delete a remote branch.
git remote -vlist all remote repositories.
git remote add origin <url>connect local repo to a remote one.
git remote set-url origin <url>change the url of an existing remote.
git fetchdownload changes from remote but don't merge.
git fetch --prunedelete local refs to remote branches that were deleted.
git pullfetch + merge (update current branch).
git pushupload local commits to remote.
git push -u origin <branch>push and set upstream tracking.
git push --force-with-leasesafer force push; fails if someone else pushed.
git push --tagspush all local tags to remote.
inspection & comparison
git log -S "text"search history for the first occurrence of a string.
git log --author="name"filter commit history by author.
git log --since="2.weeks"show commits from a specific timeframe.
git logshow commit history.
git log --oneline --graphvisualize commit history graph compactly.
git shortlog -snshow summary of commits by author.
git diffshow unstaged changes.
git diff --stagedshow staged changes (what will be committed).
git diff --word-diffhighlight changed words instead of whole lines.
git show <hash>show changes and metadata for a specific commit.
git blame <file>show who modified each line of a file.
git blame -L 10,20 <file>blame only lines 10 through 20.
git grep "text"search for text inside tracked files.
undo & fix
git checkout <hash> -- <file>restore a file to a specific past version.
git update-ref -d HEADeffectively "un-initialize" the first commit of a repo.
git reset --soft HEAD~1undo last commit but keep changes staged.
git reset --hard HEAD~1undo last commit and discard all changes (dangerous).
git revert <hash>create new commit that reverses a previous one.
git restore <file>discard local changes in a file.
git restore --staged <file>unstage a file (keep changes in working directory).
git commit --amendadd staged changes to previous commit / edit message.
advanced & power tools
git rebase -i HEAD~3interactively rewrite history: squash, fixup, reorder, or drop.
git rebase <branch>reapply commits on top of another base tip.
git stashtemporarily shelve dirty changes.
git stash popreapply stashed changes and remove from stash list.
git stash listlist all stashed changesets.
git stash -ustash including untracked files.
git bisect startstart binary search to find the commit that introduced a bug.
git cherry-pick <hash>apply the changes from a specific commit.
git reflogshow a log of all reference movements (recover lost commits).
git worktree add <path> <branch>checkout multiple branches in parallel folders.
git submodule update --initfetch and update submodule dependencies.
git rererereuse recorded resolution of conflicted merges.
administration
git clean -fdremove all untracked files and directories.
git gccleanup unnecessary files and optimize local repo.
git check-ignore -v <file>debug why a file is being ignored.
git archive -o project.zip HEADexport the current branch to a zip file.
git rev-parse HEADoutput the full SHA-1 hash of the current commit.