Git cheatsheet

Doorzoekbare lijst met de handigste Git-commando’s voor setup, branches, remotes, wijzigingen ongedaan maken en geschiedenis bekijken.

Draait in je browser · er wordt niets verstuurd

55 commando’s

git initCreate a new, empty repository in the current folder.

Creates a hidden .git folder that stores the whole history. Run it once per project, then add files and make a first commit.

Voorbeeld

git init -b main

Opties

-b <name>Name of the first branch, for example main.
--bareRepository without working files, used on servers as a central remote.
git clone <url> [folder]Copy a remote repository, including its full history.

Downloads all commits and branches, checks out the default branch and sets the remote called origin.

Voorbeeld

git clone https://github.com/user/repo.git my-folder

Opties

--depth 1Shallow clone with only the latest commit. Much faster for big repositories and CI.
-b <branch>Check out this branch instead of the default one.
--recurse-submodulesAlso fetch submodules.
git config --global user.name "Your Name"Set the name that appears on your commits.

Stored in ~/.gitconfig. Leave out --global to set it only for the current repository, for example when you use a different identity for work.

Opties

--globalFor your whole user account.
--localOnly this repository (default when --global is missing).
--list --show-originShow all settings and which file they come from.
git config --global user.email "[email protected]"Set the email address on your commits.

Use the same address as your Git hosting account (GitHub, GitLab) so commits are linked to you.

git config --global init.defaultBranch mainUse main as the default name for new repositories.

Applies to new repositories created with git init.

git submodule update --init --recursiveFetch submodules after cloning a repository that uses them.

Submodules are other repositories pinned at a specific commit. A normal clone leaves their folders empty until you run this.

git statusShow which files are changed, staged or untracked.

Run it often. It also tells you which branch you are on and the next useful command.

Opties

-sShort output, one line per file.
-bShow branch and tracking info in short output.
git add <file>Stage a file so it is included in the next commit.

Staging lets you build a commit from selected changes instead of everything at once.

Voorbeeld

git add src/app.js README.md

Opties

.Stage everything in the current folder (new, changed and deleted).
-AStage all changes in the whole repository.
-uStage only files Git already tracks.
-pPick changes interactively.
git add -pStage changes interactively, piece by piece.

Git shows each changed hunk and asks what to do. Good for splitting unrelated changes into separate, clean commits.

Opties

y / nStage or skip this hunk.
sSplit the hunk into smaller pieces.
eEdit the hunk by hand.
qQuit.
git commit -m "message"Save the staged changes as a commit.

A commit is a snapshot with a message. Write the message in the imperative: “Fix login timeout”. Without -m an editor opens.

Voorbeeld

git commit -m "Add password strength checker"

Opties

-aAutomatically stage all modified tracked files first (new files are not included).
-vShow the diff in the message editor.
--no-verifySkip pre-commit hooks.
Let op: --no-verify skips checks such as linting and tests. Only use it when you know why.
git commit --amendChange the most recent commit (message or content).

Stage extra changes first to add them to the last commit, or run it alone to fix the message. It replaces the commit with a new one.

Voorbeeld

git commit --amend --no-edit

Opties

--no-editKeep the existing message.
-m "new"Replace the message.
Let op: This rewrites history. Do not amend a commit you already pushed to a shared branch.
git diffShow unstaged changes in your working files.

Compares your working directory with the staging area. Nothing shown means everything changed is already staged.

Voorbeeld

git diff main..feature -- src/

Opties

--statSummary of changed files and line counts.
--name-onlyOnly list file names.
<a>..<b>Compare two commits or branches.
-- <path>Limit to a file or folder.
git diff --stagedShow what is staged and will go into the next commit.

Review this before committing. Also written as --cached.

git rm <file>Delete a file and stage the deletion.

Opties

--cachedStop tracking the file but keep it on disk (useful after adding it to .gitignore).
-rRemove a folder recursively.
git mv <old> <new>Rename or move a file and stage the change.

Equivalent to moving the file yourself and running git add. Git detects renames automatically.

git log --oneline --graph --decorateCompact history with a branch graph.

One line per commit, with branch and tag names. The best quick overview of how branches relate.

Voorbeeld

git log --oneline --graph --all -n 20

Opties

--allInclude all branches, not just the current one.
-n <num>Limit the number of commits.
--author="name"Only commits by this author.
--since="2 weeks ago"Only recent commits.
git log -p <file>History of one file including the changes.

