Git
1
# Cleanup .git http://gcc.gnu.org/ml/gcc/2007-12/msg00165.html
2
git repack -a -d --depth=250 --window=250
3
​
4
# Reset to previous commit
5
git reset HEAD~
6
​
7
# Reset to commit
8
git reset <commit hash> --hard
9
​
10
# Checkout previous commit
11
git checkout HEAD~
12
​
13
# Create new branch
14
git checkout -b
15
​
16
# Revert changes to modified files (working changes)
17
git reset --hard
18
​
19
# New branch without git history & files
20
git checkout --orphan
21
​
22
# Show where git configs get defined
23
git config --show-origin -l
24
​
25
# Undo last commit but don't throw away changes
26
git reset --soft HEAD^
27
​
28
# List all git submodules
29
git submodule--helper list
30
​
31
# Pull from PR
32
git pull origin pull/<issue ID>/head
33
​
34
# List remote branches
35
git branch -a
36
​
37
# Delete branch
38
git branch -d
39
​
40
# Delete remote branch
41
git push origin --delete
42
​
43
# Force overwrite git repo. https://stackoverflow.com/questions/10510462/force-git-push-to-overwrite-remote-files
44
git push -f <remote> <branch>
45
​
46
# Reset to specific commit
47
git reset --hard <commit>
48
​
49
# Remove dir from git
50
git rm --cached -r <dir>
51
​
52
# Rename previous commit
53
git commit --amend
54
​
55
# Force push overwrite
56
git push --force origin master
57
​
58
# Hard reset a branch
59
git reset --hard <branch-name>
60
​
61
# Change remote. i.e. when making a fork of a clone to change upstream destination.
62
git remote rename origin upstream; git remote rename nikitavoloboev origin
63
​
64
# Change upstream to my name so it pushes there
65
git branch --set-upstream-to nikitavoloboev/master master
66
​
67
# Show changes between commits. Where f5352 and be73 are unique commit hashes.
68
git diff f5352 be73
69
​
70
# Update submodule
71
git submodule update
72
​
73
# Set PGP key for Git globally. <key> = fingerprint w/o spaces
74
git config --global user.signingkey <key>
Copied!
1
# Delete commit from remote.
2
​
3
# 1. Delete commit from local repo
4
git reset --hard HEAD~1
5
​
6
# 2. Delete commit from remote repo (can get commit using git log)
7
git push -f origin last_known_good_commit:branch_name
Copied!

Change old commit message

1
#
2
# It is bad practice to rewrite history. Works best if no one has pushed commits on top of remote branch you want to rewrite history of.
3
​
4
# 1. Rebase to commit you want to change (~1 means the first ancestor of the specified commit)
5
git rebase -i <hash>~1
6
​
7
# Can also do this
8
git rebase -i HEAD~4 # where HEAD~4 = last 3 commits
9
​
10
# 2. Rename pick to reword (in opened editor) & save/quit
11
​
12
# 3. Change commit message in editor and save
13
​
14
# 4. Overwrite and remove duplicate commits
15
git push --force-with-lease
Copied!
1
git config --global --add alias.pr '!f() { git fetch -fu ${2:-upstream} refs/pull/$1/head:pr/$1 && git checkout pr/$1; }; f'
2
​
3
git config --global --add alias.pr-clean '!git checkout master ; git for-each-ref refs/heads/pr/* --format="%(refname)" | while read ref ; do branch=${ref#refs/heads/} ; git branch -D $branch ; done'
Copied!
Last modified 1yr ago