← Full reference

git

Version control — commits, branches, merge and rebase, undoing changes, stash, remote repositories

git stores a project's history as a chain of commits — snapshots of files. Changes pass through three places: the working directory (files on disk), the index (what goes into the next commit) and the repository itself (commits already made).

Syntax

git <command> [options] [arguments]

Help for any command — git help <command> or git <command> -h.

How to refer to a commit: a hash (a1b2c3d, 7 characters are enough), a branch (main), a tag (v1.0), HEAD — the current commit, HEAD~1 — its parent, HEAD~3 — three commits back. main..feature — commits that are in feature but not in main.

Commands

Setup

CommandWhat it does
git config user.name 'Name' / user.emailcommit author in this repository; --global — for all
git config --global init.defaultBranch mainname of the first branch for new repositories
git config --global alias.lg 'log --oneline --graph --all'your own short command: git lg
git config --list --show-originall settings and the files they came from

Creating and status

CommandWhat it does
git init [directory]create a repository (.git/ appears)
git clone URL [directory]copy a remote repository
git status / -swhat's changed, what's staged, what's untracked; -s — short
git add file… / git add -Astage changes / all changes, including new and deleted files
git add -ppick individual hunks of changes to stage
git rm file / git rm --cached filedelete a file / stop tracking it, leaving it on disk
git mv old newrename a file
git commit -m 'text'record the index as a new commit
git commit -am 'text'first stage changes to tracked files
git commit --amendredo the last commit (message and/or content)

Viewing history and changes

CommandWhat it does
git log --onelinehistory, one line per commit
git log --graph --allwith branches drawn as a graph
git log -n 5, --since='2 weeks ago', --author=namelimit the output
git log -p / --statwith changes / with a list of files
git log -- pathonly commits that changed the file
git log -S 'text'commits where the string appeared or disappeared
git log --format='%h %an %s'custom format: hash, author, subject
git diffworking directory vs index
git diff --stagedindex vs last commit (what will go into the commit)
git diff A B / --name-only / --statdifference between commits / file names only / summary
git show commit / git show commit:patha commit's changes / a file as it was in the commit
git blame filewho changed each line and in which commit
git reflogwhere HEAD has pointed — the way to rescue "lost" commits

Branches

CommandWhat it does
git branch / -a / -vlocal branches / including remote ones / with the last commit
git branch namecreate a branch without switching
git branch -d name / -D namedelete a merged branch / delete forcibly
git branch -m old newrename
git branch --show-currentname of the current branch
git switch branch / git switch -c branchswitch / create and switch
git switch -go back to the previous branch
git switch --detach commitlook at an old commit without a branch
git checkout branchthe old way to switch (can do both switch and restore)

Combining

CommandWhat it does
git merge branchmerge a branch into the current one (fast-forward if possible)
git merge --no-ff branchalways create a merge commit
git merge --squash branchgather a branch's changes into the index as one future commit
git rebase branchrewrite the current branch's commits on top of the given one
git rebase -i HEAD~3interactively edit the last commits (opens an editor)
git cherry-pick commitbring one commit into the current branch
--continue / --abortfor merge, rebase, cherry-pick: continue after a conflict / cancel

Undoing changes

CommandWhat it does
git restore filerestore a file to the index state (discard edits)
git restore --staged fileunstage a file, edits stay on disk
git restore --source=commit filetake a file's version from a commit
git reset --soft HEAD~1undo a commit, changes stay in the index
git reset HEAD~1 (--mixed)undo a commit, changes stay in the files but not in the index
git reset --hard [commit]move the branch and discard all uncommitted changes
git revert commita new commit that undoes the given one (history isn't rewritten)
git clean -n / -fdshow / delete untracked files and directories

Stash and tags

CommandWhat it does
git stash / git stash push -m 'text'stash uncommitted edits
git stash -utogether with untracked files
git stash list / show -plist / contents of the latest
git stash pop / apply / droprestore and remove / restore and keep / remove
git tag v1.0 / git tag -a v1.0 -m 'text'lightweight / annotated tag
git describe --tagsnearest tag for the current commit

Remote repositories

CommandWhat it does
git remote -v / git remote add origin URLlist / add a remote repository
git fetchdownload new commits without changing your branches
git pullfetch + merge into the current branch (--rebase — via rebase)
git push -u origin branchpush a branch and remember the link; afterwards just git push
git push --tagspush tags

Files git should ignore are listed in .gitignore as patterns (*.log, build/). Why a file is ignored — git check-ignore -v file.

Examples

# introduce yourself (for the current repository; --global — for all)
git config user.name 'Jane Doe'
git config user.email 'jane@example.com'

# a new repository and the first commit
git init
git add README.md
git commit -m 'Add README'

# what changed and what will go into the commit
git status --short
git diff
git diff --staged

# work in a separate branch and merge back
git switch -c feature
git commit -am 'Fix typo'
git switch -
git merge feature

# rewrite your branch on top of a fresh main
git switch feature
git rebase main

# history with branches; what's in feature but not yet in main
git log --oneline --graph --all
git log --oneline main..feature

# temporarily put edits aside, switch, bring them back
git stash
git stash list
git stash pop

# changed your mind: unstage, then discard the file's edits
git restore --staged config.ini
git restore config.ini

# fix the message of the last (not yet pushed) commit
git commit --amend -m 'Better message'

# safely undo a published commit — with a new commit
git revert --no-edit HEAD

Pitfalls

  • The default branch name depends on settings. git init in this version creates master unless init.defaultBranch is set. Check git branch --show-current before writing main in commands.
  • user.name/user.email not set — git either refuses to commit or substitutes something like root@hostname and prints a warning. That author stays in the history.
  • git diff without flags doesn't see what's already staged, and doesn't show new untracked files. For the index — git diff --staged.
  • git commit -a doesn't add new files, only changes to already tracked ones. git add new files first.
  • git reset --hard, git restore file and git clean -f are irreversible for uncommitted edits: they aren't in any commit. In doubt — git stash first. Committed work lost after reset --hard can be found via git reflog.
  • .gitignore doesn't affect already tracked files. First git rm --cached file, then commit.
  • git clean doesn't touch ignored files — for those you also need -x. Always start with -n.
  • git stash doesn't touch untracked files by default. To stash them too, add -u.
  • rebase and commit --amend change commit hashes. Don't rewrite a branch others have already pulled; for published work use revert.
  • git revert of a merge commit requires specifying the parent: git revert -m 1 commit.
  • git branch -d refuses for an unmerged branch (not fully merged). -D deletes it along with commits that exist nowhere else.
  • On a conflict git stops and marks the disputed spots with <<<<<<<, =======, >>>>>>> markers. Fix the files, run git add and continue (--continue) or roll back (--abort).
  • The first git push of a new branch fails with has no upstream branch — you need git push -u origin branch.

See also

make · ssh · gitlab-ci · github-actions