Skip to content

↑↓ moveEnter openEsc close

Git commands

Git 2.51 · 77 commands · checked Nov 2026 · Source: Git reference

Git is the version control system behind almost every code project. These are the commands you type every day to stage, commit, branch, merge, sync with a remote and undo mistakes.

Practise by typing11 short lessons with spaced review. Type each command from its description.

Setting up

6 commands
CommandWhat it does
git config --global user.name "Ada Lovelace"Set your name for every repository on this computer to Ada LovelaceStored in ~/.gitconfig and written into every commit you make.
git config --global user.email ada@example.comSet your email for every repository to ada@example.comUse the address your Git host knows, so commits link to your account.Also: git config --global user.email "ada@example.com"
git initTurn the current folder into a new Git repositoryCreates the hidden .git folder. Nothing is tracked until you add and commit files.
git clone https://github.com/acme/shop.gitCopy the repository at https://github.com/acme/shop.git to your computerDownloads the whole history into a new folder named shop and sets the remote origin.
git config --global init.defaultBranch mainMake new repositories start on a branch called mainApplies to every later git init on this computer.
git config --listList every Git setting in effect hereShows system, global and repository settings together; later ones win.Also: git config -l

Staging and committing

12 commands
CommandWhat it does
git statusSee which files are changed, staged or untrackedThe command to run whenever you are unsure what state you are in.
git status -sShow the status in the short, one-line-per-file formTwo columns: staged on the left, unstaged on the right (M modified, A added, ?? untracked).Also: git status --short
git add cart.jsStage the file cart.js for the next commitCopies the file as it is now into the staging area (the index).
git add -AStage every change in the repository, including new and deleted filesgit add . does the same for the current folder and below.Also: git add --all
git add -pChoose piece by piece which changes to stageShows each hunk and asks y (stage), n (skip), s (split) or q (quit).Also: git add --patch
git commit -m "Fix total"Commit what is staged with the message Fix totalWithout -m Git opens your editor for the message.
git commit -am "Tidy"Stage every tracked file that changed and commit it with the message TidySkips git add for tracked files. New, untracked files are not included.Also: git commit -a -m "Tidy"
git commit --amend --no-editAdd the staged changes to the last commit, keeping its messageRewrites the last commit. Avoid it on commits you have already pushed.
git diffShow the changes that are not staged yetCompares your working files with the staging area.
git diff --stagedShow the changes that are staged for the next commitCompares the staging area with the last commit.Also: git diff --cached
git rm --cached .envStop tracking .env but keep the file on diskAdd the file to .gitignore too, or it shows up as untracked again.
git mv old.js new.jsRename old.js to new.js and stage the renameSame as renaming the file yourself and staging both sides.

Branches

10 commands
CommandWhat it does
git branchList your local branchesThe current branch is marked with an asterisk.
git branch -aList local and remote-tracking branchesRemote branches show as remotes/origin/name.Also: git branch --all
git branch featureCreate a branch called feature without switching to itThe new branch starts at your current commit.
git switch mainSwitch to the existing branch maingit switch (Git 2.23+) only changes branches; checkout also restores files.Also: git checkout main
git switch -c featureCreate a branch called feature and switch to itThe most used way to start new work.Also: git checkout -b feature
git switch -Switch back to the branch you were on beforeLike cd - in the shell.Also: git checkout -
git branch -m mainRename the current branch to mainTo rename another branch, give both names: git branch -m old new.Also: git branch --move main
git branch -d featureDelete the merged branch featureRefuses if the branch has unmerged work; -D forces it.Also: git branch --delete feature
git branch -D spikeDelete the branch spike even though it is not mergedIts commits are lost once nothing else points to them (the reflog keeps them for a while).
git branch --mergedList branches already merged into the current oneThese are safe to delete with -d.

Merging and rebasing

8 commands
CommandWhat it does
git merge featureMerge the branch feature into the current branchFast-forwards when it can, otherwise makes a merge commit.
git merge --no-ff featureMerge feature and always make a merge commitKeeps a visible record that the branch existed.
git merge --abortGive up on a merge that has conflictsPuts everything back as it was before the merge started.
git rebase mainReplay the current branch's commits on top of mainGives a straight history. Do not rebase commits other people already have.
git rebase -i HEAD~3Edit, squash or reorder the last 3 commitsOpens a list where you change pick to squash, reword, edit or drop.Also: git rebase --interactive HEAD~3
git rebase --continueCarry on a rebase after fixing a conflictStage the fixed files first with git add.
git rebase --abortCancel a rebase in progressReturns the branch to where it was before the rebase.
git cherry-pick a1b2c3dCopy the commit a1b2c3d onto the current branchMakes a new commit with the same changes and message.

Remotes

