-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit.html
758 lines (732 loc) · 38.6 KB
/
git.html
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Git Cheatsheet</title>
<!-- Bootstrap CDN -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.min.css">
<!-- CSS -->
<link rel="stylesheet" href="assets/style.css">
</head>
<body data-theme="light">
<div class="container">
<!-- Theme Toggle -->
<div class="button float-end"><i class="bi bi-circle-half"></i></div>
<h3 class="title">GIT</h3>
<div class="section-list">
<ul>
<li><a href="#initial-setup">Initial Setup</a></li>
<li><a href="#git-ignore-file">Git Ignore File</a></li>
<li><a href="#setting-repo">Setting Repo</a></li>
<li><a href="#view-and-inspect-changes">View and Inspect Changes</a></li>
<li><a href="#staging-and-unstaging">Staging and Unstaging</a></li>
<li><a href="#commit-and-rebase">Commit and Rebase</a></li>
<li><a href="#reset">Reset</a></li>
<li><a href="#push">Push</a></li>
<li><a href="#fetch-merge-and-pull">Fetch Merge and Pull</a></li>
<li><a href="#branch">Branch</a></li>
<li><a href="#remote">Remote</a></li>
<li><a href="#stash">Stash</a></li>
<li><a href="#reflog">Reflog</a></li>
<li><a href="#tags">Tags</a></li>
<li><a href="#tips">Tips</a></li>
</ul>
</div>
<div class="sections" id="initial-setup">
<h4 class="heading"># Initial Setup</h4>
<div class="content">
<div class="table-responsive">
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Command</th>
<th>Info</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>git --version</code></td>
<td>show installed git version</td>
</tr>
<tr>
<td><code>git --help [command]</code></td>
<td>help for specified command</td>
</tr>
<tr>
<td><code>git config --global user.name "<username>"</code></td>
<td>set git username</td>
</tr>
<tr>
<td><code>git config --global user.email "<email>"</code></td>
<td>set git email</td>
</tr>
<tr>
<td><code>git config --global --list</code></td>
<td>list saved global configs</td>
</tr>
</tbody>
</table>
</div>
<p><strong>Note :</strong> All global settings are saved in a file <code>.gitconfig</code> in your home directory.</p>
</div>
</div>
<div class="sections" id="git-ignore-file">
<h4 class="heading"># Git Ignore File</h4>
<div class="content">
<ul class="justify">
<li>It is a special config file. Any files added to it will be excluded from the git repo.</li>
<li>This file should be in the root folder of the repository.</li>
<li>File name: <code>.gitignore</code></li>
<li><a href="https://github.com/github/gitignore">Collection of useful .gitignore templates</a></li>
</ul>
<h6>Ex :</h6>
<pre class="code-box code-box-color"><code># setup.txt will be excluded
setup.txt
# All files with extension.sh will be excluded
*.sh
# folder bin will be excluded
bin/</code></pre>
</div>
</div>
<div class="sections" id="setting-repo">
<h4 class="heading"># Setting Repo</h4>
<div class="content">
<div class="table-responsive">
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Command</th>
<th>Info</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>git init</code></td>
<td>initialize current directory as git repo</td>
</tr>
<tr>
<td><code>git init [repo]</code></td>
<td>create local repo</td>
</tr>
<tr>
<td><code>git clone https://github.com/[username]/[repo]</code></td>
<td>clone remote repo over https</td>
</tr>
<tr>
<td><code>git clone ssh://[email protected]/[username]/[repo].git</code></td>
<td>clone remote repo over SSH</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="sections" id="view-and-inspect-changes">
<h4 class="heading"># View and Inspect Changes</h4>
<div class="content">
<div class="table-responsive">
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Command</th>
<th>Info</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>git status</code></td>
<td>show untracked files</td>
</tr>
<tr>
<td><code>git diff</code></td>
<td>show difference between commits and working tree (unstaged changes)</td>
</tr>
<tr>
<td><code>git diff [source-branch] [target-branch]</code></td>
<td>show diff between branches</td>
</tr>
<tr>
<td><code>git diff [commit] [commit]</code></td>
<td>show diff between two commits</td>
</tr>
<tr>
<td><code>git diff HEAD</code></td>
<td>show all stage + unstage changes</td>
</tr>
<tr>
<td><code>git log</code></td>
<td>show commits log</td>
</tr>
<tr>
<td><code>git log -n</code></td>
<td>show last n commits log</td>
</tr>
<tr>
<td><code>git log --summary</code></td>
<td>show commits history in detailed</td>
</tr>
<tr>
<td><code>git log [commit]</code></td>
<td>show info of commit</td>
</tr>
<tr>
<td><code>git log [branch] [commit]</code></td>
<td>show commit log of specified branch</td>
</tr>
<tr>
<td><code>git log --oneline</code></td>
<td>show each commit in single line only</td>
</tr>
<tr>
<td><code>git log --graph --decorate</code></td>
<td>show commit history in formatted way</td>
</tr>
<tr>
<td><code>git log -g</code></td>
<td>show reflog entries from most recent one to older ones</td>
</tr>
<tr>
<td><code>git show</code></td>
<td>show recent commited changes</td>
</tr>
<tr>
<td><code>git show [commit|file]</code></td>
<td>show changes for commit or file</td>
</tr>
<tr>
<td><code>git blame [file]</code></td>
<td>show last modified revisions & authors for file</td>
</tr>
</tbody>
</table>
</div>
<p><strong>Note :</strong> <code>HEAD</code> is a pointer which points at the current working tree and working branch.</p>
</div>
</div>
<div class="sections" id="staging-and-unstaging">
<h4 class="heading"># Staging and Unstaging</h4>
<div class="content">
<div class="table-responsive">
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Command</th>
<th>Info</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>git add [file]</code></td>
<td>add file to stage area or index</td>
</tr>
<tr>
<td><code>git add .</code></td>
<td>add all changes to stage area or index</td>
</tr>
<tr>
<td><code>git add -A</code></td>
<td>add all changes to stage area or index</td>
</tr>
<tr>
<td><code>git restore [file]</code></td>
<td>discard changes in working directory</td>
</tr>
<tr>
<td><code>git restore --staged [file]</code></td>
<td>unstage file</td>
</tr>
<tr>
<td><code>git rm -r [file]</code></td>
<td>remove file or folder from working tree and index</td>
</tr>
<tr>
<td><code>git checkout .</code></td>
<td>delete all unstage changes or restore working tree</td>
</tr>
<tr>
<td><code>git checkout -- [file]</code></td>
<td>discard changes to file</td>
</tr>
<tr>
<td><code>git checkout [commit]</code></td>
<td>move/switch HEAD to specified commit, in detached HEAD state</td>
</tr>
</tbody>
</table>
</div>
<strong>Note :</strong>
<ul>
<li>Working tree = unstaged = untracked</li>
<li>Index = staged = tracked</li>
</ul>
</div>
</div>
<div class="sections" id="commit-and-rebase">
<h4 class="heading"># Commit and Rebase</h4>
<div class="content">
<div class="table-responsive">
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Command</th>
<th>Info</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>git commit</code></td>
<td>commit staged changes</td>
</tr>
<tr>
<td><code>git commit -m "Commit Message"</code></td>
<td>commit staged changes with commit message</td>
</tr>
<tr>
<td><code>git commit -a -m "Commit Message"</code></td>
<td>stage and commit changes (git add+commit)</td>
</tr>
<tr>
<td><code>git revert [commit]</code></td>
<td>revert commit by producing new commit</td>
</tr>
<tr>
<td><code>git commit --amend</code></td>
<td>amend/change last commit</td>
</tr>
<tr>
<td><code>git commit --amend --no-edit</code></td>
<td>amend last commit without changing commit message</td>
</tr>
<tr>
<td><code>git commit --amend -m "Commit Message"</code></td>
<td>amend last commit with changing message</td>
</tr>
<tr>
<td><code>git rebase</code></td>
<td>merge branch without creating additional merge commit</td>
</tr>
<tr>
<td><code>git rebase -i HEAD~n</code></td>
<td>squash commits ie combine series of commits and condense it to few commits interactively</td>
</tr>
</tbody>
</table>
</div>
<p><strong>Note :</strong> Never rebase on a public branch. Other teams may face conflicts as rebasing changes checksums.</p>
</div>
</div>
<div class="sections" id="reset">
<h4 class="heading"># Reset</h4>
<div class="content">
<div class="table-responsive">
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Command</th>
<th>Info</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>git reset</code></td>
<td>reset current HEAD to specified state (reset index)</td>
</tr>
<tr>
<td><code>git reset HEAD~n</code></td>
<td>reset HEAD to last n-th commit</td>
</tr>
<tr>
<td><code>git reset --soft [commit]</code></td>
<td>doesn't touch index file or working tree but resets head to commit</td>
</tr>
<tr>
<td><code>git reset --mixed [commit]</code></td>
<td>resets index but not working tree ie changed files are preserved but not marked for commit (default action)</td>
</tr>
<tr>
<td><code>git reset --hard [commit]</code></td>
<td>resets index and working tree, Any changes to tracked files in working tree since commit are discarded</td>
</tr>
</tbody>
</table>
</div>
<p>
<strong>Note : <code>checkout</code> vs <code>reset</code></strong><br>
Git reset is specifically about updating the index and moving the HEAD.<br>
Git checkout is about updating the working tree to the index or the specified tree.<br>
It will update the HEAD only if you checkout a branch. If not, then you end up with a detached HEAD.
</p>
</div>
</div>
<div class="sections" id="push">
<h4 class="heading"># Push</h4>
<div class="content">
<div class="table-responsive">
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Command</th>
<th>Info</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>git push</code></td>
<td>push commited changes to remote (default or remembered branch)</td>
</tr>
<tr>
<td><code>git push [remote] [branch]</code></td>
<td>push commited changes to remote branch</td>
</tr>
<tr>
<td><code>git push -u origin [branch]</code></td>
<td>push commited changes to remote branch & remember branch</td>
</tr>
<tr>
<td><code>git push origin --delete [branch]</code></td>
<td>delete remote branch</td>
</tr>
<tr>
<td><code>git push -f</code></td>
<td>force push changes, required when command refuses to update remote ref that is not ancestor of local ref (usally happen with rebase)</td>
</tr>
<tr>
<td><code>git push --force-with-lease</code></td>
<td>
when rebased on already pushed changes, force pushing will lose pushed commit history by others. Force with lease gives flexibility to override new commits on your remote branch, while protecting old
commit history
</td>
</tr>
</tbody>
</table>
</div>
<p><strong>Note :</strong> Use the <code>force-with-lease</code> option instead of <code>f</code> with push.</p>
</div>
</div>
<div class="sections" id="fetch-merge-and-pull">
<h4 class="heading"># Fetch Merge and Pull</h4>
<div class="content">
<div class="table-responsive">
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Command</th>
<th>Info</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>git fetch [remote]</code></td>
<td>download objects and refs (branches, tags) from remote (doesn't merge)</td>
</tr>
<tr>
<td><code>git fetch [short-name]</code></td>
<td>fetch objects and refs from remote</td>
</tr>
<tr>
<td><code>git fetch --all</code></td>
<td>fetch all remotes</td>
</tr>
<tr>
<td><code>git merge [branch]</code></td>
<td>merge fetched histories/changes to active branch</td>
</tr>
<tr>
<td><code>git merge [source-branch] [target-branch]</code></td>
<td>merge fetched changes into target-branch from source-branch</td>
</tr>
<tr>
<td><code>git pull</code></td>
<td>fetch and merge changes from origin (git fetch+marge)</td>
</tr>
<tr>
<td><code>git pull [remote] [branch]</code></td>
<td>pull changes from specified remote branch</td>
</tr>
<tr>
<td><code>git pull --rebase</code></td>
<td>fetch latest changes from origin & rebase</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="sections" id="branch">
<h4 class="heading"># Branch</h4>
<div class="content">
<div class="table-responsive">
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Command</th>
<th>Info</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>git branch</code></td>
<td>list branch (* is current working branch)</td>
</tr>
<tr>
<td><code>git branch -a</code></td>
<td>list all branches (local + remote)</td>
</tr>
<tr>
<td><code>git branch -r</code></td>
<td>list all remote branches</td>
</tr>
<tr>
<td><code>git branch [branch]</code></td>
<td>create new branch</td>
</tr>
<tr>
<td><code>git branch -d [branch]</code></td>
<td>delete branch</td>
</tr>
<tr>
<td><code>git branch -u [branch]</code></td>
<td>set upstream branch</td>
</tr>
<tr>
<td><code>git push origin --delete [branch]</code></td>
<td>delete remote branch</td>
</tr>
<tr>
<td><code>git switch [branch]</code></td>
<td>switch branch</td>
</tr>
<tr>
<td><code>git switch -c [branch]</code></td>
<td>create & switch to new branch</td>
</tr>
<tr>
<td><code>git checkout [branch]</code></td>
<td>switch branch</td>
</tr>
<tr>
<td><code>git checkout -</code></td>
<td>switch to branch last checked out</td>
</tr>
<tr>
<td><code>git checkout -b [branch]</code></td>
<td>create & switch to new branch</td>
</tr>
<tr>
<td><code>git checkout -b [branch] [branch]/Origin</code></td>
<td>clone remote branch and switch to it</td>
</tr>
<tr>
<td><code>git branch -m [new-branch]</code></td>
<td>rename current local branch</td>
</tr>
<tr>
<td><code>git branch -m [old-branch] [new-branch]</code></td>
<td>rename local branch</td>
</tr>
</tbody>
</table>
</div>
<p><strong>Note :</strong> It is recommended to create branches from the main branch.</p>
</div>
</div>
<div class="sections" id="remote">
<h4 class="heading"># Remote</h4>
<div class="content">
<div class="table-responsive">
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Command</th>
<th>Info</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>git remote</code></td>
<td>show active remote</td>
</tr>
<tr>
<td><code>git remote -v</code></td>
<td>show active remote url</td>
</tr>
<tr>
<td><code>git remote show [remote]</code></td>
<td>show info about remote</td>
</tr>
<tr>
<td><code>git remote add origin [path-to-repo]</code></td>
<td>add remote repository</td>
</tr>
<tr>
<td><code>git remote add [short-name] [repository-path]</code></td>
<td>add new remote repo as short-name</td>
</tr>
<tr>
<td><code>git remote rename [old-name] [new-name]</code></td>
<td>rename remote</td>
</tr>
<tr>
<td><code>git remote remove [short-name]</code></td>
<td>delete remote</td>
</tr>
<tr>
<td><code>git remote set-url origin ssh://[email protected]/[username]/[repository-name].git</code></td>
<td>set repo`s origin to SSH</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="sections" id="stash">
<h4 class="heading"># Stash</h4>
<div class="content">
<div class="table-responsive">
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Command</th>
<th>Info</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>git stash</code></td>
<td>save index state WIP on stash</td>
</tr>
<tr>
<td><code>git stash -u</code></td>
<td>save index state with untracked changes on stash</td>
</tr>
<tr>
<td><code>git stash save "stash-name"</code></td>
<td>save WIP with name stash-name on stash</td>
</tr>
<tr>
<td><code>git stash apply</code></td>
<td>apply changes from stash to current branch</td>
</tr>
<tr>
<td><code>git stash apply stash@{n}</code></td>
<td>apply changes from stash n to current branch</td>
</tr>
<tr>
<td><code>git stash list</code></td>
<td>list saved stash entries</td>
</tr>
<tr>
<td><code>git stash pop</code></td>
<td>remove single most recent stash entry and apply to current working tree</td>
</tr>
<tr>
<td><code>git stash drop</code></td>
<td>remove single most recent stash entry</td>
</tr>
<tr>
<td><code>git stash clear</code></td>
<td>remove all stashed entries</td>
</tr>
<tr>
<td><code>git stash branch [branch-name]</code></td>
<td>create new branch from stash and switch to it, also drop corresponding stash</td>
</tr>
</tbody>
</table>
</div>
<strong>Note :</strong>
<ul class="justify">
<li>As in the array, the stash index begins at 0.</li>
<li>It is similar to a data structure stack with LIFO.</li>
</ul>
</div>
</div>
<div class="sections" id="reflog">
<h4 class="heading"># Reflog</h4>
<div class="content">
<div class="table-responsive">
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Command</th>
<th>Info</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>git reflog</code></td>
<td>manage reflog information</td>
</tr>
<tr>
<td><code>git branch [branch-name] [commit]</code></td>
<td>restore specified commit to branch</td>
</tr>
</tbody>
</table>
</div>
<p><strong>Note :</strong> Git reflog can be used to restore deleted or lost commits.</p>
</div>
</div>
<div class="sections" id="tags">
<h4 class="heading"># Tags</h4>
<div class="content">
<div class="table-responsive">
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Command</th>
<th>Info</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>git tag [tag|version]</code></td>
<td>create new tag</td>
</tr>
<tr>
<td><code>git tag -a [tag|version] -m "Tag Message"</code></td>
<td>create new tag with message</td>
</tr>
<tr>
<td><code>git tag [commit]</code></td>
<td>give tag to commit</td>
</tr>
<tr>
<td><code>git tag</code> or <code>git tag -l</code> or <code>git tag --list</code></td>
<td>list all tags</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="sections" id="tips">
<h4 class="heading"># Tips</h4>
<div class="content">
<h5>• Undo recent commit :</h5>
<p>Undo or delete the most recent remote commit.</p>
<pre class="code-box code-box-color"><code>$ git reset --hard HEAD~1
$ git push -f</code></pre>
<h5>• Local to remote repo :</h5>
<p>Creating a local repository and connecting to a non-empty remote repository.</p>
<pre class="code-box code-box-color"><code>$ git init
$ git remote add origin [remote-repo]
$ git pull origin main --allow-unrelated-histories
$ git add -A
$ git commit -m "commit message"
$ git push --set-upstream origin main</code></pre>
</div>
</div>
<!-- Navigation Buttons -->
<nav>
<a href="https://itspatkar.github.io/Cheatsheets/"><i class="bi bi-arrow-left-square-fill"></i></a>
<a href="#" class="float-end"><i class="bi bi-arrow-up-square-fill"></i></a>
</nav>
</div>
<!-- JavaScript -->
<script src="assets/script.js"></script>
</body>
</html>