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:
git 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:
git checkout BRANCH
git reset --hard REMOTE-ID/BRANCH
Undo a commit:
git reset --hard HEAD^
Undo last local commit and move the changes to index:
git reset --soft HEAD^
Remove staged and working directory changes:
git reset --hard
Change the commit message:
git commit --amend -m "New commit message"
Remote Servers
List Remote Servers:
git remote -v
See more information about a particular remote:
git remote show [REMOTE-ID]
git remote show origin
Add Remote:
git 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:
git remote remove REMOTE-ID
# ex:
git remote remove upstream
Rename Remote Id:
git remote rename REOMOTE-ID NEW-REMOTE-ID
# ex:
git remote rename origin old-origin
Change remote URL:
git remote set-ur REMOTE-ID REMOTE-URL
# ex:
git remote set-url upstream git@foobar.com:project/name.git
Multiple Remotes (push to all):
# 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:
git branch -v -a
Create a remote branch:
# 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:
git checkout master
git merge src-branch
git push
Merge 2 branches:
git checkout target-branch
git merge src-branch
git push
Keep a branch in sync with master:
# 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:
git show-branch --more=10 --sha1-name <branch-name>
Log
Compare Branches / Filter by Range:
# 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:
git log --graph
git log --graph --decorate --oneline
Reverse the order of a git log output:
git log --reverse
Filter git log by date:
git 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):
git 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:
# 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):
git log -S"Keyword"
git log -G"Reg(ex)Keyword"
List all commits that changed a specific file:
git log --follow -- <file-name>
Stash
# 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:
# git format-patch <branch> <options>
git format-patch master --stdout > patch_file.patch
Apply patch:
#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:
git checkout master # OR branch-name
git fetch
git log ..origin/master #see the new commits
git rebase origin/master
Squash Merge:
git merge --squash branch_name
git commit -m "comment"
git push
Add a backup remote:
# 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:
git show --name-only <commit-id>
Archive git project (excludes files in .gitignore)
git archive --format=zip HEAD -o zipfile.zip
Config & Settings
Set username and email:
git config --global user.name "username"
git config --global user.email "usermail@example.com"
DON'T DO THIS
Disable SSL Verify
export 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:
# Timeout in seconds
git config --global credential.helper 'cache --timeout=3600'