-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-mosh-notes.txt
326 lines (231 loc) · 6.96 KB
/
git-mosh-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
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
GETTING STARTED
- initialize git repo
> git init
- move files to staging area
> git add *.py
- commit to git
> git commit -m "MSG"
- direct commit to git, without moving to staging area
> git commit -am "MSG"
- removing files
> git rm <file_name/pattern>
- renaming files
> git mv <old file_name> <new file_name>
- ignoring already tracked file
- add already tracked file to .gitignore
- show files in staging area
> git ls-files
- remove file from only from staging area
> git rm --cached <file_name>
- add files to staging area
- commit
- review code in staging area
> git diff --staged
- check log
> git log --oneline --all --reverse --graph
- show changes in commits
> git show <commit_id>
- show all files changed in commit
> git ls-tree <commit_id>
- unstage changes
> git reset <file_name>
> git restore --staged <file_name>
- discard all changes in current working directory
> git clean
> git clean -fd
- restore a file that was accidently deleted from previous commit
> git restore --source=HEAD~1 <file_path>
BROWSING HISTORY
- view history
> git log
- view all files changed in each commit
> git log --oneline --stat
- view file content that's been changed
> git log --oneline --patch
- filtering history
- last n commits
> git log --oneline -n
- by author
> git log --oneline --author="<author name>"
- by date
> git log --oneline --before="2020-08-17" --after="2020-08-17"
> git log --oneline --before="yesterday" --after="one week ago"
- by commit msg
> git log --oneline --grep="GUI"
- by content (added specific word)
> git log --oneline -S"git_opeation"
> git log --oneline -S"git_opeation" --patch
- by commit range
> git log --oneline <commit_id>..<commit_id>
- which modified a particular file
> git log --oneline <file_name>
> git log --oneline -- <file_name>
- Formatting log output
> git log --pretty=format:"%Cgreen%an%Creset comitted %h on %cd"
- adding alias
> git config --global alias.<alias> "<cmd>"
> git config --global alias.lg "log --pretty=format:'%Cgreen%an%Creset comitted %h on %cd'"
- edit global config file
> git config --global -e
- check file names that were modified in a commit
> git show <commit_id> --name-only
- check file names that were modified in a commit with status
> git show <commit_id> --name-status
- show changes across commits
> git diff HEAD~2 HEAD --name-only <optional file_name>
- checking out a commit
> git checkout <commit_id>
- finding bugs using bisect
> git bisect start
> mark current commit as bad > git bisect bad
> provide last known good commit > git bisect good <commit_id>
> check current HEAD state if good/bad and mark it > git bisect good/bad
> once done > git bisect reset
- find contributers
> git shortlog
- restoring deleted file from history
> git checkout <commit_id> <file_name>
- blame for code
> git blame <file_name>
> git blame -L 1,3 <file_name>
- tagging
> git tag v1.0 <commit_id>
- view tags with messages
> git tag -n
- annotated tags
> git tag -a v1.1 -m "msg"
- delete tag
> git tag -d v1.1
BRANCHING
- view branches
> git branch
- create a branch
> git branch <branch_name>
- switch to different branch
> git switch <branch_name>
- create and switch to new branch
> git switch -C <branch_name>
- rename existing branch
> git branch -m <old branch_name> <new branch_name>
- delete a branch (force delete using -D)
> git branch -d <branch_name>
- delete a branch from remote after removing it in local
> git push origin :<branch_name>
- Comparing branch
- Commits that are in branch_name2 but not in branch_name1
> git log <branch_name1>..<branch_name2>
- Comparing changes
> git diff <branch_name1>..<branch_name2>
- View only files changed
> git diff --name-only <branch_name1>..<branch_name2>
> git diff --name-status <branch_name1>..<branch_name2>
- Stashing changes
(By default, stash doesn't save untracked files, to include them in stash, use --all)
> git stash push -m "<msg>"
> git stash push --all -m "<msg>"
- List stash
> git stash list
- Show changes from stash
> git stash show stash@{#}
> git stash show #
- Apply stash changes
> git stash apply #
- Delete stash
- drop stash by number
> git stash drop #
- drop all stash
> git stash clear
- Merging branch
> git merge <branch_name>
- Merging branch without fast-forward even if its available
> git merge --no-ff <branch_name>
- Viewing merged and unmerged branches
> git branch --merged
> git branch --no-merged
- Cancel merge
> git merge --abort
- Undo faulty merge
- removing last commit (local only)
> git reset --hard HEAD~1
- new commit reverting changes
> git revert HEAD
> git revert -m <parent> HEAD
- Squashing commit
> git merge --squash <branch_name>
The above statement will add files to STAGING AREA
- Rebasing (local only)
>> go to feature branch and
> git rebase <master>
>> in case of conflict, resolve it and
> git rebase --continue
>> other options --skip, --abort
- Cherry Picking
> git cherry-pick <commit_id>
- Picking file from one branch to another
> git restore --source=<from branch> <filename>
OR
> git restore --source=<from branch> -- <filename>
COLLABORATING
- Cloning a repo
> git clone <repo url> <optional directory name>
- check remote repos
> git remote
- check remote repo details
> git remote -v
- get new commits from remote repo
> git fetch
- check divergence bw local and remote branch
> git branch -vv
- pull changes from remote repo (fetch + merge)
> git pull
- Rebasing while pulling
> git pull --rebase
- push changes to remote repo
- first time
> git push origin master
- subsequent times
> git push
- force push (DONT USE)
> git push -f
- push branch to remote
- first time
> git push -u origin <branch_name>
- subsequent push
> git push
- save credentials to cache
> git config --global credentials.helper cache
- push tags to Commits
> git push origin <tag_name>
- delete tags
- from remote repo
> git push origin --delete <tag_name>
- from local repo
> git tag -d <tag_name>
- check remote branches
> git branch -r
- remove remote branches from local
> git remote prune origin
REWRITING HISTORY (only use for local repos)
- removing commit
- only removes the commit, doesn't change working directory or staging area
> git reset --soft HEAD~1
- removes the commit and unstage files from staging area
> git reset --mixed HEAD~1
- undo changes in working directory and staging area
> git reset --hard HEAD~1
- check HEAD history in case of wrong reset
> git reflog
- undo reset by moving HEAD
> git reset --hard <commit id>
- modify/amend last commit
> stage changes
> git commit --amend
- modify/amend earlier commit (interactive rebasing)
> git rebase -i <old commit_id>
can be used to -
- amend earlier commit (include/exclude files)
- drop intermediate commits
- rewording commit messages
- reordering commits
- squash multiple previous commit
- split a commit