-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathGit-Notes
More file actions
738 lines (368 loc) · 14.6 KB
/
Git-Notes
File metadata and controls
738 lines (368 loc) · 14.6 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
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
Day 1 - Commands:
**********************************
Command to install git:
$ yum install git -y
To Check the version of git installed on your server:
$ git --version
Scenario 1: How to create a Local repository
Cretae a working directory
$ mkdir myproject
$ cd myproject
Initialize git on it
$ git init ==> create LR
$ ls -al ==>.git folder will be there
Scenario 2: How to add some files into Local repository
> first create some files in working directory
$ touch index1.html
$ touch index2.html
$ git status
> add the files to stagging area
git add index1.html
OR
git add .
OR
$ git add filename1 file2
> see the status of files
$ git status
> commit the files to Local repo
$ git commit -m "adding files" ==> commits all the files
$ git commit -m "add file only 1" filename ==> only this particular file or change will be commited to local repo
Scenario3: remove the stagged files back to working directory
************************
touch newfile ==> file will be in workign directory
git add newfile ==> added to staggign area
move back the file to working directory from stagging area
*******************************
> git configurations
> git log
> Modify a tracked file and add the changes to local repo
> Delete a file from the local repository and commit the changes.
Scenario 1 :
To check all the commits that we have done on the local repo
$ sudo su -
$ git log
Will give you all commits done so far
HEAD : represents the latest commit done on the repository.
If you want the log to be available in a single line format
$ git log --oneline
Your will be in format:
First 7 characters of your commit & message what you gave with commit
To see all the changes that happened in a commit
$ git log --oneline
$ git show <commitid>
$ git show 1376741
Scenario 3: Git configurations:
They are Variables storing values that are to be used by GIT installed in your local machine
Can be set in 3 ways
Set the configurations as Local, global or System
Variable/configuration that you will set in git
user.name
user.email
git config
If you want to set the variable only to single repository , you will set configuration as local
Locally :
$ git config –local user.name sonal0409
After this if i commit in the repo, the author name will be sonal0409
Global:
**************
If you want to set same user.name and same email on every repository created by the OS user that your are logged in:
$ git config –global user.name Name
$ git config –global user.email emialid
Example:
$ git config –global user.name sonal0409
$ git config –global user.email mittal.sonal04@gmail.com
You can find the configuration at below location
$ cat ~/.gitconfig
System:
*****************
If you want to set same user.name and same email on every repository created by any OS user that your are logged in:
$ git config –system user.name sonal0409
$ git config –system user.email mittal.sonal04@gmail.com
Priority of setting will be given to System → global → local
Where is git going to check the system level configuration:
$ cat /etc/gitconfig
> In this file the system level variable and values will be set
Where is git going to check the global level configuration:
You the find the global configuration set in the file:
$ cat ~/.gitconfig
Where is git going to check the local level configuration:
$ cat .git/config
Command to edit set global configuration:
git config --global --edit
git config --global --edit
> unsetting our configuration file using flag --unset in git config --global
command.
Command used: git config --global --unset user.email
git config --global --unset user.name
Scenario 4:
*********************
Take an existing file which is tracked
$ make changes in the file
$ git diff filename
$ git diff index1.html
Discard the changes that are in working directory permanently
$ git restore index1.html
OR
$ git checkout -- index1.html
Discard the changes from staging area back to working directory:
$ git restore –staged index1.html
OR
$ git reset HEAD index1.html
If a changes are committed on a file and you want to go back to previous version of the file then use revert command
$ git revert commit id
Delete a file from WD and Local Repository and commit the changes
Method 1 : using linux command
In the working directory delete the file using command:
$ rm <filename>
rm: remove regular empty file ‘file1’? y
$ git status ⇒ you will see the file is deleted status
$ git add file1
$ git commit -m “deleted file file1” ⇒ this will remove file from local repo also and will generate a commit id.
Method 2: GIT command to remove a file from working Directory and LR
$ ls
See the files that you have in WD
$ git ls-files
See the files that you have in local repo
Take a filename which is in both WD and LR
$ git rm <filename> ⇒ GIT command to remove a file from working Directory and LR
$ git status ⇒ file will be in deleted state
$ git commit -m “deleted the file”
Scenario 2:
*****************************************
.gitignore concept
It is a concept where you can ask to git to ignore certain files from your working directory.
In the working directory you will create few file:
touch file.xml config.xml data.txt
If your will give git status command, git will tell these files are untracked
But we don’t want these files to be version controlled
In this case, you will create a .gitignore file [ name of file will be same and it has no extension]
Open this file and add the names of all the files or directories, save the file
And git will not track the files names & directories mentioned in .gitignore file
Note: This scenario is applicable to files that are with status as untacked.
Scenario 3:
Create a Remote repositories
> Login to github
> click on New button to create a remote repo
> Name to the repository
> select public repo
> click on Create repository
The URL of my github repo is : https://github.com/Sonal0409/myproject206Aug.git
Create a Personal Access token on github
It is a authentication method which very secure
This is used whenever we have to push files or changes form Local to remote
There is not concept of using password, instead we have to use Personal access token in git locally
Demo:
Go to settings of github → on the left side → go to developer settings → go to Personal Access tokens → Generate New token
Note→ give a name to the token
Expiration -> 30 days
Select scope : repo (1 checkbox)
Click on generate token
Make sure to copy your personal access token now. You won’t be able to see it again!
Connect Local repo to remote repo
Go to git and execute this command
$ git remote add origin https://github.com/Sonal0409/myproject206Aug.git
Execute the command to check local and remote repo is set:
$ git remote -v
To remove local repo and remote repo connection:
$ git remote rm origin
Command that can be used to update existing remote origin:
$ git remote set-url origin <url
Pushing the file to remote from local repo
$ git push origin master
Username : <guthubusername>
Password: give your personal access token
Note: you will not be able to see the token text on screen.. Go ahead and press enter key
Pull those files to your local
$ git pull origin master
Pulling a specific file form a remote repo to local
Create 2 new files in github
In the local git CLI
Execute the command:
$ git fetch origin master
⇒ this command will only fetch the new changes from the remote repo and commits
⇒ it will not merge with WD & LR
For your information - no need to execute:
How to Check what has been fetched from remote repo
cd .git
cat FETCH_HEAD
You will a single commit with remote files information in it
cd ..
$ git diff 2830d1093a8f519b68c929cb4501ad1c247c9fe3
This will show you, which files are not in your local, but are there in remote
Diff ⇒ difference
In this case, diff with commit id , will tell us what has been fetched from remote repo
In order to merge a single file form remote to local repo
Execute this:
$ git checkout origin/master <filename>
$ git checkout origin/master fetchfile1
$ git status ⇒ file will be staged
$ git commit -m “message”
Fork a repo
Forking is copying the remote repo from github account to another github account
Copying repository remote to remote
A fork is a copy of a repository. Forking a repository allows you to freely experiment with changes without affecting the original project.
Cloning of remote repo to Local machine
Conflicts and resolving conflicts
Branching & Merging
Scenario 1: Create branches in Local repo
> Command to check branch in Local repo
$ git branch
> Create a new branch in Local
$ git branch f1
> Switch to the branch
$ git checkout f1
> Add new file and commit on the branch f1
$ touch feature1
$ git add .
$ git commit -m “added onf1”
$ git log –oneline // check the new commit
> Switch back to master branch and compare the 2 branches
> Compare the 2 branches:
git diff --name-status master..f1
OR
Git diff master..f1
Scenario 2:
> Command to create a new branch as well as switch to new branch
$ git checkout -b f2
> Add new file and commit on the new branch
> Switch back to master
Scenario 3:
> Always switch to the branch where you want to merge the files from feature branch
$ git checkout master
> Merge the changes from new branch to master branch
$ git merge <sourcebranch> <destinationbranch>
$ git merge f1 master
> Check the commit history on master
Scenario 4:
Change the name of the branch
$ git branch -M f1 dev
Delete a branch in local:
Make sure , you are not on the branch that is to be deleted
First checkout to master branch
$ git checkout master
$ git branch -D f2
List of branches that have not been merged to master
git branch --no-merged
List of branches that have been merged to master
git branch --merged
Scenario 5:
Create a new branch, switch to the branch , add new changes and commit
$ git branch feature
$ git checkout feature
$ touch newfilename
$ git add .
$ git commit -m “doneon feature”
Push the branch to remote repository
$ git push origin feature
Scenario 6:
Merge 2 branches on remote repository -> Pull requests
Scenario 1: Rebasing in GIT
Rebasing is merging strategy that is always applied on a feature branch
It is never applied on Master branch
In case rebase the commit history on your git repository is re-arranged on the feature branch
The commit history will integrated as a patch
> all the commit of master branch (base branch)
> than all the new commits of done on feature branch
Demo:
> Checkout to feature branch and create 2 new commits on the feature branch
vim benchmarks.rb
git commit -a -m "modified new file on fetaure branch"
Make second commit
vim benchmarks.rb
git commit -a -m "modified new file on fetaure branch"
> Checkout to master branch and make new commits on the master branch
touch file.rb
git add .
git commit -m " done on master"
> We are going to switch to feature branch, on the feature I want to merge the commits of 2 branches in a linear workflow , i.e we should have all commits of master branch available and then commits of feature branch should be there
$ git checkout feature
$ git rebase master
we should have all commits of master branch available and then new commits of feature branch should be there
Scenario 2: Tags in GIT
Light weight tags:
In this method the tag is directly appended to the latest Head commit id
$ git tag release1
To see all tags on the local repo:
$ git tag --list
To see tag associate dto commit id
$ git log –oneline
Output:
d3e9a9d (HEAD -> feature, tag: release1) modified new file on fetaure branch
db50ea3 modified new file on fetaure branch
a29fafc done
e54ddb6 done
0e6bac9 done on feature branch
Annotated tags:
$ git tag -a v1.1 -m “july release” e07cdad
$ git tag --list
$ git show <tagname>
Scenario 1: Stashing in GIT
*******************************
Statshing is done in Local repo
Which is applied only to the files that are tracked by git and have incomplete changes or modifications
$ git ls-files
Take a file that is already tracked by git
Make some modifications to it
$ git status
Execute the stash command
$ git stash
Changes will be stashed in .git folder /refs/stash file
List all stash
$ git stash list
Stash files using interactive mode
$ git stash -p
See what is saved in the stash
$ git show stash@{0}
Delete the stash and move the changes back to WD
$ git stash pop stash@{0}
Keep the stash and move changes back to WD
$ git stash apply stash@{0}
Assignment:
**********************
Password less authentication with GITHUB
Basically git can communicate with GITHUb over https or SSH
Method1
When git is connected to github over https, you will use
Username and personal access token to push files in to repo by authenticating the user.
Method 2:
************************
When git is connect to github over SSH
Step 1:
For this process on the local machine you will have to generate :
SSH private key
SSH public key
The command to generate SSH key is:
Come out of your repo
$ cd
$ ssh-keygen
Press enter key 3 times
The key will be generated and public key will be in /root/.ssh/id_rsa.pub
Get the public key content
$ cat /root/.ssh/id_rsa.pub
You will see the public key
You will then copy the public key
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC214RqDfafQxC5m5mZYnXLDvjMdeF4RKUw/RxszJKO1hAMkyMf5lPVEu2zZ3453646YKGaxdr0ys3IqRDhJXT0NKR5e490eki9LzWTxXODd6YI5wbARacjVRx1kkjVKEvbq3CHXOs1rGCrmDaMQ2rmgHOkdHO2LSxi4p/q897RWmpzs4JoAw6wDlQk0Xn12DGJ7lfTbuiypVY7O223553vNPGoj6mk+mW4lofMejkYeC/E5Yzhnx/Yn4LvP1ptXzzg1WOLICe//PJ7QIn7lCuQa79ce+PSvWOS7auYQN+8SwNC2U+oW/ZbK1NnsPvD/IcMyjyRyFshuEeHuL+WY7wKf+QBmGUlFlQuP root@ip-172-31-22-204.us-east-2.compute.internal
Go to GITHUb and paste the public key in its Settings
GITHUB →Settings → leftside click on SSH & GPG keys → click on new SSh key
Give title of key as purduekey
Copy the key in next box
Save the key
By this method github now identifies you local machine as a authenticated user
Now you don't have to give any username or personal access token while pushing files into github
If you want to push your files over SSH, then the remote URL will be different form https
The remote URL for the repo will be like this:
git@github.com:Sonal0409/myproject206Aug.git
Demo Example :
mkdir 20Aug
cd 20Aug
git init
touch file1 file2 file3
git add .
git commit -m "added to lR"
Create a new remote repo and copy its ssh path
Execute below command on local
git remote add origin git@github.com:Sonal0409/SSHConnectionDemo.git
git remote -v
git push origin master
It will prompt you for authentication, give yes there.
Push will be complete.