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
| Command | What it does |
|---|---|
git config user.name 'Name' / user.email | commit author in this repository; --global — for all |
git config --global init.defaultBranch main | name 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-origin | all settings and the files they came from |
Creating and status
| Command | What it does |
|---|---|
git init [directory] | create a repository (.git/ appears) |
git clone URL [directory] | copy a remote repository |
git status / -s | what's changed, what's staged, what's untracked; -s — short |
git add file… / git add -A | stage changes / all changes, including new and deleted files |
git add -p | pick individual hunks of changes to stage |
git rm file / git rm --cached file | delete a file / stop tracking it, leaving it on disk |
git mv old new | rename 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 --amend | redo the last commit (message and/or content) |
Viewing history and changes
| Command | What it does |
|---|---|
git log --oneline | history, one line per commit |
git log --graph --all | with branches drawn as a graph |
git log -n 5, --since='2 weeks ago', --author=name | limit the output |
git log -p / --stat | with changes / with a list of files |
git log -- path | only 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 diff | working directory vs index |
git diff --staged | index vs last commit (what will go into the commit) |
git diff A B / --name-only / --stat | difference between commits / file names only / summary |
git show commit / git show commit:path | a commit's changes / a file as it was in the commit |
git blame file | who changed each line and in which commit |
git reflog | where HEAD has pointed — the way to rescue "lost" commits |
Branches
| Command | What it does |
|---|---|
git branch / -a / -v | local branches / including remote ones / with the last commit |
git branch name | create a branch without switching |
git branch -d name / -D name | delete a merged branch / delete forcibly |
git branch -m old new | rename |
git branch --show-current | name of the current branch |
git switch branch / git switch -c branch | switch / create and switch |
git switch - | go back to the previous branch |
git switch --detach commit | look at an old commit without a branch |
git checkout branch | the old way to switch (can do both switch and restore) |
Combining
| Command | What it does |
|---|---|
git merge branch | merge a branch into the current one (fast-forward if possible) |
git merge --no-ff branch | always create a merge commit |
git merge --squash branch | gather a branch's changes into the index as one future commit |
git rebase branch | rewrite the current branch's commits on top of the given one |
git rebase -i HEAD~3 | interactively edit the last commits (opens an editor) |
git cherry-pick commit | bring one commit into the current branch |
--continue / --abort | for merge, rebase, cherry-pick: continue after a conflict / cancel |
Undoing changes
| Command | What it does |
|---|---|
git restore file | restore a file to the index state (discard edits) |
git restore --staged file | unstage a file, edits stay on disk |
git restore --source=commit file | take a file's version from a commit |
git reset --soft HEAD~1 | undo 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 commit | a new commit that undoes the given one (history isn't rewritten) |
git clean -n / -fd | show / delete untracked files and directories |
Stash and tags
| Command | What it does |
|---|---|
git stash / git stash push -m 'text' | stash uncommitted edits |
git stash -u | together with untracked files |
git stash list / show -p | list / contents of the latest |
git stash pop / apply / drop | restore and remove / restore and keep / remove |
git tag v1.0 / git tag -a v1.0 -m 'text' | lightweight / annotated tag |
git describe --tags | nearest tag for the current commit |
Remote repositories
| Command | What it does |
|---|---|
git remote -v / git remote add origin URL | list / add a remote repository |
git fetch | download new commits without changing your branches |
git pull | fetch + merge into the current branch (--rebase — via rebase) |
git push -u origin branch | push a branch and remember the link; afterwards just git push |
git push --tags | push 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 initin this version createsmasterunlessinit.defaultBranchis set. Checkgit branch --show-currentbefore writingmainin commands. user.name/user.emailnot set — git either refuses to commit or substitutes something likeroot@hostnameand prints a warning. That author stays in the history.git diffwithout flags doesn't see what's already staged, and doesn't show new untracked files. For the index —git diff --staged.git commit -adoesn't add new files, only changes to already tracked ones.git addnew files first.git reset --hard,git restore fileandgit clean -fare irreversible for uncommitted edits: they aren't in any commit. In doubt —git stashfirst. Committed work lost afterreset --hardcan be found viagit reflog..gitignoredoesn't affect already tracked files. Firstgit rm --cached file, then commit.git cleandoesn't touch ignored files — for those you also need-x. Always start with-n.git stashdoesn't touch untracked files by default. To stash them too, add-u.rebaseandcommit --amendchange commit hashes. Don't rewrite a branch others have already pulled; for published work userevert.git revertof a merge commit requires specifying the parent:git revert -m 1 commit.git branch -drefuses for an unmerged branch (not fully merged).-Ddeletes it along with commits that exist nowhere else.- On a conflict git stops and marks the disputed spots with
<<<<<<<,=======,>>>>>>>markers. Fix the files, rungit addand continue (--continue) or roll back (--abort). - The first
git pushof a new branch fails withhas no upstream branch— you needgit push -u origin branch.
See also
make · ssh · gitlab-ci · github-actions