-
Notifications
You must be signed in to change notification settings - Fork 2
/
Git_cheatsheet.txt
328 lines (258 loc) · 10.7 KB
/
Git_cheatsheet.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
https://nvie.com/posts/a-successful-git-branching-model/
https://www.atlassian.com/git/tutorials/
Get git repo
-----------------------------------------------------------------------
git clone https://github.com/nhphuong91/Linux.git
Useful options:
--depth=1 -> useful for git with long commit history (ex: tensorflow, opencv...)
--bare -> repo that can only be pushed or pulled from, no direct edit
-b, --branch -> only clone a specific branch/tag -> cloned repo with display no branch, it's ok; using checkout if wanted
-----------------------------------------------------------------------
Initialize git
-----------------------------------------------------------------------
git init <dir>
Useful options: --bare/--templates=<path to tpl dir>
-----------------------------------------------------------------------
Control remote repo
-----------------------------------------------------------------------
Adding a remote to current repo
git remote add <name> <url>
Other actions:
rm or remove <name> - delete branch
prune <name> - delete all local with <name> that not present on remote
rename <old name> <new name> - rename of remote link
show <name> - inspect remote
get-url - show url
Useful options:
-f: fetch remote
--tags: fetch tags from remote
-----------------------------------------------------------------------
Stage changes
-----------------------------------------------------------------------
git add <dir/file>
or
git stage <dir/file> => preferred
Useful options: -p (choose portions of a file to add to the next commit)
-----------------------------------------------------------------------
Commit changes
-----------------------------------------------------------------------
git commit -am "<descriptive msg>"
or
git commit -m "<descriptive msg>"
Useful options: --amend (append current staged changes to previous commit)
-----------------------------------------------------------------------
Revert commits
-----------------------------------------------------------------------
2 methods: reset (best for local commits) & revert (best for pushed commits)
* Reset:
git reset <options> <commit>
-> Undo all commits after <commit> (set the current branch head to <commit>), preserving changes locally
Discards all history and changes back to the specified commit:
git reset --hard <commit>
Useful options: hard/mixed/soft/merge/keep
* Revert:
git revert HEAD
-> append a commit which undo previous commit (work for pushed commit)
-----------------------------------------------------------------------
Branches
-----------------------------------------------------------------------
view current branch + all local branches:
git branch
Create new branch:
git branch <name>
or
git checkout -b <name>
or create new branch from existing branch/commit
git checkout -b <new branch> <existing branch/commit>
Switch branch:
git checkout <branch name>
Merge target branch to current checkout branch:
git merge <target branch> =>> ~ pull request
Delete branch:
git branch -d <name>
Rename branch:
git branch -m <oldname> <newname> -> to rename other branch
or
git branch -m <newname> -> to rename current branch
Get remote branch & switch local branch to it:
git checkout --track origin/<remote_branch>
-----------------------------------------------------------------------
Synchronize changes
-----------------------------------------------------------------------
Downloads all history from the remote tracking branches:
git fetch
Uploads all local branch commits:
git push => ~assume: git push origin master
Create remote branches:
git push <remote name> <branch name>~
Upload to target branch/folder:
git push -u <remote name> <branch to push>
-> -u: set as default upstream
Updates your current local working branch:
git pull
Delete branch/tag & update to remote:
git branch -d <branch name>
git push <remote name> :<branch name>
or
git push <remote name> --delete <branch name>
Upload "amend" commit to remote:
git push --force
-----------------------------------------------------------------------
Push an existing Git repository
-----------------------------------------------------------------------
cd existing_repo
git remote rename origin old-origin
git remote add origin [email protected]:tic/camera-box.git
git push -u origin --all
git push -u origin --tags -> update tags only to remote repo
-----------------------------------------------------------------------
Tag a commit
-----------------------------------------------------------------------
Step 1: git tag -a <tag_name> -m "<message>" -> tag current active branch & current commit
or
git tag -a <tag_name> <commit_sha> -m "message" -> tag target commit
Step 2:
git push --tags
-----------------------------------------------------------------------
Config git
-----------------------------------------------------------------------
git config <scope> <config>
<scope>: --local/--global/--system
<config>: user/format/gui/core/color...
ex:
git config --global user.name "nhphuong91"
git config --global user.email "[email protected]"
-----------------------------------------------------------------------
Create alias (shortcut)
-----------------------------------------------------------------------
git config --globall alias.<shortkey> <cmd>
-----------------------------------------------------------------------
Caching change and go back to last commit to edit
-----------------------------------------------------------------------
Save change:
git stash <options: -u (include untracked files)>
or
git stash save "<msg or description>"
Restore change:
git stash pop
or
git stash pop stash@{<#>}
List all stash
git stash list
Remove stash
git stash clear
or
git stash drop stash@{<#>}
Create new branch from caching change & pop it out
git stash branch <branch name> stash@{<#>}
-----------------------------------------------------------------------
Display commit history
-----------------------------------------------------------------------
git log
Useful options:
-n <# of commit to display>
--author=<name>
--grep=<msg string>
-S"<added-line to search>"
<since>..<until>
--after="<year-month-date>"/--before="<year-month-date>"
--oneline/--stat/-p/-- <file path>
--no-merges/--merges
--graph --decorate --oneline
-----------------------------------------------------------------------
View commit history of file
-----------------------------------------------------------------------
git blame <file>
Useful options:
-L <from line #>,<to line #>: restrict the output to the requested line range
-e: shows the authors email address instead of username
-w: ignores whitespace changes
-M: detects moved or copied lines within in the same file
-C: detects lines that were moved or copied from other files
-----------------------------------------------------------------------
Remove untracked files from the working tree
-----------------------------------------------------------------------
git clean <options>
-> recursively removing untracked files, starting from the current directory
options:
-d: include & recurse into untracked dir
-f: force
-n: dry-run -> recommended
-x: remove ignored files
-i: interactive mode -> ask what to do in detail
-----------------------------------------------------------------------
Remove file from working directory, and staging index
-----------------------------------------------------------------------
git rm <options> <file>
options:
-n: dry-run -> state which files are going to be removed
-r/-f/--/--cached/--ignore-unmatch/q
Undo git rm:
git reset HEAD
or
git checkout .
git diff --name-only --diff-filter=D -z | xargs -0 git rm --cached
-> generate a list of the removed files from the working directory and pipe that list to git rm --cached
-----------------------------------------------------------------------
Changing base (of commits/branches)
-----------------------------------------------------------------------
git rebase <options> <target base>
<target base>: could be either commit/branch...
git rebase --on-to <new base> <old base> <current branch>
options: --interactive or -i/--d (discard)/--p (preserve)/--x (pick)
-----------------------------------------------------------------------
View log for recovery
-----------------------------------------------------------------------
git reflog
or
git reflog show <HEAD/branches/tags/remotes/stash>@{<#>}
<#> could also be "1.minute/hour/day/week/month/year.ago"
-----------------------------------------------------------------------
Merge
-----------------------------------------------------------------------
Merge branch from different repo:
Step 1: Create & checkout branch to be merged
Step 2: pull target branch
git pull <repo url>.git <branch>
Step 3: merge target branch to current branch
git merge <target branch> --allow-unrelated-histories
Step 4: (Optional) Solve merge conflict
When merge conflict occurr, conflicted file will be edit to:
<<<<<<< HEAD
<content existing in HEAD of current branch>
=======
<content existing in HEAD of target branch>
>>>>>>> <target branch name>
Solve & avoid conflict:
1. Direct way: Edit file & commit changes
2. Tools:
_ git status - identify conflicted files
_ git log --merge - produce a log with a list of commits that conflict between the merging branches
_ git diff - find differences between states of a repository/files
_ git checkout - undoing changes to files
_ git reset --mixed - undo changes to the working directory and staging area
_ git merge --abort - exit from the merge process and return the branch to init state
_ git reset - reset conflicted files to a know good state
Merge strategies:
git merge -s recursive branch1 branch2
-> default merge strategy
-> detect and handle merges involving renames, but currently cannot make use of detected copies
git merge -s resolve branch1 branch2
-> only resolve two heads using a 3-way merge algorithm
git merge -s octopus branch1 branch2 branch3 branchN
-> default merge strategy for more than two heads
git merge -s ours branch1 branch2 branchN
-> the preference effectively ignoring all changes from all other branches
git merge -s subtree branchA branchB
-> an extension of the recursive strategy
Recursive Git Merge Strategy Options:
_ ours: auto-resolved cleanly by favoring the 'our' version
_ theirs: opposite of the 'ours' strategy
_ patience: best used when branches to be merged have extremely diverged
_ diff-algorithim: allows specification of an explicit diff-algorithm
_ renormalize: used with merging branches with differing checkin/checkout states
_ no-normalize: Disables the renormalize option
_ no-renames: ignore renamed files during the merge
_ find-renames=n: honor file renames -> default behavior
_ subtree: strategy operates on two trees and modifies how to make them match on a shared ancestor
-----------------------------------------------------------------------