-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGit.txt
More file actions
538 lines (415 loc) · 15.5 KB
/
Git.txt
File metadata and controls
538 lines (415 loc) · 15.5 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
Some (not Frequently) Q&A's
---------------------------
****
How to list all files by an author between two commits
****
How to print commit message of a given commit
git log --format=%B -n 1 <commit>
git rev-list --format=%B --max-count 1 <commit>
git show -s --format=%B
****
How to backout all changes committed to local repo but not yet pushed to
remote?
git fetch
git reset --hard origin/master
****
Forgot to commit local changes and checked out [or merged] another branch. How
do I get back older changes?
git reset --merge
****
Revert single uncommitted file
git checkout filename # this is also used to checkout branches. If
# filename is same as branch, this doesn't work
git checkout -- filename # is preferred
****
Revert single committed file to earlier commit
git checkout <commit-ref> path/to/file
****
How to fix log of last committed message.
git commit --ammend # will open an editor to change commit message.
git commit --ammend -m "New message, which will overwrite old one"
git commit --ammend -author "New Author Name <email@address.com>"
****
How to fix logs of last few committed messages. Multiple ways to do this:
(a) Using git rebase:
- `git rebase -i $parent_of_flawed_commit`
- i) Editor will pop up. Change `pick` to `edit` or appropriate
option.
ii) Save and git will replay listed commits.
- Git will drop you in to an editor for every commit you want to
change.
i) Change commit as you like.
ii) git commit --ammend
iii) git rebase --continue
(b) Using git filter-branch. Read following references:
http://stackoverflow.com/questions/750172/how-do-i-change-the-author-of-a-commit-in-git
http://schacon.github.io/git/git-filter-branch.html
https://help.github.com/articles/changing-author-info
****
How to revert a single local commit?
A definitive answer at SO: http://stackoverflow.com/a/6866485/630866
****
Squash multiple commits in to a single one?
Easy way:
# Reset branch to origin's state
git reset origin/master
# All changes are unstaged and present in working directory. Add them and
# commit as one commit.
git add --all
git commit
Longer way:
git rebase -i
****
How to add remote repo (say a friends code base)
git remote add <nickname> <full url> // this full url can be ssh also
Ex:
git remote add friend ssh://sarma@<hostname or IP>/path/to/repo/
git remote -v // should show 'fetch' and 'merge' links to friend's repo.
Fetch & Merge using:
git fetch friend // all branches are fetch'ed
git merge friend/branch
****
How to make an existing Git branch track a remote branch?
http://stackoverflow.com/questions/520650/make-an-existing-git-branch-track-a-remote-branch
****
How to see detailed history of a single file
git log --follow -p -- <path-to-file>
The two important options are:
-p says “show all patch information”
--follow tells git to also show information in the event a file has been renamed
https://stackoverflow.com/a/31306082/630866
https://alvinalexander.com/git/show-commit-history-detailed-for-single-file/
****************************************************************************
Patching:
--------
- Create a patch to a file.
git format-patch <branch> --stdout > file.patch
// <branch> is optional and defaults to current branch.
- Create patch file for each local commit. Let's say, you have two commits.
git format-patch HEAD~~
//This creates two patches
0001-make-stuff-more-awesome.patch
0002-allow-users-to-be-locked.patch
- Create patch of last commit
git format-patch -1 --stdout > file.patch
- Create patch out of a commit
git format-patch -1 <sha>
- Before applying look for stats
git apply --stat file.patch
- Check for errors
git apply --check file.patch
// You can use 'git apply <some>.patch' to have the changes applied to CWD.
// They'll be unstaged and needs to be committed.
- To apply a patch as a commit (with its commit message), use
'git am <some>.patch'
git am --signoff < file.patch // --signoff and '<' are optional
git am -3 file.patch // do a 3 way merge. If there is conflict, it
// can be resolved with help of <<<, ===, >>>>
git am *.patch // all patches are applied, in the order. You cannot apply
// out of sequence patches, individually.
Patching (TLDR;)
---------------
$ git format-patch HEAD-- // generate patches
$ git am *.patch // apply patches, sequentially, including commit msgs.
Reference:
http://ariejan.net/2009/10/26/how-to-create-and-apply-a-patch-with-git/
http://makandracards.com/makandra/2521-git-how-to-create-and-apply-patches
Diffs
-----
# git diff
! changes between index/staging and working files
# git diff --staged
! changes between HEAD and index/staging
! `git diff --cached` also does the same thing. `staged` and `cached`
! can be used interchangeably.
# git diff HEAD
! changes between HEAD and working files
# git diff $commit $commit
! changes between 2 commits
# git diff origin
! diff between HEAD & remote/origin
Shows
-----
# git show
# git show --stat
! a bit more brief
# git show --name-status
! SVN like output: Add, Delete, Modified etc
# git show HEAD
# git show HEAD^^^
! prior to 3 commits
# git show master ~10
! prior to 10 commits
# git show master@{May.16}
! Master branch on May 16th
# git show HEAD:file
! show contents of a file
Stash commands
-----
# git stash
! Push contents of working directory on to stash
# git stash list
! List contents of stash stack
# git stash pop
! Pop topmost entry of stack
# git stash apply
! Apply topmost entry of stach without popping
# git stash show [stash@{WIP}]
! List file names of topmost
# git stash show -p
! Show contents of topmost entry on stack
Searching [git grep is 10% faster than grep]
---------
# git grep -e "pattern" file
# git grep -e "pattern" branch
References
----------
# git branch -l
! shows all the local branches
# git tag -l
! shows all local tags
# git branch -r
! remote branches
Git Tagging
-----------
- Two types of tags, Lightweight and Annotated. Annotated has full info about
the commit.
// List all the tags
$ git tag
$ git tag -l
$ git show <tag-name>
// Create a light weight git tag
$ git tag <name>
// Create an annotated tag
$ git tag -a <name> -m "message"
// Push tags to remote
$ git push origin <tag-name>
$ git push origin --tags
$ git push --tags (push all tags)
// Delete tags locally
git tag -d <tag-name>
git tag --delete <tag-name>
// Delete tags from remote
git push origin -d <tag-name>
git push origin --delete <tag-name>
git push origin :<tag-name>
// We cannot checkout tags in git. We can create a branch from a tag and
// checkout the branch
git checkout -b <branch-name> <tag-name>
// Create a tag from past commit
git tag <tag-name> <reference-commit>
Git is slow
-----------
# git gc
! Run git garbage collection
# git config preloadindex
! Pre-loads index in parallel
# git update-index --asume-unchanged <file>
! Does not index file and hence saves time
****************************************************************************
Advanced Git: Graphs, Hashes and Compressions
https://www.youtube.com/watch?v=ig5E8CcdM9g
****************************************************************************
GIT INTERNALS
==============
$ git init // creates .git/ directory
$ ls .git/
HEAD -- Points to branch you currently checkedout.
branches/
config -- local git config
description
hooks/ -- contains client/server hook scripts
index -- Files containing staging area info.
info/ -- contains .gitignore file
objects/ -- Stores all content for your database.
refs/ -- Stores pointers in to commit objects in that branches.
Core parts of Git: HEAD, index, objects, refs
Tree Objects
---
$ git update-index --add --cacheinfo SHA-1 <filename>
updates staging area/index with <filename> from version SHA-1. --add
flag adds files to staging area. --cacheinfo pulls content from
database and not the file itself.
$ git write-tree
Writes contents of staging area to a tree object.
$ git read-tree --prefix=<name> SHA-1
Read trees in to your staging area.
$ git commit-tree SHA-1
Builds a commit tree object.
$ git update-ref <ref-file> SHA-1
Plumbing Commands
---
git hash-object
$ echo 'test contents' | git hash-object -w --stdin
d670460b4b4aece5915caf5c68d12f560a9fe3e4
$
// -w indicates store the content; --stdin indicates the content
// comes from stdin instead of a file.
git cat-file -p SHA
git cat-file -t SHA // gives type of SHA
git cat-file blob SHA // gives content of the blob (if it's blob)
git cat-file commit SHA // gives detailed info about SHA
git ls-tree SHA // if the SHA is that of a tree, list its contents
git ls-tree -d SHA // list only tree within SHA
git ls-tree --name-only // only blobs (files)
git ls-tree -r SHA // recursively list the tree
git ls-files
Git Interactive Rebase
----------
SHA9 Examples 5.4
SHA8 Examples 5.3
SHA7 Examples 5.2
SHA6 Exampels 4.5 <<< typo
SHA5 Examples 4.4
SHA4 Examples 4.3
SHA3 Examples 5.1 <<< out of order
SHA2 Examples 4.2
SHA1 Examples 4.1
$ git rebase -i SHA2
- This will open editor in REVERSE commit order, like this
pick SHA3 Examples 5.1 <<< move this to right place
pick SHA4 Examples 4.3
pick SHA5 Examples 4.4
pick SHA6 Exampels 4.5 <<< fix the typo
pick SHA7 Examples 5.2
pick SHA8 Examples 5.3
pick SHA9 Examples 5.4
Reference:
https://www.youtube.com/watch?v=tukOm3Afd8s
Git Submodules
-----------
# In main git repo, do following. It'll pull another repo and make it a submodule
$ git submodule add <git url>
- After this, there'll be a `.gitmodules` file created. It's a text file.
- After above command, when you clone repo, it'll not clone submodule.
- Similarly, `git pull` does not pull submodule content.
- If you want to do `git pull` on all submodules, do this:
$ git pull --recurse-submodules
# You can set this in config
$ git config submodule.recurse true
# following will recursively clone submodules
$ git clone <main-repo> --recurse
#
$ git submodule update --init
- Updates or commits to submodules will reflect on parent module. A separate
commit must be done on parent module also.
https://www.youtube.com/watch?v=gSlXo2iLBro
https://www.youtube.com/watch?v=ZYq3NJnO08U
Git Worktree
-----------
- Manage multiple working trees within same repository
$ git worktree add ../another-dir/ # This is best practice. Create a worktree (actual directory) at ../another-dir
$ git worktree list # This will list all worktrees
$ git worktree remove ../another-dir/ # Remove the dir after your work
git worktree add <path> <branch>
- Create a new worktree at <path> and check out the existing <branch> into it.
- Example: git worktree add ../review-feature feature/A
git worktree add -b <new-branch> <path>
- Create a new worktree at <path>, create and check out a new branch named
<new-branch> starting from HEAD.
- Example: git worktree add -b bugfix-B ../bugfix-dir
git worktree add --detach <path> <commit-ish>
- Create a detached HEAD worktree (not on a branch) at a specific commit or
tag.
- Example: git worktree add ../old-version v1.0.0
git worktree list
- List all linked working trees, showing their paths, HEAD commit, and
branch.
- Example: git worktree list
git worktree remove <path>
- Remove a linked worktree. The directory at <path> will be deleted and
metadata in $GIT_DIR/worktrees directory is cleaned up. This command
takes care of deleting the *actual* directory at <path>. You do not have
to 'rm -f <path>'
- Example: git worktree remove ../review-feature
git worktree prune
- Remove stale worktree management files from the main repository's
internal files. Used to clean up after worktree directories have been
manually deleted.
- Example: git worktree prune
https://git-scm.com/docs/git-worktree
https://www.youtube.com/watch?v=2uEqYw-N8uE
https://www.youtube.com/watch?v=s4BTvj1ZVLM
****************************************************************************
* Notes from Building Git (James Coglan)
****************************************************************************
Chapter 2: Getting to know .git
[10:08] tree .git
.git
├── HEAD
├── config
├── description
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── fsmonitor-watchman.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── pre-merge-commit.sample
│ ├── pre-push.sample
│ ├── pre-rebase.sample
│ ├── pre-receive.sample
│ ├── prepare-commit-msg.sample
│ ├── push-to-checkout.sample
│ └── update.sample
├── info
│ └── exclude
├── objects
│ ├── info
│ └── pack
└── refs
├── heads
└── tags
9 directories, 17 files
- To understand git layout, read details here:
https://git-scm.com/docs/gitrepository-layout
[10:08] tree .git
.git
├── HEAD # Contains reference to current commit. Initially it'll
│ # have following:
│ # ref: refs/heads/master
│ # 1) When you create new commit, the commit referenced
│ # by HEAD is used as it's parent.
│ # 2) When you checkout new commit or branch, HEAD will
│ # point to the thing you checked out.
│ # 3) When you perform merge, the requested branch is
│ # merged with whatever HEAD is pointing to.
│
├── config # Settings that appy *only* to this repo and not globally.
├── description # Project name. Edit this file and name the project. Used by gitweb.
│
├── hooks # Scripts to run at various points in Git workflow. Remove
│ │ # `.sample` and edit the files.
│ │
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── fsmonitor-watchman.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── pre-merge-commit.sample
│ ├── pre-push.sample
│ ├── pre-rebase.sample
│ ├── pre-receive.sample
│ ├── prepare-commit-msg.sample
│ ├── push-to-checkout.sample
│ └── update.sample
├── info # This directory is used to save lots of metadata that does
│ │ # not fit anywhere else.
│ │
│ └── exclude # All the files you want to exclude. This is different than
│ # .gitignore where .gitignore is saved along with your code
│ # in the repository. Contents of `exclude` file is not
│ # saved in the repo.
│
├── objects # Git database, where Git stores all the content it
│ │ # tracks: your source code and other assets put under
│ │ # Git version control.
│ │
│ ├── info #
│ └── pack # Stores objects in optimized format.
└── refs
├── heads
└── tags
9 directories, 17 files