git
cheat sheet
Every everyday git command at a glance: setup, staging and commits, branches, remotes, merge and rebase, undo, stash, history, tags, worktrees, bisect and the new git history command.
- Version
- v2.56.1
- Commands
- 129
- Sections
- 16
Version notes · git 2.56.1
- 2.54–2.56 add the experimental
git history:reword,split,fixupanddropone commit without an interactive rebase. - 2.52 added
git repo infoandgit last-modified. 2.56 addsfetch.followRemoteHEAD. - Git 3.0 will default new repositories to
main. Setinit.defaultBranchnow to avoid surprises. - Git 3.0 will also switch new repositories to the reftable backend and SHA-256 by default.
Git records snapshots of your files as commits, lets you branch and merge cheaply, and syncs history with remotes such as GitHub or GitLab.
Local first
commit, branch, diff offline
Cheap branches
switch in milliseconds
Nothing is lost
reflog remembers 90 days
Integrity
every object is hashed
Quick start
git config --global user.name "Ada Lovelace"git config --global user.email [email protected]git init my-app && cd my-appecho "# my-app" > README.mdgit add README.md && git commit -m "Initial commit"git remote add origin [email protected]:ada/my-app.gitgit push -u origin main- 1git switch -cStart a branch
- 2git add -pStage changes
- 3git commitRecord them
- 4git rebase mainCatch up
- 5git pushShare & open a PR
- Your name on commits
- Your email
- Name of the first branch
git pullrebases instead of merging- First
git pushsets the upstream - Stash dirty files around a rebase
- Remember conflict resolutions
- Editor for messages
- Alias:
git lg - All settings and where they come from
- Edit
~/.gitconfig
- Turn this folder into a repo
- Clone over SSH
- Clone into a folder
- Latest commit only (fast CI)
- Full history, file contents on demand
- Including submodules
- Repository facts: format, hash, refs backend
- Short status + branch
- Unstaged changes
- What will be committed
- Changes on
featuresince it leftmain - Files changed + line counts
- Changed words, not lines
- Against 3 commits ago, one folder
- Open in your diff tool
- Stage a file
- Stage everything (new, changed, deleted)
- Pick hunks interactively
- Unstage (keep the change)
- Commit with a message
- Stage tracked files + commit
- Add staged changes to the last commit
- Reword the last commit
- Fix an older commit (squash with
--autosquash) - Stop tracking, keep the file
- Rename
- Go to a branch
- Create + switch
- Local branch from a remote one
- Back to the previous branch
- Branches with upstream + last commit
- Rename the current branch
- Delete (if merged)
- Delete anyway
- Branches already in
main - Look around at a tag / commit
- List remotes
- Add a remote
- Change its URL
- Download + drop deleted remote branches
- Fetch + replay your commits on top
- Push the current branch, set upstream
- Force push, unless someone else pushed
- Delete a remote branch
- Push tags
- Clean stale remote-tracking refs
Merge
- Merge into the current branch
- Always create a merge commit
- Stage all changes as one commit
- Give up a conflicted merge
- Copy one commit here
Rebase
- Replay this branch on
main - Edit, squash, reorder the last 3
- Apply
--fixupcommits - After fixing conflicts (
--abort,--skip) - Move stacked branches along
Conflicts
- Which files conflict
- Keep our side (
--theirsfor theirs) - Resolve in a merge tool
- Mark as resolved
- Commits that touch the conflict
Rebase only commits that nobody else has built on. On a shared branch, merge instead.
- Change a commit message
- Fold the staged changes into that commit (2.55+)
- Split paths out into their own commit
- Remove a commit (2.56+)
- Show what would change
- Only move the current branch
Experimental: behaviour may change. Unlike rebase it runs no hooks, touches no files in your worktree (except fixup) and updates every descendant branch by default.
- Discard unstaged changes to a file
- File as it was one commit ago
- Undo last commit, keep changes staged
- Undo last commit, keep changes unstaged
- Undo last commit and its changes
- Make the branch match the remote
- New commit that reverses an old one
- Everywhere HEAD has been: find "lost" commits
- Jump back to a reflog entry
- Preview deleting untracked files (
-fto do it)
reset --hard and clean -f delete uncommitted work for good. Committed work is still in git reflog.
- Shelve changes
- Include untracked files, with a name
- Stash one file
- All stashes
- What a stash contains
- Re-apply the latest and drop it
- Re-apply, keep it
- Delete one
- New branch from a stash
- Every branch as a graph
- Commits + diffs for one file
- Across renames
- Commits that added / removed a string
- Filter by author and date
- Commits in
feature, not inmain - One commit
- A file at a revision
- Who last touched lines 10–30
- Last commit per path (2.52+)
- Commits per author
- Second checkout on a new branch
- Check out a branch next to yours
- All worktrees
- Remove one
- Forget deleted folders
One repo, several folders, each on its own branch: review or hotfix without stashing.
- Begin
- Current commit is broken
- This one was fine
- Let a command decide (exit 0 = good)
- Done: back to where you started
- Only check out some folders
- Fetch submodules
- Background prefetch, gc, commit-graph
- Pack and clean up now
- Repository size
- Store big files with Git LFS (extension)
