This repository has been archived by the owner on May 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.tut
390 lines (336 loc) · 11.3 KB
/
git.tut
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
#=============================================================================== 1. Set up Git
$ git config --golbal user.name "your name"
$ git config --golbal user.email "your email"
#
# The information will be stored in ~/.gitconfig.
#
#=============================================================================== 2. Caching GitHub ID
$ git config --global credential.helper cache
$ git config --global credential.helper 'cache --timeout=3600'
#
# Tell Git to remember your GitHub username and password every time it talks to
# GitHub.
#
#=============================================================================== 3. Initialize
$ git init
#=============================================================================== 4. Clone
$ git clone <repository_url>
#=============================================================================== 5. Add Files
https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository
$ git add <file_name>
#
# Adds the file to your local repository and stages it for commit. To unstage a
# file, use 'git reset HEAD <your_file>'.
#
# NOTE Using 'git add .' to add all the file in pwd.
# NOTE Use '$ git status' to staging, '$ git status -s' to get impact info.
#
$ git commit -m "Add existing file"
#
# Commits the tracked changes and prepares them to be pushed to a remote
# repository. To remove this commit and modify the file, use 'git reset --soft
# HEAD~1' and commit and add the file again.
#
NOTE Example:
In "IGV" repository, running:
$ catkin_create_pkg igv_motion roscpp std_msgs
gives out:
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
src/igv_motion/
nothing added to commit but untracked files present (use "git add" to track)
Then putting 2 files into igv_motion/src and running:
$ git add .
$ git status
gives out:
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: src/igv_motion/CMakeLists.txt
new file: src/igv_motion/package.xml
new file: src/igv_motion/src/MotionGenerator.cpp
new file: src/igv_motion/src/MotionGenerator.hpp
#
# NOTE If anything change applied to a tracked file, the file needs to be
# "git add"ed again.
#
#=============================================================================== 6. Ignoring Files
TODO https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository#Ignoring-Files
#=============================================================================== 7. Change Details
$ git diff
#
# Show what exactly you changed, not what files you changed.
#
$ git diff --staged
#
# Show what you've staged that will go into your next commit.
#
#=============================================================================== 8. Commit
$ git commit
#
# $ git commit -v:
#
# puts the diff of your change in the editor so you can see exactly what
# changes you’re committing
#
# $ git commit -m "[commit_message]"
#
# Committing with message.
#
# NOTE According to the website, using '$ git commit' will bring up the editor
# and commits will be created after exiting the editor. However using ":q"
# to quit the editor (Vim) made my commit abort, "due to empty commit
# message" as it said. Maybe ":wq" should be used.
# But by using
#
# $ git commit -m "added MotionGenerator.cpp/hpp"
#
# made a commit without the editor popping up.
#
# NOTE ":wq" IS NOT WORKING.
#
#=============================================================================== 9. Removing Files
$ git rm <file_name>
TODO More details about this topic:
https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository#Removing-Files
#=============================================================================== 10. Moving Files
$ git mv <file_from> <file_to>
#
# This is equivalent to (example):
#
# $ mv README.md README
# $ git rm README.md
# $ git add README
#
# ERROR Somehow this looks not TRUE for me. The '$ git mv ...' and 'rm ...'
# worked the same in my case. It was not copying the file, but directly
# move AND renamed it.
# And when I tried to run line 133, the file cannot be found already.
#
#=============================================================================== 11. View Commit History
$ git log
#
# To remove log file, go to line 125.
#
# NOTE There are several formats for showing logs, for details go to:
#
# https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History
#
#=============================================================================== 12. Undo
#-------------------------------------------------------------------------- 12.1 Amend
$ git commit --amend
#
# If you commit and realize you forgot to stage the changes in a file you
# wanted to add to this commit, then doing this:
#
# $ git commit -m 'initial commit'
# $ git add forgotten_file
# $ git commit --amend
#
# ends up with a SINGLE commit - the 2nd one replaces the results of the 1st.
#
#-------------------------------------------------------------------------- 12.2 Unstaging
$ git add *
#
# For example, you've changed 2 files and want to commit them as 2 separate
# changes, but accidentally type git add * and stage them both.
#
# NOTE Looks like this one works as same as '$ git add .'.
#
$ git status
#
# Gives out (example):
#
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage) --> NOTE
#
# renamed: README.md -> README
# modified: CONTRIBUTING.md
#
$ git reset HEAD CONTRIBUTING.md
#
# Gives out:
#
# Unstaged changes after reset:
# M CONTRIBUTING.md
#
$ git status
#
# Gives out:
#
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# renamed: README.md -> README
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory) --> NOTE
#
# modified: CONTRIBUTING.md
#
#-------------------------------------------------------------------------- 12.3 Unmodifying
$ git check -- CONTRIBUTING.md
$ git status
#
# Gives out:
#
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# renamed: README.md -> README
#
# The changes have been discarded.
#
#=============================================================================== 13. Work with Remotes
#-------------------------------------------------------------------------- 13.1 Show
$ cd ~/Desktop/General_Tools
$ git remote
#
# Gives out:
#
# origin
#
$ git remote -v
#
# Gives out:
#
# origin https://github.com/Neur1n/General_Tools.git (fetch)
# origin https://github.com/Neur1n/General_Tools.git (push)
#
#-------------------------------------------------------------------------- 13.2 Add Repository
TODO https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes
$ git remote add [<options>] <name> <url>
#-------------------------------------------------------------------------- 13.3 Fetch & Pull
$ git fetch <remote_name>
#
# If you clone a repository, the command automatically adds that remote
# repository under the name "origin". So, "$ git fetch origin" fetches any new
# work that has been pushed to that server since you cloned (or last fetched
# from) it.
#
# '$ git fetch' only downloads the data to your local repository – it doesn't
# automatically merge it with any of your work or modify what you're currently
# working on.
#
$ git pull
#
# Automatically FETCH and MERGE that remote branch into your current branch.
#
#-------------------------------------------------------------------------- 13.4 Push
$ git push origin master
#
# NOTE If you and someone else clone at the same time and they push upstream
# and then you push upstream, your push will rightly be rejected. You'll
# have to fetch their work first and incorporate it into yours before
# you'll be allowed to push.
#
#-------------------------------------------------------------------------- 13.5 Inspect
$ git remote show <remote_name>
#
# TODO More details.
#
#-------------------------------------------------------------------------- 13.6 Remove & Rename
$ git remote rename <old_name> <new_name>
$ git remote rm <remote_name>
#=============================================================================== 14. Tagging
TODO https://git-scm.com/book/en/v2/Git-Basics-Tagging
#=============================================================================== 15. Branch
#-------------------------------------------------------------------------- 15.1 Create New Branch
$ git branch <branch_name>
#
# Example:
#
# $ git branch GT_bkp
#
# NOTE It only creates a new branch, doesn't switch to it - the HEAD pointer
# still pointers to master branch. Check it with:
#
# $ git log --oneline --decorate
#
# gives out:
#
# 8665c94 (HEAD, origin/master, origin/HEAD, master, GT_bkp) test fit.tut
# fd874d4 trivial modification
# 83c4ae1 Add files via upload
# 6eb6bb2 Rename tools.txt to tools.tut
# 617572e Add files via upload
# 02bfa6b Initial commit
#
# NOTE This method (line 302) do not show where is HEAD pointing to as in the
# website example. You have to use:
#
# $ git branch
#
# then it gives out:
#
# GT_bkp
# * master --> with green color highlighted
#
#-------------------------------------------------------------------------- 15.2 Switch Branch
$ git checkout <branch_name>
#
# Gives out:
#
# M git.tut
# Switched to branch 'GT_bkp'
#
# Run '$ git branch' again and it gives out:
#
# * GT_bkp
# master --> with green color highlighted
#
# NOTE The equivalent method of "creating & switching" is:
#
# $ git checkout -b <branch_name>
#
$ git add git.tut
$ git commit -m "GT_bkp commit 1"
#
# Make the 1st commit of the GT_bkp branch. Right now the "git.tut" file
# contains the contents up to line 336.
#
$ git checkout master
#
# Switch back to master, the "git.tut" file will be reverted to the last
# snapshot that master points to, which was section 13.3 in my case.
#
# NOTE I noticed that there was a refresh on the git.tut icon, that meant it
# was replaced.
#
#-------------------------------------------------------------------------- 15.3 Merge
$ git add git.tut
$ git commit -m "GT_bkp commit 3"
$ git checkout master
#
# Assume that I was working on git.tut file of GT_bkp branch until line 358,
# then commit it. Before merge GT_bkp/git.tut into master/git.tut, switch back
# to master branch.
#
# TODO "Stashing and cleaning" is mentioned in this section, go to the website
# for more detials.
#
$ git merge GT_bkp
#
# Merge GT_bkp into master. Gives out:
#
# Updating 8665c94..d0924c4
# Fast-forward
# git.tut | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
# 1 file changed, 103 insertions(+), 5 deletions(-)
#
# NOTE "Fast-forward" is emphasized, go to the website for more details.
#
TODO Something different than "fast-forward" is included in the website:
https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging#Basic-Merging
https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging#Basic-Merge-Conflicts
#
# Occurs when the same part of the same file is changed in 2 branches which
# are about to be merged together.
#