forked from yugandhar111/gitcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitcommand2
262 lines (228 loc) · 8.07 KB
/
gitcommand2
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
First User activities:
2 ======================
3 [github.com] Create a remote repository in github.com [User should have github account]
4
5 [local machine] Init local repo
6 [local machine] Link local repo with remote repository
$git config --global user.name 'abc'
$git config --global user.email 'abc@xyz.com'
7
8 echo "# second-repo" >> README.md
9 git init
10 git add README.md
11 git commit -m "first commit"
12 git branch -M main
13 git remote add origin https://github.com/github06022021/handson-basic.git
$git config --global credential.helper store [Store password]
14 git push -u origin main
16 Use case: 2
17 ============================
18 Second user activities
19 - Cloning and modfication of a file , then push to repo repo.
20 ============================
21
22 84 mkdir second_user
23 85 cd second_user/
24 87 git clone https://github.com/github06022021/handson-basic.git
25 89 cd handson-basic/
26 91 vi README.md [use any text editor to modify this file]
27 92 cat README.md
28 93 git status
29 94 git add README.md
30 git add . [for all files]
31 95 git commit -m "Feature 2 impl"
32 96 git push -u origin main
34 Use case: 3
35 ============================ ============================ ============================
36 First User activities after second user pushed the changes
37 <change directory to local repo>
38 Eg: /home/labsuser/handson-basic [It is a first user git repo in local machine]
39 ============================ ============================ ============================
40 $ cat README.md
41 First configuration
42 Second
44 $ git pull [it downloads all the remote changes to the local system.]
45
46 Output>>
47 remote: Enumerating objects: 5, done.
48 remote: Counting objects: 100% (5/5), done.
49 remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
50 Unpacking objects: 100% (3/3), done.
51 From https://github.com/github06022021/handson-basic
52 9cfa72a..f05c9a3 main -> origin/main
53 Updating 9cfa72a..f05c9a3
54 Fast-forward
55 README.md | 1 +
56 1 file changed, 1 insertion(+)
57
58 $ cat README.md
59 First configuration
60 Second
61 Third
62
63 Use case: 4
64 ===============================
65 Create a feature branch via Github.com [Switch branch/Tags]
67 Second User wants to start work on some feature.
$ git barnch [to check the current branch]
$ git barnch -a [to check the all branch]
68 $ git branch -r [lsit down all remote branches]
69 output>>>
70 Feature-1
71 main
72 $ git checkout <branch name>. [download the codebase of that branch/ switch to that branch]
73 Eg: git checkout Feature-1
74
75 $ git branch
76 output>>
77 *Feature-1. [current working branch]
78 main
79 $ vi README.md
80 $ git add README.md
81 $ git commit -m "Feature 1 impl "
82 $ git push -u origin <branch name>
84 Branch "Feature-1" is ahead of "main" branch. So we need to merge into "main" branch
85
86 Use case: 5
87 ===========
88 Merge the Feature branch into anthoer branch/ main branch
89 User should be changed to branch in which merging is required.
90
91 $ git checkout main
92 $ git merge <Source branch which is going to be merged>
93 Eg:
94 git merge Feature-1 [locally merge Feature-1 branch into main branch]
95 $ git push -u origin main [it updated "main" branch with Feature branch changes]
96
97 Use case: 6
98 =========
99 To use ".gitignore" [git is looking for this file to ignore the file pattern] file: to stop tracking of unwanted files eg log, temp, intermidiate build files.
100 $ cat .gitignore
101 *.txt
102 tmp/
103 output
104 classes
105 $ git add .gitignore
106 $ git commit -m "Added gitignore"
107 $ git push
108
109
110 Use case: 7
111 ===========
112 To use git commit Template: To fallow the convension setup by team-mates or within organisation.
113
114 $cat commit-template.txt
115 Comments: z
117 Jira:
119 Bug:
120
121 Reviwers:
122 Set the commit template in git conf. Based on the scope, we can choose the level. It can be local, global and system.
123
124 $ sudo git config --system commit.template /home/labsuser/commit-template.txt
125 $ git commit
126
127
128 Use case 8:
129 ==========
130 Example for different configuration levels:
131 local config:
132 [listing the local repo config]
133
134 $ git remote add origin https://github.com/github06022021/repo-1.git
135 $ git config --local -l
136 output>>>
137 labsuser@ubuntu1804:~/repo2/second-repo$ git config --local -l
138 core.repositoryformatversion=0
139 core.filemode=true
140 core.bare=false
141 core.logallrefupdates=true
142 """remote.origin.url=https://github.com/github06022021/second-repo.git"""
143 remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
144 branch.master.remote=origin
145 branch.master.merge=refs/heads/master
146
147 [Set a configuration for all repos. This configuration is independent of any repo.]
148 $git config --global core.excludesfile ~/.gitignore_all_repo
149
150 [Set a configuration for all git users in the system [perticular machine]. Set the commit template at System level.
151 $sudo git config --system commit.template /home/labsuser/commit-template.txt
152
153 Use case 9:
154 After completion of any Feature branch, we need to delete from repo Or may be wrongly created a brach.
155 [Pick the branch from github repo]
156 $ git branch -D Feature-1 [locally delete the branch]
157 $ git push origin --delete Feature-1 []
158
159 [See the origin of repo]
160 $ git remote -v
161 origin https://github.com/github06022021/second-repo.git (fetch)
162 origin https://github.com/github06022021/second-repo.git (push)
163
164 Use case 10:
165 Diff on various commits:
166
167 Same branch
168 $ git diff
169 <green> those are local changes
170
171 Use case 11:
172 To identify the diffence betwee current branch changes and cross other branch:
173
174 $ git checkout F2
175 $ git pull
176 $ echo "db()" >>App3.java
177 $ git add App3.java
178 $ git diff --staged
179 output>>
180 labsuser@ubuntu1804:~/repo2/second-repo$ git diff --staged
181 diff --git a/App3.java b/App3.java
182 new file mode 100644
183 index 0000000..1f5902a
184 --- /dev/null
185 +++ b/App3.java
186 @@ -0,0 +1,3 @@
187 +db()
188
189 $ git push
190
191 [branch diff]
192 $ git diff <branch name>
193 Eg: git diff master
194 Eg: git diff F2
195
196 [Commit difference]: For this, we need to know the commit Ids
197 $ git log
198 output>>
199 Commit 68017db15a45604db443064bd30a87e25d553198 (HEAD -> F2, origin/F2)
200 Author: divang <divang.s@gmail.com>
201 Date: Sun Feb 7 17:24:18 2021 +0000
202
203 app4
204
205 commit c2874f9cf8bbfd830d127e7e53955b14d0c2aed7
206 Author: divang <divang.s@gmail.com>
207 Date: Sun Feb 7 17:11:40 2021 +0000
208
209 delete
210
211 $ git diff <CommitId 1> <CommitId 2>
212 Eg:
213 $ git diff 68017db15a45604db443064bd30a87e25d553198 c2874f9cf8bbfd830d127e7e53955b14d0c2aed7
214
215 [Diff external tool]
216 $git difftool 68017db15a45604db443064bd30a87e25d553198 c2874f9cf8bbfd830d127e7e53955b14d0c2aed7
217
218
219 Use case 12:
220 ===========
221 How to store unfinish work and continue on other work items.
222 221 git status
223 222 git stash
224 223 git stash list
225 225 git stash apply [apply to stage but do not delete from stash]
226 228 git stash pop [it pisk from top and also remove it from stash list]
227 231 git stash drop [it picks the latest stash ]
228 236 git stash save "App5 implment in progress..."
229 239 git stash clear
230 240 git stash list