Opties

--followKeep following the file across renames.
--statShow changed-line counts instead of full diffs.
git show <commit>Show one commit with its message and changes.

HEAD is the current commit. HEAD~1 is its parent, HEAD~2 the commit before that.

Voorbeeld

git show HEAD~2
git blame <file>Show who last changed each line and in which commit.

Voorbeeld

git blame -L 10,25 src/app.js

Opties

-L <start>,<end>Only these lines.
-wIgnore whitespace changes.
git reflogHistory of where HEAD has been. Recovers “lost” commits.

After a bad reset or rebase, find the old commit here and return to it with git reset --hard <hash> or create a branch from it. Entries expire after about 90 days.

Voorbeeld

git branch rescue a1b2c3d
git grep "text"Search tracked files for text.

Faster than grep -r because it only looks at files Git tracks and ignores build output.

Opties

-nShow line numbers.
-iIgnore case.
-wMatch whole words.
git shortlog -snCommits per author, most active first.

A quick way to see who contributed how much. -s prints counts instead of commit messages and -n sorts by count.

Opties

-sSummary: only counts.
-nSort by number of commits.
-eInclude email addresses.
git bisect startBinary search for the commit that introduced a bug.

Mark a bad commit and a good one. Git checks out the middle; you test and answer good or bad until it finds the culprit.

Voorbeeld

git bisect start
git bisect bad
git bisect good v1.2.0
# test, then: git bisect good|bad
git bisect reset

Opties

bisect resetFinish and return to your original branch.
bisect run <script>Automate with a script that exits 0 for good and non-zero for bad.
git branchList local branches. The current one is marked with *.

Opties

-aInclude remote-tracking branches.
-vvShow the last commit and upstream of each branch.
--mergedOnly branches already merged into the current one.
git branch <name>Create a branch at the current commit without switching to it.

The new branch starts where you are now. Move to it with git switch <name>, or create and switch in one step with git switch -c <name>.

git switch <branch>Switch to another branch.

Updates your working files to that branch. Uncommitted changes that conflict will block the switch; commit or stash them first. Older guides use git checkout for this.

git switch -c <branch>Create a new branch and switch to it.

Voorbeeld

git switch -c feature/login
git branch -d <branch>Delete a branch that is already merged.

Opties

-DForce delete even if it is not merged.
Let op: -D throws away unmerged commits. You can still find them through git reflog for a while.
git branch -m <new-name>Rename the current branch.

If the branch was already pushed, push the new name and delete the old one on the remote.

git merge <branch>Merge another branch into the one you are on.

Creates a merge commit when histories have diverged. On conflicts, edit the marked files, git add them and run git commit.

Opties

--no-ffAlways create a merge commit, even when a fast-forward is possible.
--squashCombine all changes into one staged change without a merge commit.
--abortCancel a merge that hit conflicts.
git rebase <branch>Replay your commits on top of another branch for a linear history.

Typical use: git rebase main on your feature branch to pick up the latest changes. On conflicts fix the files, git add them and run git rebase --continue.

Opties

--continueContinue after resolving conflicts.
--abortCancel and return to how things were.
-iInteractive: reorder, squash or edit commits.
Let op: Rebase rewrites commits. Never rebase branches other people are using.
git rebase -i HEAD~3Clean up the last 3 commits: squash, reword, drop or reorder.

Opens an editor with one line per commit. Change “pick” to squash (s), reword (r), edit (e) or drop (d), save and close.

Let op: Only for commits that are not shared yet.
git cherry-pick <commit>Copy a single commit onto the current branch.

Useful for applying a hotfix to a release branch without merging everything else.

Opties

-nApply the changes but do not commit.
-xAdd a line with the original commit hash to the message.
--abortCancel after conflicts.
git worktree add ../hotfix hotfixCheck out another branch in a separate folder at the same time.

Lets you work on a hotfix without stashing or leaving your current branch. Remove it later with git worktree remove ../hotfix.

git remote -vList the remotes and their URLs.

Normally shows origin with a fetch and a push URL. Change a URL later with git remote set-url origin <url>.

git remote add origin <url>Connect your local repository to a remote.

Typical after git init: add origin, then git push -u origin main.

git fetchDownload remote changes without changing your files.

