Git Cheatsheet - Useful Git Commands
Useful:
- https://ohshitgit.com/
- https://stackoverflow.com/questions/2221658/whats-the-difference-between-head-and-head-in-git
Diff
Compare a file between branches:
shgit diff branch1 branch2 -- <filename> # or git diff <hash1> <hash2> -- <filename> # or - see the differences between the current version of a file and a historical one git diff <hash> -- <filename>
Reset / Undo
Reset a branch to match the state as on a specific remote:
shgit checkout BRANCH git reset --hard REMOTE-ID/BRANCH
Undo a commit:
shgit reset --hard HEAD^
Undo last local commit and move the changes to index:
shgit reset --soft HEAD^
Remove staged and working directory changes:
shgit reset --hard
Change the commit message:
shgit commit --amend -m "New commit message"
Remote Servers
List Remote Servers:
shgit remote -v
See more information about a particular remote:
shgit remote show [REMOTE-ID] git remote show origin
Add Remote:
shgit remote add REMOTE-ID REMOTE-URL # examples git remote add origin git@github.com:project/name.git git remote add backup git@github.com:project/name.git
Remove Remote:
shgit remote remove REMOTE-ID # ex: git remote remove upstream
Rename Remote Id:
shgit remote rename REOMOTE-ID NEW-REMOTE-ID # ex: git remote rename origin old-origin
Change remote URL:
shgit remote set-ur REMOTE-ID REMOTE-URL # ex: git remote set-url upstream git@foobar.com:project/name.git
Multiple Remotes (push to all):
sh# setup git remote add REMOTE-ID URL1 git remote set-url --add --push REMOTE-ID URL1 git remote set-url --add --push REMOTE-ID URL2 # push git push REMOTE-ID
Pull from multiple remotes:
git fetch --all
Branches
List all branches:
shgit branch -v -a
Create a remote branch:
sh# create a local branch: git checkout -b your-new-branch # push the local branch to the (remote) repository git push --set-upstream origin your-new-branch
Merge a Branch to Master:
shgit checkout master git merge src-branch git push
Merge 2 branches:
shgit checkout target-branch git merge src-branch git push
Keep a branch in sync with master:
sh# merge master with branch git checkout master git pull git checkout branch git merge master git push # alternative, get a specific commit git checkout branch git cherry-pick bugfix
Show last commits on branch:
shgit show-branch --more=10 --sha1-name <branch-name>
Log
Compare Branches / Filter by Range:
sh# The range main..branch-name contains all of the commits that are in the <branch-name> branch, # but aren’t in the main branch. git log --oneline main..branch-name # The range contains all of the commits between 5bc24f2 and c77c710 commits git log <since>..<until> git log 5bc24f2..c77c710
Visualize Git Log as a graph:
shgit log --graph git log --graph --decorate --oneline
Reverse the order of a git log output:
shgit log --reverse
Filter git log by date:
shgit log --after="yyyy-mm-dd" --before=="yyyy-mm-dd" git log --before=="yesterday" git log --since="2 weeks ago"
Filter by a specific author (or email):
shgit log --author=Name git log --author="Alice\|Bob" # show last 5 commits by author: git log --oneline --decorate -n 5 --author=Name
Filter by Regex:
sh# shows only commits with messages that match the given pattern git log --grep="Bug" # shows only commits that do not match the given pattern git log --grep="Bug" --invert-grep
Filter by content (if you want to know when "Keyword" was added to any file in the project):
shgit log -S"Keyword" git log -G"Reg(ex)Keyword"
List all commits that changed a specific file:
shgit log --follow -- <file-name>
Stash
sh# Push a new stash on the stack git stash # or git stash push # add a message git stash push -m|--message <message> # to stash a single file: git stash push <path> # List the stash entries that you currently have: git stash list # Apply the top stash git stash pop # Apply a specific stash, and REMOVE it from the stash git stash pop stash@{2} # or (does not remove it) git stash apply stash@{2} # clean stash - remove all the stash entries git stash clear # remove a specific stash from the stack git stash drop stash@{2}
Patches
Create patch:
sh# git format-patch <branch> <options> git format-patch master --stdout > patch_file.patch
Apply patch:
sh#shows what's modified git apply --stat patch_file.patch #shows possible conflicts git apply --check patch_file.patch #apply patch git apply patch_file.patch
Workflows
Fetch + Rebase:
shgit checkout master # OR branch-name git fetch git log ..origin/master #see the new commits git rebase origin/master
Squash Merge:
shgit merge --squash branch_name git commit -m "comment" git push
Add a backup remote:
sh# setup, once git remote add backup {{gitlab_url}} git remote set-url --add --push backup {{gitlab_url}} git remote set-url --add --push backup {{github_url}} # push to backup (both remotes are updated) git push backup
Utils
Show files modified in commit:
shgit show --name-only <commit-id>
Archive git project (excludes files in .gitignore)
shgit archive --format=zip HEAD -o zipfile.zip
Config & Settings
Set username and email:
shgit config --global user.name "username" git config --global user.email "usermail@example.com"
DON'T DO THIS
Disable SSL Verify
shexport GIT_SSL_NO_VERIFY=1 # OR # http.sslVerify=false git -c http.sslVerify=false push
Credentials Helper
Avoid entering your username and password every time you push or pull from the Git repo.
Store your credentials for an hour in the memory:
sh# Timeout in seconds git config --global credential.helper 'cache --timeout=3600'