Cheatsheets:Git: Difference between revisions
Create Git cheatsheet. AI-assisted (RonzzWikiCowriter). (via create-page on MediaWiki MCP Server) |
|||
| (One intermediate revision by one other user not shown) | |||
| Line 5: | Line 5: | ||
New to Git? [https://learngitbranching.js.org/ Learn Git Branching] is a visual, interactive tutorial; [https://git-scm.com/book/en/v2 Pro Git] is the free, thorough book. | New to Git? [https://learngitbranching.js.org/ Learn Git Branching] is a visual, interactive tutorial; [https://git-scm.com/book/en/v2 Pro Git] is the free, thorough book. | ||
== Basic example: | == Basic example: clone, edit, commit, push == | ||
<syntaxhighlight lang="bash" line highlight="5,6,7" copy> | <syntaxhighlight lang="bash" line highlight="5,6,7" copy> | ||
| Line 161: | Line 161: | ||
'''Amending and rebasing rewrite history''' (new commit hashes). Use them only on commits that are not yet shared. Rewriting a pushed branch needs <syntaxhighlight lang="bash" inline>git push --force-with-lease</syntaxhighlight> — never plain <syntaxhighlight lang="bash" inline>--force</syntaxhighlight> — and disrupts everyone else working on it. | '''Amending and rebasing rewrite history''' (new commit hashes). Use them only on commits that are not yet shared. Rewriting a pushed branch needs <syntaxhighlight lang="bash" inline>git push --force-with-lease</syntaxhighlight> — never plain <syntaxhighlight lang="bash" inline>--force</syntaxhighlight> — and disrupts everyone else working on it. | ||
</blockquote> | </blockquote> | ||
== Stash == | == Stash == | ||
Put uncommitted work aside to switch branches, then bring it back. Stashes are not tied to a branch — a stack you can pop anywhere. | |||
<syntaxhighlight lang="bash" copy> | |||
git stash # stash tracked changes | |||
git stash -u # also stash untracked files | |||
git stash -m "WIP" # stash with a message | |||
git stash list # see what is stashed | |||
git stash show -p # inspect the newest stash's diff | |||
</syntaxhighlight> | |||
'''pop vs apply''' — pop restores and drops the stash; apply restores and keeps it: | |||
<syntaxhighlight lang="bash" copy> | <syntaxhighlight lang="bash" copy> | ||
git stash | git stash pop # restore newest stash, then drop it | ||
git stash | git stash apply # restore, keep the stash | ||
git stash | git stash apply stash@{1} # restore a specific stash | ||
git stash drop stash@{1} # delete a specific stash | |||
git stash clear # delete all stashes | |||
git stash branch fix # new branch at the stash point, pop onto it | |||
</syntaxhighlight> | </syntaxhighlight> | ||
<blockquote> | |||
'''Untracked files are left behind by default.''' <syntaxhighlight lang="bash" inline>git stash</syntaxhighlight> only stashes ''tracked'' changes — a brand-new file survives in the working tree. Add <syntaxhighlight lang="bash" inline>-u</syntaxhighlight> for untracked files too, or <syntaxhighlight lang="bash" inline>-a</syntaxhighlight> to also include ignored files. | |||
</blockquote> | |||
<blockquote> | |||
'''A conflicting pop keeps the stash.''' Restoring onto a branch that changed the same lines leaves the stash in place: resolve like a merge conflict, then <syntaxhighlight lang="bash" inline>git stash drop</syntaxhighlight> once the work is safely committed. | |||
</blockquote> | |||
== Further reading == | == Further reading == | ||
Latest revision as of 14:30, 26 August 2026
Quick reference for smart people — part of our dev cheatsheets collection.
Git is a distributed version control system that tracks changes to files and coordinates work between people on the same codebase.
New to Git? Learn Git Branching is a visual, interactive tutorial; Pro Git is the free, thorough book.
Basic example: clone, edit, commit, push
git clone https://github.com/user/repo.git
cd repo
git switch -c my-feature # branch off main
# ... edit files ...
git add . # stage all changes
git commit -m "Add my feature" # snapshot them
git push -u origin my-feature # share on the remote, remember upstream
Every change follows the same loop: edit → stage (git add) → commit → push. The working tree is what you edit, the index is what you stage, the remote is where you share.
Setup
git config --global user.name "Ada Lovelace"
git config --global user.email ada@example.com
git config --global init.defaultBranch main
git config --global alias.co checkout # `git co` works from now on
git config --global pull.rebase true # pull = fetch + rebase
| Command | Effect |
|---|---|
git config --global user.name "Ada" |
who you are, for every repo — needed before the first commit |
git config --global init.defaultBranch main |
new repos default to main |
git config --global alias.co checkout |
create a short alias |
git config --global --list |
show all config |
Start a repository:
git init # new repo in the current directory
git init my-project # ... in a subdirectory
git clone <url> # copy an existing repository
git clone <url> dir # ... into a given directory
Daily workflow
git status # what changed?
git diff # unstaged changes
git diff --staged # staged changes
git add file.py # stage one file
git add . # stage all changes
git add -p # stage hunks interactively
git commit -m "Fix typo" # commit the staged changes
git commit -am "Fix typo" # stage tracked changes + commit in one go
git commit -amonly stages tracked files. A brand-new file is untracked and still needs an explicitgit add.
Inspecting history
git log --oneline # one line per commit
git log --oneline --graph --all # branch topology
git log -p # full diffs
git log --follow -- file.py # history of a single file
git show <hash> # one commit in detail
git blame file.py # who changed each line
Branching & merging
git branch # list local branches
git branch -a # include remote-tracking branches
git switch -c feature # create a branch and switch to it
git switch feature # switch to an existing branch
git switch - # switch back to the previous branch
git merge feature # merge feature into the current branch
git branch -d feature # delete a merged branch
git branch -D feature # force-delete an unmerged branch
Git before 2.23 has no
git switch/git restore. Usegit checkout -b featureto branch,git checkout featureto switch, andgit checkout -- file.pyto discard changes.
Rebase
git rebase main # replay current branch on top of main
git rebase -i HEAD~3 # interactive: squash, reword, reorder commits
Merge conflicts
# git status shows "both modified"; files contain markers:
# <<<<<<< HEAD ... ======= ... >>>>>>> feature
# edit the file to keep the right lines, then:
git add <resolved files>
git commit # after a merge
git rebase --continue # after a rebase
git merge --abort # back out of the merge entirely
git rebase --abort # ... or of the rebase
Remotes
git remote -v # list remotes
git remote add origin <url> # connect a remote
git fetch origin # download commits, don't touch your work
git pull # fetch + merge
git push # upload your commits
git push -u origin feature # first push of a branch: set upstream
git push origin --delete feature # delete a branch on the remote
git tag v1.0 # tag the current commit
git push origin v1.0 # share the tag
pull is fetch + merge. It can create merge commits. Prefer a linear history with
git pull --rebase, or set it once withgit config --global pull.rebase true.
Undoing
| Want to… | Command |
|---|---|
| discard unstaged changes to a file | git restore file.py
|
| unstage a file | git restore --staged file.py
|
| fix the last commit (message / forgotten file) | git commit --amend
|
| undo the last commit, keep changes staged | git reset --soft HEAD~1
|
| undo the last commit, keep the edits | git reset HEAD~1
|
| undo the last commit and drop everything | git reset --hard HEAD~1
|
| undo a commit that is already pushed | git revert <hash>
|
git reset --hardis destructive: it throws away the commit and the uncommitted edits. Hit it by mistake?git refloglists everywhere HEAD has been —git reset --hard "HEAD@{1}"takes you back.
Amending and rebasing rewrite history (new commit hashes). Use them only on commits that are not yet shared. Rewriting a pushed branch needs
git push --force-with-lease— never plain--force— and disrupts everyone else working on it.
Stash
Put uncommitted work aside to switch branches, then bring it back. Stashes are not tied to a branch — a stack you can pop anywhere.
git stash # stash tracked changes
git stash -u # also stash untracked files
git stash -m "WIP" # stash with a message
git stash list # see what is stashed
git stash show -p # inspect the newest stash's diff
pop vs apply — pop restores and drops the stash; apply restores and keeps it:
git stash pop # restore newest stash, then drop it
git stash apply # restore, keep the stash
git stash apply stash@{1} # restore a specific stash
git stash drop stash@{1} # delete a specific stash
git stash clear # delete all stashes
git stash branch fix # new branch at the stash point, pop onto it
Untracked files are left behind by default.
git stashonly stashes tracked changes — a brand-new file survives in the working tree. Add-ufor untracked files too, or-ato also include ignored files.
A conflicting pop keeps the stash. Restoring onto a branch that changed the same lines leaves the stash in place: resolve like a merge conflict, then
git stash droponce the work is safely committed.
Further reading
- Git documentation — the official docs
- Learn Git Branching — interactive, visual tutorial — the recommended start
- Pro Git — the free book
- Oh Shit, Git!?! — quick fixes for common mistakes