-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_notes.txt
263 lines (179 loc) · 9.02 KB
/
git_notes.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
http://mislav.uniqpath.com/2010/07/git-tips/
http://cheat.errtheblog.com/s/git
# Put the current git branch (if applicable) into our prompt
PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
# Undo all working dir changes including new files:
git reset --hard
git clean -f -x -d (this is dangerous and will delete all untracked files and directories!)
# Split older commits:
git rebase -i <upstream>
# Mark the appropriate commits with 'e'
git reset HEAD^
git add <relevant changes>
git commit
# Repeat last two steps as necessary
git rebase --continue
# Push a specific branch:
git push origin <branch>
# Push the checked-out branch:
git push origin HEAD
# Undo previous commit:
git reset --soft HEAD^ (1)
<edit> (2)
git add .... (3)
git commit -c ORIG_HEAD (4)
# Or:
git commit --amend
# Rebase or edit the root commit (from 1.7.2 onward):
git rebase -i --root
# To edit, make desired changes, add them, and finish the commit with:
git commit --amend
# Rebase to remote repo:
git pull --rebase <repo> <branch>
# Cherry-pick a range of commits in pre-1.7.2:
# Assume that tag A is the commit on top of which the patch series that you want to select is based and that tag B is the commit of the final patch in the series.
# Also, assume that br is the name of the current branch and the branch where the new patch series should be applied.
# Checkout a new temporary branch at the current location
git checkout -b tmp
# Move the br branch to the head of the new patchset
git branch -f br B
# Rebase the patchset onto tmp, the old location of br
git rebase --onto tmp A^ br
# Change author of historical commits:
# https://stackoverflow.com/questions/16217801/git-rebase-change-author
GIT_COMMITTER_NAME="your-name" GIT_COMMITTER_EMAIL="your-email" git rebase --root --exec 'git commit --amend --author="your-name <your-email>" --no-edit'
# If you want to see moves in git diff you can supply -M in addition to an option that lists which files have changed, e.g.
git diff -M
# git will look at the adjacent trees in the commit history and infer if any files where moved by each commit.
# To find copies as well as renames you can use the -C option, you can supply it twice to make git look harder for possible copy sources at the expense of some performance.
git diff -M -C -C
# To view the log of a file that has been moved/renamed (only works for a single file):
git log --follow <filepath>
# View last stash as a patch:
git stash show -p
# View past stash n as a patch:
git stash show -p stash@{n}
# Rename a stash (requires git 1.8.4 or later, works with dirty working directory):
# https://stackoverflow.com/a/35549615/289099
git stash drop stash@{1}
# This prints something like this: Dropped stash@{1} (<hash>)
git stash store -m "message" <hash>
# Compare commit against its parent:
git diff COMMIT^!
# grep through entire git history:
# http://stackoverflow.com/questions/2928584/how-to-grep-search-committed-code-in-the-git-history
git grep 'search-string' $(git rev-list --all)
# grep through remote branches:
git grep 'search-string' $(git ls-remote . 'refs/remotes/*' | cut -f 2)
# If you run into an "Argument list too long" error:
git rev-list --all | xargs git grep expression
# If you want to limit the search to some subtree (for instance "lib/util") you can pass that to the rev-list subcommand:
git grep <regexp> $(git rev-list --all -- lib/util)
# Search for differences that change the number of occurrences of a string:
git log -S <string>
# Add --pickaxe-regex to search for a regex instead of a string.
# Look for differences whose patch text contains added/removed lines that match a regex:
git log -G <regex>
# View commits in one branch but not in another:
git log <branch> ^<excluded_branch> --no-merges
# Delete a remote branch (either works):
git push origin --delete <branchName>
git push origin :<branchName>
# Add a repo to another (as a subtree, including history):
git remote add <other_repo_nickname> <other_repo_url>
git fetch <other_repo_nickname>
git merge -s ours --allow-unrelated-histories --no-commit <other_repo_nickname>/master
git read-tree --prefix=<new_subdirectory>/ -u <other_repo_nickname>/master
git commit
# You can track upstream changes like so:
git pull -s subtree <other_repo_nickname> master
# Equivalent of 'svn export', only better:
git archive --format zip --prefix=<dir>/ --output <file>.zip <branch> <repo_dir>/
# Display tracked vs. untracked/ignored files in a directory:
git ls-files
git ls-files -o
# View file at a given revision:
git show <rev>:<path>
# Prune all extraneous git data (makes undoing changes impossible):
git reflog expire --expire=now --all
git gc --prune=now
# Determine if a file is still present somewhere in the repo:
git log --all --full-history -- <path>
# Finding and Purging Big Files From Git History
# http://naleid.com/blog/2012/01/17/finding-and-purging-big-files-from-git-history
git verify-pack -v .git/objects/pack/pack-*.idx | egrep "^\w+ blob\W+[0-9]+ [0-9]+ [0-9]+$" | sort -k 3 -n -r > bigobjects.txt
git gc && git rev-list --objects --all | sort -k 2 > allfileshas.txt
for SHA in `cut -f 1 -d\ < bigobjects.txt`; do echo $(grep $SHA bigobjects.txt) $(grep $SHA allfileshas.txt) | awk '{print $1,$3,$7}' >> bigtosmall.txt; done;
# Remove binaries or other arbitrary files from the entire history of the repo (this is dangerous and requires all users to rebase very carefully!):
git filter-branch -d /dev/shm/temp.git --index-filter 'git rm -r --cached --ignore-unmatch *.exe *.sdf *.suo *.fig *.lib *.asv *.dll *.obj *.pdb *.mod *.log *.zip *.doc *.pptx *.pdf *.rtf' -- --all
# Instead consider these options:
https://github.com/newren/git-filter-repo
https://rtyley.github.io/bfg-repo-cleaner/
# Undo git filter-branch:
# http://stackoverflow.com/a/27975288/289099
for orig_ref in $(git for-each-ref --format="%(refname)" refs/original/); do
git update-ref ${orig_ref#refs/original/} $orig_ref
git update-ref -d $orig_ref # to also remove backup refs
done
git fetch
git reset --hard origin/master
# Recovering from upstream rebase:
git rebase --onto <new-parent> <old-parent>
# For example, if origin/master was rebased, and you have 4 local commits:
git rebase --onto origin/master HEAD~4
# Track and pull all remote branches into local branches:
# http://stackoverflow.com/a/4754797/289099
for BRANCH in $(git branch -a | grep remotes | grep -v HEAD | grep -v master); do git branch --track "${BRANCH#remotes/origin/}" "${BRANCH}"; done
git pull --all
# Rename branch locally and remotely:
# https://gist.github.com/lttlrck/9628955
# First make sure your local branch has the same commits as the branch in origin!
git branch -m old_branch new_branch # Rename branch locally
git push origin :old_branch # Delete the old branch
git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote
# Identify refs (references) contained in a git bundle (either works):
git bundle verify <bundle>
git ls-remote <bundle>
# Rename branches remotely (without renaming any local branches at the same time):
git push <remote> <remote>/<old_name>:refs/heads/<new_name> :<old_name>
# Dealing with line endings:
# https://help.github.com/articles/dealing-with-line-endings/
# http://adaptivepatchwork.com/2012/03/01/mind-the-end-of-your-line/
# Reset all projects in a repo:
repo forall -vc "git reset --hard"
# View commits to be rebased while resolving conflicts:
vim .git/rebase-apply/0001
# Delete all merged branches:
# https://stackoverflow.com/questions/6127328/how-can-i-delete-all-git-branches-which-have-been-merged
git branch --merged | grep -v "\*" | grep -vw master | grep -vw main | xargs -n 1 git branch -d
# Now aliased to this:
git delete-merged
# List all authors/contributors/developers on a project
# https://stackoverflow.com/questions/9597410/list-all-developers-on-a-project-in-git
git shortlog --summary --numbered
# or simply:
git shortlog -sn
# To get e-mail addresses as well, add -e
# To show users from all branches (not only the ones in the current branch) you have to add --all
# tig workflow:
# move between commits with C-n, C-p
# open interesting commit with Return
# move inside with j, k, C-u, C-d... (C-n, C-p still work for switching commits)
# if strong focus is needed, open commit in full screen with O, go back to commit view with q
# Open all files changed in HEAD (aliased to git cur):
vim -p $(git show --name-only --pretty="format:")
# Also works with diff for current changes (git prev).
# Get/view/show a file from a specific revision:
git show <treeish>:<file>
# Push a branch from one remote to another:
# https://stackoverflow.com/a/7819233/289099
git push <remote2> <remote1>/<branch>:refs/heads/<branch>
# Show lines changed stats:
# https://stackoverflow.com/questions/2528111/how-can-i-calculate-the-number-of-lines-changed-between-two-commits-in-git/2528129
git diff --shortstat <commit-ish> <commit-ish>
git diff --stat <commit-ish> <commit-ish>
git log --stat <commit1>..<commit2>
# List remote tags:
git ls-remote --tags <remote>
# Delete remote tag:
git push --delete <remote> <branch>