HowToGit
This git cheatsheet assumes you have at least some familiarity with git, but want to look up the commands to quickly perform actions on a repository. A square bracket within a command means that the brackets as well as the name in it needs to be replaced with the actual value.
Basics
Setup a new repository (project) and work on it
git init
: Start a new repositorygit add .
: Stage all changes (prepare for a commit)git commit -m [MESSAGE]
: Commit the staged changes with a messagegit push
: Upload local commits to origingit pull
: Download local commits from origin
Get a remote repository
git clone [URL]
: Copy a project onto your local computergit clone [URL] -b [BRANCH]
: Clone from (and switch to) specified branch
Show information about the repository
git status
: Check your current repository stategit log --pretty=oneline
: Show a graph of commit history
Changes, Search & Gitignore
Exclude files by negative wildcard from diff (example: exclude pdfs)
git diff ':!*.pdf'
Show diff of staged files
git diff --staged
Show diff against changes since last commit
git diff HEAD^ [FILENAME]
Show diff for one file
https://stackoverflow.com/questions/5586383/how-to-diff-one-file-to-an-arbitrary-version-in-git
Search for commit in all branches where a search term was added or removed in its files
https://stackoverflow.com/a/5816177
Oldest commits first (that introduced the term): --reverse
git log --all --source -S "[SEARCHTERM]"
Search for commit containing any change to the search term in its files
Show with diff: -p
git log -G"[SEARCHTERM]" -- [PATH]
Search for lines in commits containing the search term in its files under a specified path
git grep "[SEARCHTERM]" $(git rev-list --all -- [PATH]) -- [PATH]
See the last changes to the Head pointer (helps to recover a deleted branch or commit)
git reflog
Forget commited files now matched by .gitignore
Global or repo-specific .gitignore just for the current user
https://stackoverflow.com/a/5724484
- Global:
git config --global core.excludesfile $HOME/.gitignore
- Repo-specific: Edit
.git/info/exclude
Get all modified files in branch
https://stackoverflow.com/questions/10641361/get-all-files-that-have-been-modified-in-git-branch
Reset changes to a file since last commit
git restore [FILE]
Remove the changes to a file in the last commit
https://stackoverflow.com/a/44074005
git reset HEAD^ [FILE]
git commit --amend
Reset all local changes since last commit
https://docs.gitlab.com/ee/topics/git/numerous_undo_possibilities_in_git/
git reset -–hard
Stage a file for removal without removing it from the working directory
git rm --cached [FILE]
Patches
Create a patch with changes of tracked files
git diff > changes.patch
Create a patch with all staged changes
git diff --staged > changes.patch
Apply patch with changes
git apply --reject --whitespace=fix changes.patch
Undo apply patch
git apply changes.patch --reverse
Stashes
Stash only one file
git stash push -p
Unstash only some files
https://stackoverflow.com/questions/15264553/how-to-unstash-only-certain-files
Show changes in stash by index number
https://opensource.com/article/21/4/git-stash
git stash show stash@{[INDEX]} -p
Stash multiple files
git stash -- [FILE1] [FILE2]
Undo stash pop with conflict
git reset HEAD .
git checkout -f
Stash only unstaged changes
git stash --keep-index
Stash only unstaged changes (including untracked files)
https://stackoverflow.com/questions/20028507/git-stash-uncached-how-to-put-away-all-unstaged-changes
git stash --keep-index -u
Stash only staged changes
https://stackoverflow.com/questions/14759748/how-can-i-stash-only-staged-changes-in-git
- For git version >= 2.35:
git stash push --staged
- For git version < 2.35:
git stash -- $(git diff --staged --name-only)
Remove last stash
git stash drop
Apply and remove last created stash
git stash pop
Apply single stash by index number
git stash apply [INDEX]
git stash drop stash@{[INDEX]}
Stash single file with message
https://stackoverflow.com/questions/12420924/stash-just-a-single-file
git stash push -m "[MESSAGE]" [FILE]
Save stash by index number as a patch
https://stackoverflow.com/questions/3973034/export-a-stash-to-another-computer
git stash show stash@{[INDEX]} -p > changes.patch
Commits
Remove last commit locally and remotely
- Remove commit locally:
git reset HEAD^
- Force-push the new HEAD commit:
git push origin +HEAD
See changes in a commit
https://stackoverflow.com/questions/17563726/how-can-i-see-the-changes-in-a-git-commit
Amend to last commit
https://www.atlassian.com/de/git/tutorials/rewriting-history
Change last commit message
git commit --amend
Change message of older commit
- Show last n commits (replace n with number):
git rebase -i HEAD~n
- Replace “pick” with “reword” before respective commit.
- Save files and edit the commit file that opens and save it as well.
Undo commit and keep changes
https://stackoverflow.com/questions/15772134/can-i-delete-a-git-commit-but-keep-the-changes
git reset HEAD^
Undo last commit without keeping changes
git reset --soft HEAD~1
Undo changes of an older commit (by creating a new commit that has the opposite effect)
git revert [OLDCOMMITID]
Add changes to older commit
https://stackoverflow.com/a/27721031
git add FIXEDFILES
git commit --fixup=[OLDCOMMITID]
git rebase -i --autosquash [OLDCOMMITID]^
Remove changes to file from older commit
https://stackoverflow.com/a/32528151
git rebase -i [OLDCOMMITID]^1
- Mark the problem commit for edit in the editor by changing
pick
toe
. git show -- [BADFILE] | git apply -R
git add [BADFILE]
git commit --amend
git rebase --continue
Remove older commit
git rebase -i HEAD~[N]
to list last N commits- Remove the line of the commit that shall be deleted
Remove initial commit if there are no other commits, but keep changes staged
git update-ref -d HEAD
Add single commit from other branch (cherry-pick)
https://stackoverflow.com/questions/9339429/what-does-cherry-picking-a-commit-with-git-mean
git cherry-pick [COMMITID]
Move last N commits to new (other) branch
https://stackoverflow.com/questions/1628563/move-the-most-recent-commits-to-a-new-branch-with-git
git branch [NEWBRANCH]
git checkout [OLDBRANCH]
git reset --hard HEAD~[N]
git checkout [NEWBRANCH]
Sign commits with ssh key
https://git.openlogisticsfoundation.org/help/user/project/repository/signed_commits/ssh.md
git config --global gpg.format ssh
git config --global commit.gpgsign true
git config --global user.signingkey ~/.ssh/[KEYFILE].pub
Tags
List all tags
git tag
Add tag for the current commit
https://stackoverflow.com/questions/18216991/create-a-tag-in-a-github-repository
git tag [TAGNAME]
- To push all tags:
git push origin --tags
Add tag for a commit
git tag [TAGNAME] [COMMITID]
- To push all tags:
git push origin --tags
Remove tag locally
git tag --delete [TAGNAME]
Remove tag remotely
https://stackoverflow.com/questions/5480258/how-can-i-delete-a-remote-tag
git push origin :refs/tags/[TAGNAME]
Branches (Creating, Merging & Co.)
Create branch and switch to it
git checkout -b [BRANCH]
Create branch from a previous commit
https://stackoverflow.com/questions/2816715/branch-from-a-previous-commit-using-git
git checkout -b [BRANCH] [COMMITID]
Switch to an existing branch
git checkout [BRANCH]
Switch to last active branch (from detached head)
https://stackoverflow.com/questions/11801071/git-how-to-return-from-detached-head-state
git checkout -
Switch to remote branch
https://www.git-tower.com/learn/git/faq/checkout-remote-branch
git checkout --track [REMOTENAME]/[BRANCHNAME]
List all local branches
git branch
List all remote branches
git branch -a
Merge branch into current branch (add commits from other branch)
git merge [BRANCH]
Merge multiple commits as one onto another branch
git checkout master
git merge --squash [BRANCH]
git commit
Add new commits from current branch to other remote branch
https://stackoverflow.com/a/51909592
git push origin head:[BRANCH]
Remove branch locally
https://www.git-tower.com/learn/git/faq/delete-local-branch
- Safe removal (checks if merged etc.):
git branch -d [BRANCH]
- Force removal:
git branch -D [BRANCH]
Remove branch remotely
https://www.git-tower.com/learn/git/faq/delete-remote-branch
git push -d origin [BRANCH]
Remove all commits from branch on repository
- Create empty folder
git init
git commit --allow-empty -m "Initial commit"
git remote add origin [URL]
git branch --set-upstream [BRANCH] origin/[BRANCH]
git push -f
Fix origin of current branch (after rename)
https://stackoverflow.com/a/48100062
Squash all commits on branch
https://stackoverflow.com/questions/25356810/git-how-to-squash-all-commits-on-branch
Squash and rebase
https://www.jenweber.dev/how-to-squash-and-rebase/
Put last commit on new branch
https://stackoverflow.com/questions/1628563/move-the-most-recent-commits-to-a-new-branch-with-git
Rename branch (that you are currently working on)
https://stackoverflow.com/questions/6591213/how-do-i-rename-a-local-git-branch
git branch -m [NEWNAME]
Change remote for one branch (e.g. for branches on forks)
https://stackoverflow.com/questions/4878249/how-to-change-the-remote-a-branch-is-tracking
git fetch [REMOTENAME]
git branch [BRANCHNAME] --set-upstream-to [REMOTENAME]/[BRANCHNAME]
Merge and favor changes of own or other branch
https://stackoverflow.com/a/33569970
Rename master to main in new repo
Rebasing
Rebase branch onto other branch
git rebase [OTHERBRANCH]
git push --force-with-lease
Rebase branch onto (previous) commit of other branch
git rebase --onto [COMMITID] [OTHERBRANCH]
git push --force-with-lease
Interactive rebasing
git rebase -i
Interactive rebasing from first commit on (with all commits)
git rebase -i --root
Change base of branch
https://stackoverflow.com/a/10853956
Abort rebase:
git rebase --abort
Rebase with merge conflict handling
https://demisx.github.io/git/rebase/2015/07/02/git-rebase-keep-my-branch-changes.html
Rebase without changing commit dates when there are merge conflicts
https://stackoverflow.com/a/2976598
Remote (Origin / Upstream)
Pull and use incoming changes in case of conflicts
git pull -X theirs
Use incoming changes in case of conflicts in a file
git checkout --theirs [FILE]
Fetch remote branch
git fetch origin [BRANCH]
Fetch all branches
git fetch --all
Change remote for all branches and push everything
https://stackoverflow.com/questions/18801147/changing-the-git-remote-push-to-default
git push -u [REMOTENAME] --all
Change remote url
https://stackoverflow.com/questions/2432764/how-do-i-change-the-uri-url-for-a-remote-git-repository
Force pull (hard reset to origin, get clean state) when origin commits were overwritten/rebased
https://stackoverflow.com/questions/1125968/how-do-i-force-git-pull-to-overwrite-local-files
git reset --hard origin/[BRANCH]
Pull changes of another branch (without checkout)
https://stackoverflow.com/questions/18857570/git-pull-without-checkout
git fetch origin [BRANCH]:[BRANCH]
Forks
Working on forks
https://docs.github.com/en/get-started/quickstart/fork-a-repo
https://www.atlassian.com/git/tutorials/comparing-workflows/forking-workflow
Change remote to fork
https://gist.github.com/jagregory/710671
git remote -v
git remote rename origin upstream
git remote add origin [FORKURL]
git fetch origin
Checkout a branch from a fork
https://stackoverflow.com/questions/1783405/how-do-i-check-out-a-remote-git-branch
git remote add [REMOTENAME] [REMOTEURL]
git fetch [REMOTENAME]
git checkout [BRANCHNAME]
Submodules
Add another repository as a submodule
https://www.atlassian.com/git/tutorials/git-submodule
git submodule add [URL]
Initialize submodules (also when new ones were added to the repo)
https://openmetric.org/til/programming/git-pull-with-submodule/
git submodule update --init --recursive
Specify branch for a submodule
Update submodules (and top repository)
git pull --recurse-submodules
See checked out commit id for each submodule
git submodule status
Revert changes to all submodules
https://stackoverflow.com/questions/10906554/how-do-i-revert-my-changes-to-a-git-submodule
git submodule foreach --recursive git reset --hard
Config
Initial config
git config --global init.defaultBranch main
git config --global user.email "[EMAIL]"
git config --global user.name "[USERNAME]"
Show full config
git config --list
Change author in previous commits
https://www.git-tower.com/learn/git/faq/change-author-name-email
git rebase -i
(Interactive rebasing)- Mark commits to change with
edit
- For each commit:
git commit --amend --author="[USERNAME] <[EMAIL]>" --no-edit
git rebase --continue
Multiple author identities (different identity per repo)
https://garrit.xyz/posts/2023-10-13-organizing-multiple-git-identities
Useful defaults
https://spin.atomicobject.com/2020/05/05/git-configurations-default/ https://jvns.ca/blog/2024/02/16/popular-git-config-options/
git config --global pull.rebase true
git config --global diff.colorMoved zebra
git config --global rebase.autosquash true
Git LFS
- Install:
- Install PackageCloud: https://git-lfs.github.com/
sudo apt install git-lfs
git lfs install
- Download LFS files in repo:
git lfs pull
autoSetupRemote
https://twitter.com/SantoshYadavDev/status/1558086948484530177
More
https://www.cheatsheet.wtf/Git/
https://developers.redhat.com/articles/2023/08/02/beginners-guide-git-version-control
GitFlow Workflow: https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow
Git Tools / Helpers
VS Code Compare Changes to main
Exit default git editor in the terminal
[esc], :x!
, [enter]
License note
Any content coming from Stack Exchange (recognizable by a link leading to a Stack Exchange site like Stack Overflow between the current heading and the content) is licensed under CC BY-SA 4.0.