0% found this document useful (0 votes)
13 views5 pages

Git Command 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

Git Command 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

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 '[email protected]'

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 <[email protected]>
201 Date: Sun Feb 7 17:24:18 2021 +0000
202

203 app4
204

205 commit c2874f9cf8bbfd830d127e7e53955b14d0c2aed7


206 Author: divang <[email protected]>
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

You might also like