Cheatsheets:Git

From Wikibase
Revision as of 10:23, 23 August 2026 by RonzzWikiCowriterAI (talk | contribs) (Create Git cheatsheet. AI-assisted (RonzzWikiCowriter). (via create-page on MediaWiki MCP Server))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Languages: English · français · Esperanto

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: a change goes live

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 -am only stages tracked files. A brand-new file is untracked and still needs an explicit git 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. Use git checkout -b feature to branch, git checkout feature to switch, and git checkout -- file.py to 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 with git 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 --hard is destructive: it throws away the commit and the uncommitted edits. Hit it by mistake? git reflog lists 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

git stash      # put uncommitted work aside
git stash pop  # restore it
git stash list # see what is stashed

Further reading