Safe to run any time. Afterwards compare with git log HEAD..origin/main or merge when ready.

Opties

--pruneRemove local references to branches deleted on the remote.
--allFetch from all remotes.
git pullFetch remote changes and merge them into your branch.

Opties

--rebaseRebase your commits on top instead of creating a merge commit.
--ff-onlyRefuse to pull if it would need a merge.
git pushUpload your commits to the remote branch.

Rejected? Someone pushed first. Pull (or fetch and rebase), then push again.

git push -u origin <branch>Push a new branch and remember it as the upstream.

After this, a plain git push and git pull work for that branch.

git push --force-with-leaseForce push, but only if nobody else pushed in the meantime.

Needed after rebasing or amending a commit that was already pushed to your own branch.

Let op: Plain --force can erase other people’s work. Prefer --force-with-lease and never force-push to shared main branches.
git push origin --delete <branch>Delete a branch on the remote.

Only removes the branch on the server. Delete your local copy with git branch -d <branch>. Teammates clean up stale references with git fetch --prune.

git restore <file>Discard unstaged changes in a file and return it to the last commit.
Let op: The changes are lost for good. Git cannot bring back uncommitted edits.
git restore --staged <file>Unstage a file but keep your edits.

Moves the file from the staging area back to your working files. Nothing is lost. It replaces the older git reset HEAD <file>.

git restore --source=<commit> <file>Bring back a file as it was in an older commit.

Voorbeeld

git restore --source=HEAD~3 src/app.js
git reset --soft HEAD~1Undo the last commit but keep the changes staged.

Handy when you committed too early or want to redo the message or split the commit.

git reset HEAD~1Undo the last commit and unstage the changes, keeping the files.

This is the default “mixed” reset. Your edits stay in the working directory.

git reset --hard HEAD~1Undo the last commit and delete its changes.
Let op: Destroys uncommitted work. If you reset too far, look in git reflog to get the commit back.
git revert <commit>Create a new commit that undoes an earlier commit.

Safe for shared branches because history is not rewritten. For a merge commit add -m 1 to pick the mainline parent.

Opties

-nApply the undo without committing.
-m 1Required when reverting a merge commit.
git clean -fdDelete untracked files and folders.

Always preview first with git clean -nd.

Opties

-nDry run: only show what would be removed.
-dInclude folders.
-xAlso remove ignored files.
-fRequired to actually delete.
Let op: Deleted files do not go to the recycle bin and cannot be recovered through Git.
git stashShelve your uncommitted changes so you can switch tasks.

Voorbeeld

git stash push -u -m "wip login form"

Opties

-uInclude untracked files.
-m "msg"Label the stash so you recognise it later.
git stash popRe-apply the latest stash and remove it from the list.

Use git stash apply to keep the stash after applying it.

git stash listList your stashes, newest first.

Each entry has an index such as stash@{0}, the branch and your label. Inspect one with git stash show -p stash@{0}, apply a specific one with git stash apply stash@{1}.

git tag -a v1.0.0 -m "Release 1.0.0"Mark a commit as a release with an annotated tag.

Annotated tags store author, date and message. A lightweight tag (git tag v1.0.0) is only a pointer.

git push --tagsUpload all local tags to the remote.

Tags are not pushed by a normal git push.

De omschrijvingen in dit naslagwerk zijn voorlopig in het Engels.

Uitleg

Zoek of filter op groep en kopieer een commando met één klik. Open een commando voor uitleg, een voorbeeld, de belangrijkste opties en waarschuwingen waar het schade kan aanrichten.

🔒 Je invoer blijft in je browser en wordt nergens naartoe gestuurd.

Veelgestelde vragen

Wat is het verschil tussen reset en revert?

Reset verplaatst de branch-aanwijzer en herschrijft geschiedenis. Revert voegt een nieuwe commit toe die een wijziging ongedaan maakt en is veilig op gedeelde branches.

Wat nu?

Meer in dit onderdeel

Ook handig

  • Tekst vergelijken (diff)Vergelijk twee teksten regel voor regel en zie toegevoegde en verwijderde regels. Handig voor configs en scripts.
  • RAID calculatorBereken bruikbare capaciteit, rendement en foutentolerantie voor RAID 0, 1, 5, 6 en 10 op basis van aantal en grootte van schijven.

Een probleem gevonden of een suggestie? Laat het ons weten