10 commands
CommandWhat it does
git remote -vList the remotes with their URLsUsually just origin, shown once for fetch and once for push.Also: git remote --verbose
git remote add origin https://github.com/acme/shop.gitAdd a remote called origin at https://github.com/acme/shop.gitNeeded after git init before you can push.
git fetchDownload new commits from origin without changing your filesUpdates origin/* branches only; nothing is merged.Also: git fetch origin
git pullFetch and merge the remote changes into your branchgit pull is git fetch followed by git merge (or rebase, if configured).
git pull --rebasePull, rebasing your commits on top instead of mergingAvoids a merge commit for every pull.Also: git pull -r
git pushSend your commits to the branch's remoteWorks once the branch has an upstream (see push -u).
git push -u origin featurePush the new branch feature to origin and track itAfter this, plain git push and git pull know where to go.Also: git push --set-upstream origin feature
git push --force-with-leaseForce-push after a rebase, but only if nobody else pushed meanwhileSafer than --force, which overwrites other people's commits.
git push origin --delete featureDelete the branch feature on originYour local branch stays; delete it with git branch -d.Also: git push -d origin feature
git fetch --pruneFetch and remove remote-tracking branches deleted on the serverKeeps git branch -a free of branches that no longer exist.Also: git fetch -p

History

11 commands
CommandWhat it does
git logShow the commit historyPress q to leave, Space for the next page.
git log --onelineShow the history one commit per lineShort hash and subject only.
git log --oneline --graph --allShow every branch as a one-line graphThe quickest picture of how branches split and merge.Also: git log --graph --oneline --all, git log --all --graph --oneline
git log cart.jsShow the commits that changed cart.jsAdd -p to see each change, --follow to track renames.Also: git log -- cart.js
git log -pShow the history with each commit's changesCombine with -2 to see only the last two.Also: git log --patch
git log --author=AdaShow only commits by AdaMatches part of the author name or email.Also: git log --author Ada, git log --author="Ada"
git show a1b2c3dShow the details and changes of commit a1b2c3dWith no hash it shows the latest commit.
git blame cart.jsSee who last changed each line of cart.jsShows the commit, author and date beside every line.
git reflogList everywhere HEAD has pointed recentlyThe way back to commits you lost with reset or a deleted branch.
git diff main featureCompare the branch main with featureUse three dots (main...feature) to see only what feature added.Also: git diff main..feature

Undoing changes

9 commands
CommandWhat it does
git restore cart.jsThrow away your unstaged changes to cart.jsThe edits are gone for good; Git never had them.Also: git checkout -- cart.js
git restore --staged cart.jsUnstage cart.js but keep the editsThe opposite of git add.Also: git reset cart.js, git reset HEAD cart.js
git restore --source a1b2c3d cart.jsPut cart.js back as it was in commit a1b2c3dOnly that file changes; the branch stays where it is.Also: git restore --source=a1b2c3d cart.js, git checkout a1b2c3d -- cart.js
git revert a1b2c3dUndo commit a1b2c3d with a new commit, keeping historyThe safe way to undo something already pushed.
git reset --soft HEAD~1Undo the last commit but keep its changes stagedHandy to redo a commit with a better message or more files.Also: git reset --soft HEAD~, git reset --soft HEAD^
git reset HEAD~1Undo the last commit and unstage its changes, keeping the editsThe default reset: files keep the changes, nothing is staged.Also: git reset HEAD~, git reset HEAD^
git reset --hard HEAD~1Throw away the last commit and all its changesDestructive: uncommitted work is lost. git reflog can still find the commit.Also: git reset --hard HEAD~, git reset --hard HEAD^
git reset --hard origin/mainMake your main exactly match origin/mainRun git fetch first. Local commits and edits are thrown away.
git clean -fdDelete untracked files and foldersRun git clean -n first to see what would go.Also: git clean -df, git clean -f -d

Stashing

7 commands
CommandWhat it does
git stashPut your uncommitted changes aside for laterLeaves a clean working tree so you can switch branches.Also: git stash push
git stash push -m "half-done login"Stash your changes with the note half-done loginThe note shows in git stash list.Also: git stash -m "half-done login"
git stash -uStash your changes including untracked filesPlain git stash leaves new files behind.Also: git stash --include-untracked, git stash push -u
git stash listList your stashesNewest first: stash@{0}, stash@{1}…
git stash popBring back the latest stash and remove it from the listIf it conflicts, the stash is kept until you drop it.
git stash applyBring back the latest stash but keep it in the listUseful to apply the same changes to several branches.
git stash dropDelete the latest stashgit stash clear deletes every stash.

Tags

4 commands
CommandWhat it does
git tagList the tagsAdd -l "v1.*" to filter.
git tag -a v1.0 -m "First release"Make an annotated tag v1.0 with the message First releaseAnnotated tags store who tagged, when and why; use them for releases.
git push --tagsPush all your tags to origingit push alone does not send tags.Also: git push origin --tags
git tag -d v1.0Delete the local tag v1.0Remove it on the remote with git push origin --delete v1.0.Also: git tag --delete v1.0

Commands checked against the Git reference for Git 2.51. Names such as cart.js, feature and a1b2c3d stand for your own files, branches and commit hashes. To learn them, open the Git practice: it gives the task and you type the command.