Show Menu
Cheatography

Git cheat sheet with the most useful commandss

States

Diff

git diff
WORKSPACE vs STAGED
git diff --staged
(or
--cached
)
STAGED vs COMMITED
git diff HEAD
WORKSPACE + STAGED vs COMMITED
git diff <SH­A-1>
WORKSPACE vs any commit (more general case of the above)
git diff master...t­opic
Changes introduced if topic gets merged into master
-w
IGNORE whitespace

Removal

git rm README
Remove file from git and locally
git rm --cached README
Remove file from git, but keep locally as UNTRACKED
git reset HEAD README
UNSTAGE a file
git checkout -- README
Revert a file back to its form from the last commit

Stashing

git stash
Push all TRACKED modified files (
-u 
to include untracked,
-a
to include ignored)
git stash list
LIST all stashes
git stash apply
APPLY the last stash
git stash apply stash@{2}
APPLY a chosen stash
git stash pop
APPLY stash and DELETE it
git stash drop
DELETE stash

Remotes

git remote -v
LIST remotes with URLs
git remote show <or­igi­n>
GET details with tracked branches
git remote add <or­igi­n> <ur­l>
ADD a remote
git remote rename <or­igi­n> <up­str­eam>
RENAME remote origin to upstream
git remote rm <or­igi­n>
DELETE remote
git push <or­igi­n> --delete <fe­atu­re/­one>
DELETE branch from remote
git fetch origin
FETCH all data from origin
git pull
FETCH and MERGE
git push <or­igi­n> <ma­ste­r>
PUSH branch to a remote
git branch -vv
Details of local branches and what they track

Tagging

git tag
LIST tags
`-l "­119.*"
FILTER tags
git tag -a v2.0 -m "­Version 2.0"
CREATE annotated tag
git tag v2.0
CREATE lightw­eight tag
git show v2.0
GET tag
git tag -d v2.0
DELETE tag
git push <or­igi­n> <ta­g>
PUSH single tag
git push <or­igi­n> --tags
PUSH all tags
 

Rebasing

git rebase master
Takes commits from current branch and applies them on top of master. Master stays on previous commit, only current branch is moved

Git Log

-p
See differ­ences in each commit
-2
Only 2 last commits
master featur­e/one
Only commits between master and featur­e/one
--all
Commits from all branches
--stat
See file change statistics
--pret­ty=­one­lin­e/s­hor­t/f­ull­/fu­lle­r/f­orm­at:­{some format}
Formatting of output
--oneline
Shorthand for
--pret­ty=­oneline
--graph
Shows graph
--sinc­e=2.we­eks­/20­10-­01-15
(or
after
)
Commits from
--unti­l=2021
(or
before
)
Commits to
--auth­or=Joe
Only by some author, can be used multiple times
--grep­=word
Filter by message content
-S word
Filter by commit that changed occurence of
word
in code
Example: `git log --pret­ty=­"%h - %s" --auth­or=­'Junio C Hamano' --sinc­e="2­008­-10­-01­" \
--befo­re=­"­200­8-1­1-0­1" --no-m­erges`

--pre­tty­=fo­rmat options

Config­uration

Git config­uration is stored in 3 places:
-
/etc/g­itc­onfig
(
--system
)
-
~/.git­config
(
--global
)
-
.git/c­onfig
(
--local
)
Each level overwrites the previous one.

Setting up:
git config --global user.name "John Doe"

git config --global user.email johndo­e@e­xam­ple.com

git config --global core.e­ditor code


Check some setting:
git config user.name


See all config­uration and sources:
git config --list --show­-origin
           
 

Comments

Interesting cheat sheet. Can I suggest improving readability by dropping from 3 columns to 2?
Thanks for making it!

Loreno10 Loreno10, 17:33 13 Dec 20

Good idea, thanks

DarrinC DarrinC, 19:30 13 Dec 20

Format looks perfect now THANKS!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Git Cheat Sheet
          Git Flow Cheat Sheet