Git Cheatsheet
?
R
BashGit cheat sheet updated regularly. Common and trivial use cases are covered. Example of a proper GIT commit message: "Add new md file to the blog"
1# Git branch naming conventions
2feature/* prefix - introduce new features or functionality.
3fix/* or bugfix/* addressing a bug or an issue
4
5# List all commits from a user within a time period
6git log --pretty=format:"%ad - %an: %s" --after="2021-11-01" --until="2021-11-30" | grep André | grep -v Merge
7
8# Count amount of lines changed
9git log --after="2022-01-01" --until="2022-01-31" --shortstat --pretty="%cE" | sed 's/\(.*\)@.*/\1/' | grep -v "^$" | awk 'BEGIN { line=""; } !/^ / { if (line=="" || !match(line, $0)) {line = $0 "," line }} /^ / { print line " # " $0; line=""}' | sort | sed -E 's/# //;s/ files? changed,//;s/([0-9]+) ([0-9]+ deletion)/\1 0 insertions\(+\), \2/;s/\(\+\)$/\(\+\), 0 deletions\(-\)/;s/insertions?\(\+\), //;s/ deletions?\(-\)//' | awk 'BEGIN {name=""; files=0; insertions=0; deletions=0;} {if ($1 != name && name != "") { print name ": " files " files changed, " insertions " insertions(+), " deletions " deletions(-), " insertions-deletions " net"; files=0; insertions=0; deletions=0; name=$1; } name=$1; files+=$2; insertions+=$3; deletions+=$4} END {print name ": " files " files changed, " insertions " insertions(+), " deletions " deletions(-), " insertions-deletions " net";}'
10
11# show the merge commits in the history line between SHA and staging:
12git log e7722e642a0a49e2061b3fda5ce903783e2fa507..staging --ancestry-path --merges
13
14# Merge with strategy (conflicts resolution)
15git merge --strategy-option ours develop
16
17# Remove all branches but 'master'
18git branch -r | grep 'origin' | grep -v 'master$' | grep -v HEAD | cut -d/ -f2- | while read line; do git push origin :heads/$line; done;
19
20# Show commits of a specific branch
21git log --graph --abbrev-commit --decorate --first-parent <branch_name>
22
23# History of commits difference between 2 branches (production is behind staging, show me the commits for that gap)
24 git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative production..staging
25
26# Fetch and checkout to a new branch from a remote Pull Request
27 git fetch origin pull/1/head:xx
28
29# Detect how many commits is production ahead of staging (3) and behind (6)
30git rev-list --left-right --count staging...production
316 3
32
33# GIT Basics
34HEAD is a reference to the current commit. (.git/HEAD via SHA-1)
35Detached HEAD is a reference to a commit older than the latest commit.
36
37## Fix Conflicts
38git pull -s recursive -X theirs <remoterepo or other repo>
39git pull -X theirs
40git checkout --theirs path/to/file
41
42##
43git merge --squash --allow-unrelated-histories --strategy-option theirs develop
44
45## Navigating through HEAD history
46Show the list og HEADS
47`git reflog`
48Checkout to a previous commit
49`git checkout HEAD@{...}`
50
51# Move the HEAD back to a different commit
52## If there's work to keep:
53Save local modifications
54`git stash`
55Reset to a different ID
56`git reset --hard COMMIT_ID`
57Re-apply the modifications
58`git stash pop`
59
60## Destroy any local modifications.
61`git reset --hard COMMIT_ID`
62
63# Move the HEAD forward (recover from Detached HEAD)
64`git checkout <commit_id>`
65`git checkout -b <new branch> <commit_id>`
66Go 1 commit back
67`git checkout HEAD~1`
68
69# Undo current commit changes
70`git revert <sha-1>`
71
72# Add to Staging Area
73`git add -A`
74# User info
75git config --list
76git config --global user.email admin.google@gmail.com
77
78# Emergency
79###################################################
80# Temporarily switch to a previous commit
81git checkout -b old-state 0d1d7fc32
82
83# Remove all untracked files
84git clean -f
85
86
87# Rollback 1 git pull
88git reset --hard HEAD^1
89
90# Undo git add before commit with
91git reset <file>
92
93# Reset one branch with the last commit of another
94git chekcout messedbranch
95git reset --hard cleanbranch
96
97# Revert/Ignore changes on a file (from PR or other different branch from a stable origin)
98git checkout origin/production my-file.js
99
100# Cancel a local commit
101git reset HEAD~1
102
103# Reset a branch to a specific ID
104git reset --hard c14809fa
105
106# Take a commit from somewhere else, and "play it back" wherever you are right now`
107git cherry-pick <commit-hash>
108
109# Show which files were modified on a commit
110git diff-tree --no-commit-id --name-only -r 98ab5f291fee32506cd23ee981dbdcc3d43c3b25
111
112# Tags
113###################################################
114## Basics
115git tag
116git tag -l "v1.2.*"
117git tag -a v1.2.7 -m "v1.2.7 Release Notes"
118git show v1.2.7
119
120## Show a fancy graph
121git log --all --decorate --oneline --graph
122
123## Show a graph per branch
124git log production --decorate --oneline --grap
125
126# Show all tag dates
127git log --date-order --graph --tags --simplify-by-decoration --pretty=format:'%ai %h %d'
128
129## Push Tags
130git push origin v1.2.7
131git push origin --tags
132
133## Tag Later
134git log --pretty=oneline
135git tag -a v1.2.7 9fceb02
136
137## Advanced
138git checkout v1.2.7
139
140# Branching
141###################################################
142# Remove a remote origin
143git remote remove origin
144
145# Rename a branch locally and on remote
146git branch -m old new
147git push origin :old new --no-verify
148
149# Overwrite remote changes with local
150git push origin staging --force --no-verify
151
152# Push a branch to remove
153git push origin master
154
155# List commits are on production but not on develop
156git log production ^develop --no-merges
157
158#Check with branches have the commits of another one
159git branch --contains branch-to-delete
160
161#Compare differences between 2 branches
162git diff develop..production
163
164#Compare differences between #release/3.1 and #production
165git log --color --pretty=oneline --full-diff production..release/3.1
166
167
168# Remove Branches in Bulk
169git branch | grep feature | xargs git branch -D
170
171# Remove Single Branch
172git branch staging-merge-beta -D
173
174# Reset local to remote
175git checkout mybranch
176git reset --hard origin/mybranch
177
178# Pull all branches from remote
179git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
180git fetch --all
181git pull --all
182
183# Pull a new upstream branch into a fork
184git checkout -b production upstream/production
185
186# Create empty branch
187git checkout --orphan empty-branch
188git rm -rf .
189
190# List remote branches with grep
191git remote show origin | grep "featu*"
192
193# Delete remote branches
194git push -d origin fix/sorting-layers
195
196# Delete remote branches in bulk
197git branch -r | awk -Forigin/ '/\/feature/ {print $2}' | xargs -I {} git push origin :{}
198
199# Delete remote branches in bulk (dry run)
200git branch -r | awk -Forigin/ '/\/feature/ {print $2}'
201
202
203
204# Stash #############################################
205# Discard 9 files and commit only 1
206git add -u
207git reset -- package-lock.json
208git reset -- package-lock.json
209...
210git commit env/.env.development -m 'added new db'
211...
212git checkout -- .
213git push
214
215
216# Show a list of stashed commits (most recent are on top)
217git stash list
218
219# Stash all non commited changes *note: Git will create a temporary commit for the changes
220git stash save “Your stash message”.
221
222# Stash untracked files
223git stash save -u
224# Apply a Stash
225git stash apply stash@{0}
226# Apply and delete a stash
227git stash pop stash@{0}
228
229# Create a branch from a stash *Note: Useful if you run into conflicts after you’ve applied the stash to the latest version of your branch.
230git stash branch newb stash@{0}
231
232# Show a summary of stashes diffs
233git stash show -p
234git stash show stash@{1}
235
236# Delete all stashes permanently *note: not revertable
237git stash clear
238# Delete a stash permanently *note: not revertable
239git stash drop stash@{1}
240
241
242# Git Basics
243###################################################
244# Show all commits pending to push
245git log --branches --not --remotes
246
247# View which files have been changed locally against a remote branch (Your branch is ahead of 'origin/production' by 4 commits.)
248git diff --stat origin/production
249
250# Log Specific Commit
251git show commitId
252
253# Show files with conflicts
254git diff --name-only --diff-filter=U
255
256# List names of all contributors
257git log --format='%aN <%aE>' | awk '{arr[$0]++} END{for (i in arr){print arr[i], i;}}' | sort -rn | cut -d\ -f2-
258git shortlog -s | cut -c8-
259
260# List all commits with names and times
261git log --pretty=format:"%h%x09%an%x09%ad%x09%s"
262
263# Sync all branches with remote
264git fetch --all; git branch -vv
265
266# Undo a commit
267git reset --hard HEAD^
268
269# Create a Git Repo
270git init
271
272# Remove files from branch and disk
273git rm '*.txt'
274
275# Create a Branch
276git branch cleanup
277git branch
278git checkout branch
279
280# Discard local commits (reset staging area)
281git reset HEAD^ #remove all new
282# remove single from staging area and then from tracking
283git reset myFile
284git checkout -- myFile
285
286# Revert changes from a commit id
287git revert 3e6ebdf6e7f682a491e9a1da5a3967b1c6f50b43
288
289
290# Merge pull request via CLI
291git checkout -b temp-pull-branch master
292git pull git@github.com:user/remote-branch-with-pull-request.git master
293git checkout master
294git merge --no-ff temp-pull-branch
295git push origin master
296
297# Delete a feature branch locally and on remote
298git branch -d feature/userid-search-query
299git push -d origin feature/userid-search-query --no-verify
300
301# Find unmerged commits
302git log --pretty=oneline --graph --all
303
304# Find what has been modified (against HEAD)
305git diff
306git diff --cached
307git diff --staged #what has been staged
308git diff HEAD #what is different since my last commit
309
310# Find what has been changed (git commit messages and file names)
311git whatchanged
312
313# Check which files you have ready to push
314git diff --stat --cached origin/develop
315
316# Ignore files
317git rm --cached filetoignore.ext
318git rm --cached -r node_modules/semver
319
320# Rollback a commit (not pushed yet to remote)
321git reset HEAD~1
322
323# Rollback a commit (already pushed to remote)
324git revert HEAD
325
326
327# Change remote URL
328git remote set-url origin URL
329git remote -v
330
331# Push a branch to remote
332git checkout newbranch
333git push --set-upstream origin sites-checker
334
335# Add a remote repository to an existing GIT folder
336git remote add origin git@github.com:Coderecipes/lambda.git
337git push -u origin master
338git pull origin master
339
340# Create a README and a .gitignore
341echo "# My Project" >> README.md
342echo ".DS_Store node_modules" >> .gitignore
343
344# Show ignore files
345git status --ignored
346
347# Add a sub-folder Git repo to the parent repo
348rm -rf path_to_submodule/.git
349git rm --cached path_to_submodule
350
351# Ignore unstaged changes
352git checkout -- .
353
354# Search commits
355## https://git-scm.com/docs/pretty-formats
356git log --grep 'fix' --pretty=oneline
357git log --grep 'fix' --pretty=short
358
359# Show one line history with dates
360git log --pretty=format:"%h%x09%an%x09%ad%x09%s"
361
362
363## Search commits via GitHub WebApp
364## https://github.com/search?utf8=%E2%9C%93&q=repo%3ABannerwise%2Fbannerwise-2.0-website+merge%3Afalse+slider&type=Commits
365
366## Different commits/logs per file (Recommended)
367Add specific files to staging
368`git add file1.txt file2.txt`
369`git commit -m 'feat: change app title`
370Add remaining ones
371`git add .`
372`git commit -m 'add all files'`
373
374## Create git aliases
375`vi ~/.gitconfig`
376[alias]
377 lg = log --color --graph --pretty --abbrev-commit --branches
378`git lg`
379
380## Force Garbage Collection - Clean all the mess in a repository
381`git gc --aggressive --prune=all`Created on 11/7/2017