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
Command
What 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.com
Set 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 init
Turn 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.git
Copy 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 main
Make new repositories start on a branch called mainApplies to every later git init on this computer.
$ git config --list
List every Git setting in effect hereShows system, global and repository settings together; later ones win.Also: git config -l
Staging and committing
12 commands
Command
What it does
$ git status
See which files are changed, staged or untrackedThe command to run whenever you are unsure what state you are in.
$ git status -s
Show 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.js
Stage the file cart.js for the next commitCopies the file as it is now into the staging area (the index).
$ git add -A
Stage 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 -p
Choose 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-edit
Add the staged changes to the last commit, keeping its messageRewrites the last commit. Avoid it on commits you have already pushed.
$ git diff
Show the changes that are not staged yetCompares your working files with the staging area.
$ git diff --staged
Show the changes that are staged for the next commitCompares the staging area with the last commit.Also: git diff --cached
$ git rm --cached .env
Stop 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.js
Rename old.js to new.js and stage the renameSame as renaming the file yourself and staging both sides.
Branches
10 commands
Command
What it does
$ git branch
List your local branchesThe current branch is marked with an asterisk.
$ git branch -a
List local and remote-tracking branchesRemote branches show as remotes/origin/name.Also: git branch --all
$ git branch feature
Create a branch called feature without switching to itThe new branch starts at your current commit.
$ git switch main
Switch to the existing branch maingit switch (Git 2.23+) only changes branches; checkout also restores files.Also: git checkout main
$ git switch -c feature
Create 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 main
Rename the current branch to mainTo rename another branch, give both names: git branch -m old new.Also: git branch --move main
$ git branch -d feature
Delete the merged branch featureRefuses if the branch has unmerged work; -D forces it.Also: git branch --delete feature
$ git branch -D spike
Delete 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 --merged
List branches already merged into the current oneThese are safe to delete with -d.
Merging and rebasing
8 commands
Command
What it does
$ git merge feature
Merge the branch feature into the current branchFast-forwards when it can, otherwise makes a merge commit.
$ git merge --no-ff feature
Merge feature and always make a merge commitKeeps a visible record that the branch existed.
$ git merge --abort
Give up on a merge that has conflictsPuts everything back as it was before the merge started.
$ git rebase main
Replay the current branch's commits on top of mainGives a straight history. Do not rebase commits other people already have.
$ git rebase -i HEAD~3
Edit, 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 --continue
Carry on a rebase after fixing a conflictStage the fixed files first with git add.
$ git rebase --abort
Cancel a rebase in progressReturns the branch to where it was before the rebase.
$ git cherry-pick a1b2c3d
Copy the commit a1b2c3d onto the current branchMakes a new commit with the same changes and message.
Remotes
10 commands
Command
What it does
$ git remote -v
List the remotes with their URLsUsually just origin, shown once for fetch and once for push.Also: git remote --verbose
Add a remote called origin at https://github.com/acme/shop.gitNeeded after git init before you can push.
$ git fetch
Download new commits from origin without changing your filesUpdates origin/* branches only; nothing is merged.Also: git fetch origin
$ git pull
Fetch and merge the remote changes into your branchgit pull is git fetch followed by git merge (or rebase, if configured).
$ git pull --rebase
Pull, rebasing your commits on top instead of mergingAvoids a merge commit for every pull.Also: git pull -r
$ git push
Send your commits to the branch's remoteWorks once the branch has an upstream (see push -u).
$ git push -u origin feature
Push 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-lease
Force-push after a rebase, but only if nobody else pushed meanwhileSafer than --force, which overwrites other people's commits.
$ git push origin --delete feature
Delete the branch feature on originYour local branch stays; delete it with git branch -d.Also: git push -d origin feature
$ git fetch --prune
Fetch 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
Command
What it does
$ git log
Show the commit historyPress q to leave, Space for the next page.
$ git log --oneline
Show the history one commit per lineShort hash and subject only.
$ git log --oneline --graph --all
Show 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.js
Show the commits that changed cart.jsAdd -p to see each change, --follow to track renames.Also: git log -- cart.js
$ git log -p
Show the history with each commit's changesCombine with -2 to see only the last two.Also: git log --patch
$ git log --author=Ada
Show only commits by AdaMatches part of the author name or email.Also: git log --author Ada, git log --author="Ada"
$ git log --grep=login
Find commits whose message mentions loginTo find commits that added or removed a word in the code, use -S word.Also: git log --grep login, git log --grep="login"
$ git show a1b2c3d
Show the details and changes of commit a1b2c3dWith no hash it shows the latest commit.
$ git blame cart.js
See who last changed each line of cart.jsShows the commit, author and date beside every line.
$ git reflog
List everywhere HEAD has pointed recentlyThe way back to commits you lost with reset or a deleted branch.
$ git diff main feature
Compare the branch main with featureUse three dots (main...feature) to see only what feature added.Also: git diff main..feature
Undoing changes
9 commands
Command
What it does
$ git restore cart.js
Throw 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.js
Unstage 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.js
Put 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 a1b2c3d
Undo commit a1b2c3d with a new commit, keeping historyThe safe way to undo something already pushed.
$ git reset --soft HEAD~1
Undo 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~1
Undo 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~1
Throw 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/main
Make your main exactly match origin/mainRun git fetch first. Local commits and edits are thrown away.
$ git clean -fd
Delete untracked files and foldersRun git clean -n first to see what would go.Also: git clean -df, git clean -f -d
Stashing
7 commands
Command
What it does
$ git stash
Put 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 -u
Stash your changes including untracked filesPlain git stash leaves new files behind.Also: git stash --include-untracked, git stash push -u
$ git stash list
List your stashesNewest first: stash@{0}, stash@{1}…
$ git stash pop
Bring back the latest stash and remove it from the listIf it conflicts, the stash is kept until you drop it.
$ git stash apply
Bring back the latest stash but keep it in the listUseful to apply the same changes to several branches.
$ git stash drop
Delete the latest stashgit stash clear deletes every stash.
Tags
4 commands
Command
What it does
$ git tag
List 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 --tags
Push all your tags to origingit push alone does not send tags.Also: git push origin --tags
$ git tag -d v1.0
Delete 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.