Skip to content

Git Commands Explained

The Git commands you actually use, grouped by job: set up a repo, snapshot work, branch and merge, read history, share with a remote, undo mistakes, stash work, and manage files and submodules. Each row pairs the command with a realistic invocation.

Git has hundreds of commands, but daily work needs only a few. The mental model is: the working tree is your files, the index (staging area) is the next commit, and HEAD is the current commit. You stage, you commit, you share. Read status often. Everything below assumes a command already exists if it is listed.

Reference table · 55 entries
55 of 55 rows
Setup & config
Set your name, email, or editor once for every repo.git config --global user.name "Ada"
Define a shortcut name for a command you type often.git config --global alias.co checkout
Create a new repository in the current folder.git init
Copy a remote repository onto your machine.git clone git@github.com:org/repo.git
Copy only the newest commits of a single branch.git clone --depth 1 --single-branch git@github.com:org/repo.git
Snapshot basics
Show what is staged, modified, and untracked.git status -sb
Stage changes so they enter the next commit.git add src/app.ts
Review changes hunk by hunk and stage only the ones you accept.git add -p src/app.ts
Stage changes to every tracked file without adding new files.git add -u
Record the staged changes as a new commit.git commit -m "fix: null check"
Replace the last commit with a corrected one.git commit --amend -m "fix: handle empty input"
Show unstaged changes, or staged changes with --staged.git diff --staged
Show every change since the last commit, staged or not.git diff HEAD
Branching & merging
List, create, or delete branches.git branch feature-x
Delete a merged branch (-D forces it, -a lists every branch).git branch -d feature-x
Switch branches (the modern replacement for checkout).git switch feature-x
Create a new branch and switch to it in one step.git switch -c feature-x
Switch branches or restore files (the older, broader command).git checkout feature-x
Bring another branch's changes into the current branch.git merge feature-x
Cancel a conflicted merge and return the branch to its prior state.git merge --abort
Replay the current branch's commits on top of another branch.git rebase main
Squash, reorder, or edit commits while replaying them.git rebase -i main
Copy a single commit from another branch onto this one.git cherry-pick a1b2c3d
Mark a commit with a readable label such as a version.git tag -a v1.2.0
Check out another branch in a separate folder at the same time.git worktree add ../repo-feature feature-x
Inspecting history
Walk the commit history.git log --oneline --graph --decorate
Walk history from every branch, not just the current one.git log --oneline --graph --all
Show the full diff and message of one commit.git show HEAD
Show who last changed each line of a file.git blame src/app.ts
Show every commit HEAD has pointed to, including discarded ones.git reflog
Search history by halves to find the commit that broke something.git bisect start
Summarize commits by author.git shortlog -sn
Name the current commit by counting from the nearest tag.git describe --tags
Remote collaboration
List or add remote repositories.git remote add origin <url>
List remotes with their fetch and push URLs.git remote -v
Download remote changes without changing your branches.git fetch origin
Download remote changes and drop refs to deleted remote branches.git fetch --prune
Fetch and merge the current branch's remote changes.git pull --rebase
Send local commits to the remote.git push -u origin feature-x
Overwrite the remote branch, but only if nobody pushed first.git push --force-with-lease origin feature-x
Undoing changes
Discard working-tree changes, or unstage them.git restore src/app.ts
Unstage a file and keep its edits in the working tree.git restore --staged src/app.ts
Move the current branch tip and optionally the index.git reset --hard HEAD~1
Add a new commit that undoes an earlier one (safe to share).git revert HEAD
Delete untracked files and folders.git clean -fd
Stashing
Save uncommitted work and reset the working tree.git stash -u
Show every stash you have saved.git stash list
Reapply a stash and keep it stored.git stash apply stash@{1}
Delete a stash you no longer need.git stash drop stash@{1}
Reapply the most recent stash and drop it.git stash pop
Files & submodules
Move or rename a file and stage the change.git mv old.ts src/new.ts
Delete a file and stage the deletion.git rm src/old.ts
Track another repository as a folder inside this one.git submodule add git@github.com:org/lib.git lib
Check out only the folders you name from a large repository.git sparse-checkout set src docs
Export a commit's tree as a tar or zip file.git archive -o repo.zip HEAD