forked from yugandhar111/gitcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitcommand3
451 lines (398 loc) · 15 KB
/
gitcommand3
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
Alias:
2 git config --global alias.c checkout
3
4 Remove Alias:
5 local
6 vi /home/labsuser/handson-basic/.git/config [local repo path]
7
8 /home/labsuser/handson-basic
9
10 Global
11 ~/.gitconfig [User's home directory]
12
13 Amend last commit message:
14 git commit --amend -m "Modified msg 1"
15
16 Tag:
17 git tag v_0.0.1
18
19 Push Tag to remote repo
20 git push origin v_0.0.1
21
22 Unstage
23 modify <file>
24 git add <file>. [it is staged]
25 git reset <file>
26 git checkout -- <file>
27
28 Remove file from repo
29 git rm <filename>
30 git commit -m ""
31 git push
32
33 Clean local workspace/repo/Untracked files
34 git add *.java
35 git add *.properties
36 git clean -n [List all untracked files]
37 output>>> Would remove 1.txt
38 git clean -f [MAKE sure your actual files are in stage area.]
39
40 output>>
41 Changes to be committed:
42 (use "git reset HEAD <file>..." to unstage)
43
44 new file: F1.java
45
46 Untracked files:
47 (use "git add <file>..." to include in what will be committed)
48
49 1.txt
50 ==> git clean -f [It will only remove 1.txt]
51
52 git clean -d -f [it cleans untracked diretories]
53
54 Rename a file in remote repo:
55 git mv <src> <tar>
56 git commit -m "rename file"
57 git push
58
59 Restore a perticular file
60 git log
61 >>> Pick the respective commit Id which is gone be restore to current local repo
62
63 git checkout <commit Id> <filename>
64
65 out put>>>
66
67 Author: divang <divang.s@gmail.com>
68 labsuser@ubuntu1804:~/handson-basic$ git log -p
69 commit bd47d5f5305cd77ec9fee4fb855ae63d0c240819 (HEAD -> main, origin/main)
70 Author: divang <divang.s@gmail.com>
71 Date: Sat Feb 13 15:59:39 2021 +0000
72
73 feature 1
74
75 diff --git a/F1.java b/F1.java
76 index e69de29..6a11bb3 100644
77
78 git checkout 5913331a3c5bf92f07d445cda60243a3d9a2c180 F1.java
79
80
81 Merge conflict manually:
82 Second user:
83 git pull
84 vi F1.java[ add F11.1 feature ]
85 git add F1.java
86 git commit -m "add F11.1 feature"
87
88 First user:
89 [After second user pushed the code, but first user has not pull the latest code]
90 vi F1.java [add F11.0 feature]
91 git add F1.java
92 git commit -m " added F11.0 feature"
93 git push
94
95 output>>>
96 To https://github.com/github06022021/handson-basic.git
97 ! [rejected] main -> main (non-fast-forward)
98 error: failed to push some refs to 'https://github.com/github06022021/handson-basic.git'
99 hint: Updates were rejected because the tip of your current branch is behind
100 hint: its remote counterpart. Integrate the remote changes (e.g.
101 hint: 'git pull ...') before pushing again.
102 hint: See the 'Note about fast-forwards' in 'git push --help' for details.
103
104 git pull
105
106 outpull>>>
107 Auto-merging F1.java
108 CONFLICT (content): Merge conflict in F1.java
109 Automatic merge failed; fix conflicts and then commit the result.
110
111 git mergetool
112 output>>>
113 Merging:
114 F1.java
115
116 Normal merge conflict for 'F1.java':
117 {local}: modified file
118 {remote}: modified file
119 Hit return to start merge resolution tool (vimdiff):
120
121 [use your vi knowledge to manually clean the file]
122 Diff output:
123 <<<<<<<<<Head
124 <your local changes1>
125 <your local changes2>
126 <your local changes3>
127 "=========================="
128 <remote changes2>
129 <remote changes2>
130 <<<<<<<<<<< <Remote commit Id>
131
132 [After manual changes are done, make sure to save that file.]
133
134 git commit
135 git push
136
137 [If your last merge has failed and getting the below error ===>]
138 output >>>
139 error: You have not concluded your merge (MERGE_HEAD exists).
140 hint: Please, commit your changes before merging.
141 fatal: Exiting because of unfinished merge.
142
143 $git reset --merge [it will revert back the last merge]
144
145
146 Multi repo push
147 Create a empty repo in more than one git web service.
148 - github
149 git url: https://github.com/github06022021/handson-basic.git
150 - gitlab
151 git url: https://gitlab.com/divang/git-hands-on.git
152
153 - $git config --global credential.helper store [make sure, user name and password store configuraion is set]
154
155 Indivisaul push to repo
156 - git remote add <remote repo name> <git url>
157 Eg
158 - git remote add originGitLab https://gitlab.com/divang/git-hands-on.git [make sure remote name should be unique.]
159 - git remote add origin https://gitlab.com/divang/git-hands-on.git [existing one]
160 - git add <filename>
161 - git commit -m ""
162 - git push originGitLab main [it will push to gitlab]
163 - git push origin main [it will push to github]
164
165 Single push to both repo
166 git remote set-url --add --push <remote name> <Git url>
167 Eg
168 - git remote set-url --add --push origin https://gitlab.com/divang/git-hands-on.git
169 - git remote set-url --add --push origin https://github.com/github06022021/handson-basic.git
170
171 - git add <filename>
172 - git commit -m ""
173 - git push origin main [it will push to github and gitlab]
Fork branch sync with upstream
2 348 cd handson-basic/
3 351 mkdir fork-hands-on
4 352 cd fork-hands-on/
5 353 git clone https://gitlab.com/divang/git-hands-on.git
6 355 cd git-hands-on/
7 359 git remote add upstream https://github.com/github06022021/handson-basic.git [Set the upstream]
8 360 git remote -v
9 361 git fetch upstream [Fetch from upstream/orginal repo]
10 364 git pull upstream main
11 368 git push
12
13
14 Revert
15
16 395 git log --oneline -n 10
17 397 vi F7.java [added the buggy configuration or code]
18 399 git add F7.java
19 401 git commit -m "Buggy code has added" [committed the buggy configuration or code]
20 402 git push [pushed to remote repo with the buggy configuration or code]
21 403 git log --oneline -n 10
22 404 vi F8.java [New feature which does not have any issue]
23 405 git add F8.java
24 406 git commit -m "Feature 8 added"
25 409 git log --oneline -n 5 [pick the commit id which has buggy code]
26 408 git revert 0d8d545 [*** revert that perticular commit]
27 410 git push
28 411 git log --oneline -n 5
29
30 Reset
31 --hard
32 429 vi F8.java [do modification 1 ]
33 431 git add F8.java
34 432 git commit -m "F8.1" [do local commit]
35 433 vi F8.java [do more modification 2]
36 434 git add F8.java
37 435 git commit -m "F8.2" [do local commit]
38 441 vi F8.java [do more modification 3]
39 443 git add F8.java [***Staged this changes. Do not commit]
40 448 vi F1.java [do more modification on F1.java file]
41 449 git status [***Do not stage at all. Unstaged file]
42 450 git log --oneline -n 5 [We want to hard reset on a commitId.]
43 ***451 git reset --hard ac2db39. [It reset/clean my stage area and also unstage area. Untracked files are not effected by hard reset.]
44 452 git status [nothing is on stage and unsatge area]
45
46 --soft
47 labsuser@ubuntu1804:~/handson-basic$ git log --oneline -n 10
48 75381e5 (HEAD -> main) F8.1
49 99b64e4 Revert "Removed F8.java"
50 74e7183 Removed F8.java
51 b6ed15a Revert "Buggy code has added"
52 00a5011 Feature 8 added
53 0d8d545 Buggy code has added
54 9e59dbc F7
55 8462e05 F6
56 a8bfb85 (tag: h, develop) Feature F4
57 7e863a1 Feature 3
58 labsuser@ubuntu1804:~/handson-basic$ git reset --soft 99b64e4. [****]
59 labsuser@ubuntu1804:~/handson-basic$ git status
60 On branch main
61 Your branch is behind 'origin/main' by 2 commits, and can be fast-forwarded.
62 (use "git pull" to update your local branch)
63
64 Changes to be committed:
65 (use "git reset HEAD <file>..." to unstage)
66
67 modified: F8.java. [***]
68 new file: F9.java. [***]
69
70 Changes not staged for commit:
71 (use "git add <file>..." to update what will be committed)
72 (use "git checkout -- <file>..." to discard changes in working directory)
73
74 modified: F8.java
75
76 Untracked files:
77 (use "git add <file>..." to include in what will be committed)
78
79 1
80
81 labsuser@ubuntu1804:~/handson-basic$ ls
82 1 1.log app.log conf.txt F1.java F2.java F3.java F4.java F6.java F7.java F8.java F9.java logs
83 labsuser@ubuntu1804:~/handson-basic$ git reset --soft 8462e05 [****]
84 labsuser@ubuntu1804:~/handson-basic$ ls
85 1 1.log app.log conf.txt F1.java F2.java F3.java F4.java F6.java F7.java F8.java F9.java logs
86 labsuser@ubuntu1804:~/handson-basic$ git status
87 On branch main
88 Your branch is behind 'origin/main' by 8 commits, and can be fast-forwarded.
89 (use "git pull" to update your local branch)
90
91 Changes to be committed:
92 (use "git reset HEAD <file>..." to unstage)
93
94 new file: F7.java. [****]
95 new file: F8.java. [****]
96 new file: F9.java. [****]
97
98 Changes not staged for commit:
99 (use "git add <file>..." to update what will be committed)
100 (use "git checkout -- <file>..." to discard changes in working directory)
101
102 modified: F8.java
103
104 Untracked files:
105 (use "git add <file>..." to include in what will be committed)
106
107 1
108
109 --mixed
110 labsuser@ubuntu1804:~/handson-basic$ vi F9.java
111 labsuser@ubuntu1804:~/handson-basic$ git add F9.java [*** Staged]
112 labsuser@ubuntu1804:~/handson-basic$ vi F8.java [*** UnStaged]
113 labsuser@ubuntu1804:~/handson-basic$ git status
114 On branch main
115 Your branch is up to date with 'origin/main'.
116
117 Changes to be committed:
118 (use "git reset HEAD <file>..." to unstage)
119
120 modified: F9.java [***]
121
122 Changes not staged for commit:
123 (use "git add <file>..." to update what will be committed)
124 (use "git checkout -- <file>..." to discard changes in working directory)
125
126 modified: F8.java [***]
127
128 labsuser@ubuntu1804:~/handson-basic$ git log --oneline -n 5
129 016809f (HEAD -> main, origin/main) F8
130 2ad0511 Merge branch 'main' of https://github.com/github06022021/handson-basic into main
131 97a618a F8
132 12c111b Manually reset the last 4 commits.
133 ac2db39 F8.2
134 labsuser@ubuntu1804:~/handson-basic$ git reset --mixed 016809f. [*** mixed reset. It moves all Staged files into Unstage]
135 Unstaged changes after reset:
136 M F8.java [***]
137 M F9.java. [***]
138 labsuser@ubuntu1804:~/handson-basic$ git status
139 On branch main
140 Your branch is up to date with 'origin/main'.
141
142 Changes not staged for commit:
143 (use "git add <file>..." to update what will be committed)
144 (use "git checkout -- <file>..." to discard changes in working directory)
145
146 modified: F8.java [***]
147 modified: F9.java.[***]
148
149 no changes added to commit (use "git add" and/or "git commit -a")
150 labsuser@ubuntu1804:~/handson-basic$
151
152 Rebase
153 main : F1->F2->F3->F5->F6
154 Feature: ->F4
155 Rebase: F1->F2->F3->F5->F6->F4
156
157
158 Developer 1:
159 Feature branch
160 git branch -b F2 main
161 git log
162 >>>>
163 c6c03b7 F13
164 af9cd8c F10-F2
165 036890a F12
166 be06621 F11
167
168 git rebase origin/main
169 F10.java has conflit
170 git mergetool
171 [manually resolve the confit]
172 git add F10.java
173 git rebase --continue
174 git push
175
176 git log
177 >>>
178 ca4c168 (HEAD -> F2, origin/F2) Merge branch 'F2' of https://github.com/github06022021/handson-basic into F2
179
180 Developer 2:
181 git push origin main
182 git log
183 >>>
184 42f7693 (origin/main) F14 [***]
185 6f97e16 F10-main [***]
186 036890a F12
187 be06621 F11
188
189 Rebase Interactive
190 git rebase -i HEAD~10
191
192 output in VI>>>
193 pick 016809f F8
194 pick d5bfe2b F10
195 pick be06621 F11
196 pick 7a7cf02 F14-Combined
197 pick a6b8dab F12
198 pick bd07d7c Fixed F10.java
199 pick 05b5c2c F13
200 pick 2b6040b F15
201 pick e766b4e F16
202 pick a1dc806 F14-F2
203
204 # Rebase 2ad0511..a1dc806 onto 2ad0511 (10 commands)
205 #
206 # Commands:
207 # p, pick = use commit
208 # r, reword = use commit, but edit the commit message
209 # e, edit = use commit, but stop for amending
210 # s, squash = use commit, but meld into previous commit
211 # f, fixup = like "squash", but discard this commit's log message
212 # x, exec = run command (the rest of the line) using shell
213 # d, drop = remove commit
214 #
215 # These lines can be re-ordered; they are executed from top to bottom.
216 #
217 # If you remove a line here THAT COMMIT WILL BE LOST.
218 #
219 # However, if you remove everything, the rebase will be aborted.
220 #
221 # Note that empty commits are commented out
222 ~
223
224
225 GitFlow workflow