-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_course_notes
More file actions
540 lines (409 loc) · 18.4 KB
/
git_course_notes
File metadata and controls
540 lines (409 loc) · 18.4 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
# Important stuff in this course:
# Core git, Next level, Collaborating, Advanced - maybe Unnecessary
### GIT CORE START ###
# GIT - version control system, does everything like perforce
# How to Visualise this:
# 1) add checkpoints for any set of files (repos)
# 2) go back in time to that checkpoint
# 3) fork and have a different time line and thay could meet with the first timeline
# Scientists use git + Github for code and theses
# git - offline, local to the machine vs Github - web service for hosts repos
# Configuring Git:
git config user.name
git config user.email
git config --global user.name "vsangiah"
git config --global user.email venkat2797@gmail.com
git config -l
# GIT REPO: workspace with files and folders and history
# Have a master folder - cd into the folder and init git there - now this is a repo
# to get history of the repo
git log
# get status of a git repo
git status
# navigate to the folder and initialise a repo
git init
# creates a .git dir in that folder and it has all the details of various versions
# delete this .git dir, then all version data will be lost
# make sure you are not intiializing a git repo inside a subfolder of an existing repo
# so always get the status first before issuing the git init cmd
# its good to set a default branch name - this is what the repo starts off
git config --global init.defaultBranch "main"
# or u can specify the name of the brach yourself
git branch -m "firstTry"
# if you wish to rename your branch to main or master
git branch -m "newBranchName"
## COMMITTING - a commit is equivalent to a checkpoint in the devlopment process
# it is a snapshot of all changes in a repo
# Work on stuff -> Add changes to as many files as required -> Group these changes into sets and add a label -> Commit each of it individually ->| now you have a check point
# Committing is of two stages: git add (to club the changes) and git commit (send it to repo)
# There are three locations:
# 1) Working Directory
# 2) Staging Area
# 3) Repository
# git add issued from the working dir, stages the changes
# git commit issued after staging, commits the changes to the .git ie the repo
# to add all the untracked changes, use . instead of file paths
git add file1.extn
git add file2.extn file3.extn
git add .
# issue git status to see what changes are added and what are untracked
# Made changes but decided to revert changes to where it started
git restore filename.extn
git restore .
git restore --staged
# Two ways to commmit - issue the git commit with/without the message
# Without -m, issuing git commit will open a text editor and ask for a message
# with -a -m "msg" it will add all unchecked files
git commit
git commit -m "added new files for XYZ and deleted ABC"
git commit -a -m "added new files"
# log command: to display the commits timeline -
# Shows three things:
# 1) commid hash a369f9f682851937bf25cf2e0a159f9190d0af94
# 2) Author, Date
# 3) message for that commit
git log
# Documentation at https://git-scm.com
# Some Sensible Stuff: SSS
# 1) Atomic Commit - single feature or change or fix - so that it is easier to rollback
# 2) Use a Present-tense imperative stype of message for each commit:
# message shall be like giving orders to git or the computer
# 3) If message is multiline detailed explanation: inline -m "message" is not feasible
# so just give git commit and it will open the default editor, in my case it is nano
# 4) To change the default editor
git config --global core.editor "vim"
git config --global core.editor "code --wait"
git config --global core.editor "pluma"
# 5) If we give along multi-line message, git log will give a long message as well.
# How to ammend a commit:
git commit -m "sample msg"
git add forgotten_file.extn
git commit --amend
# How to ignore insignificant/secret files that is usually tracked by default:
# Eg. Secret files, API keys, credentials, OS system files, insignificant log files, dependencies and packages that get built like slprj
# 1) create a REPO_ROOT/.gitignore file and add the files and folders you want to ignore
# you can use wildcard
touch .gitingnore
# edit this file and add line by line names and folders you would like to ignore
# Refer https://gitgnore.io to know what all you would be needing to ignore and
# just copy paste and save your time
.save
.slxc
# Branches:
# Each commit has a hash, also has a parent commit - where it came from
# Imagine linked list of commit operations
# Branches in git allows us to work on different timelines of the project
# Changes in one branch will not affect the other
# Master/Main branch - are the project's main branch - this is the one that works for sure (Bmain)
# HEAD is a pointer to a branch's LAST commit like 'main', 'sub branch' etc.
# We can make HEAD point to the required branch so that we swithc between different timeline
# TO view brances
git branch
# CREATE branch
git branch main2
# switch to a different branch
git switch main2
# After switching, use these statements to find verbose information on where HEAD is pointing to
git status
git log
# ALternate to switch: checkout = switch and restore workign trees - OLD
git checkout main2
# Shorter way to create a branch and switch to it
git swtich -c main3
# Observations when there is an uncommitted file:
# The uncommitted file will follow through all cranches when switcvhed
# SO commit everything and keep the status clean before creating new branches
# To delete a branch:
git branch -d main3
# It will give error that it is not merged
# TO force delete a branch
git branch -D main3
# TO rename a branch
git branch -m "main3TheBest"
# MERGE: happens between two valid branches, to the HEAD pointing branch
# Eg. master, feature1, feature2, feature3 are the branches, user wishes to make feature3 available to master
git swtich master
git merge feature3 -m "merge action to make feature 3 available in master"
# HEAD will point to master and will have feature3
# If two branches have conflicts, then we need to fix the merge issue by opening and seeing those merge markers with HEAD and the mergeing branch
# First see what are conflicting
git diff --check
# Then open up these files to see the conflict markers and decide
# After resolving conflicts ie removing all markers and merging the text, issue the merge command
# Whnen you have uncommitted changes, use the following to see a crazy output to see differences
git diff
# this will show diff for any (un)staged files in the working dir
# Ouput analysis:
# a and b are compared. if renamed, it will be reflected
diff --git a/list.txt b/list.txt
# metadata: not needed
index c265689..4c1fa0f 100644
# markers for a is -, marker for b is +
--- a/list.txt
+++ b/list.txt
# 8 lines extracted from line 1
# 12 lines extracted from line 1
@@ -1,8 +1,12 @@
taylor
billie
eminem
-sia
+dia
sia2
sia3
-billie2
-taylor2
\ No newline at end of file
+box
+cube
+illie2
+taylor2
+taylor3
+
# Other usage for git diff
git diff branch1 branch2
git diff branch1..branch2
# list changes in wd since last commit (staged ones will not be shown)
git diff HEAD
# list the changes that were added to staging area (two ways --styaged, --cached)
git diff --staged
git diff --cached
# Use git log --oneline to get shortened hashes and use them
git diff commithash1 commithash2
git diff commithash1..commithash2
git diff branch1 file_name.extn
git diff --staged file_name.extn
# Stashing - similar but not same as shelving in perforce
# make some changes in one branch, halfway through, and you want to work on another branch
# this is where you can stash the changes in the first branch and move to another branch and work parallelly
git stash
git stash save
git stash push -m "message identifier"
# all three ways are good
# to list any stashes in that branch
git stash list
# now you can check status, youll see nothing to commit
git status
# so you can change to the new branch
git switch someotherbranch
#make changes and get back to master
git switch master
git stash list
# there is only one stash, so you can use stash pop command
# TO Get the stashed work onto the workspace and remove the stash from existence
git stash pop
#stash apply to one or more specific branches, but stash will be still there
git stash apply
git stash apply stash@{NN}
#to drop stashes: remove all the stashed informaiton.
git stash drop
git stash drop stash@{NN}
#TIP: Suppose you made changes and feel like throwing them away
git stash
git stash drop
# NOw this is equivalent to reverting changes in perforce to earlier starting state
# If you dont want any stashes
git stash clear
# Travel in time backward to a commit that was done earlier
git checkout <commitID last seven characters>
# this will detach the HEAD and usually HEAD points to a particular leading branch.
# But when checkout happens, HEAD now points to an earlier commit - now u can use this state to modify the code or content as you wish
# but still after checkout, HEAD is in a bad state. it should point to a leading branch
git swtich master
# this will get the HEAD pointing to the master branch leading node
# Use the Detatched HEAD to create a new branch and work on that alternative timeline
git switch -c new-alternate-timeline
#make changes
git commit -am "new timeline changes"
git log
# to see HEAD is in the new-alternate-timeline
# Suppose you want to move your HEAD in a branch by number of commits
git checkout HEAD~1
git checkout HEAD~2
git checkout HEAD~5
git switch -
# to go to the branch tip where you left off
# How to do Undo changes
# EG. mkae some unnecessary changes in file and you want to revert that file back to (like p4 revert)
#make changes
git checkout HEAD filename1.extn filename1=2.extn
git checkout HEAD
git checkout -- filename.extn filename1.extn
# this will get you back the reverted state back to where we committed earlier
# Another way to Undo changes to HEAD: GIT RESTORE
git restore filename1.extn filename2.extn
# This cannot be redo able. You ll lose all changes made after HEAD
# If you want to restore to some different past commit, say HEAD~2
git restore --source HEAD~2 filename.extn
# NOW, if you want to get back to branch tip HEAD
git restore filename.extn filename2.extn
# Unstage files using RESTORE
# suppose, you have added/staged some things in filename.extn accidentally and you need to unstage
git restore --staged filename.extn
#TIP:
# GIT STATUS cmd gives you all commands to work with. you dont need to remember anything specific to revert.
# GIT RESET: It is like git checkout and deleting commits
# USE IT WITH CAUTION - you'll lose commits and you'll not lose progress in your work. the changes you made will still be there
git reset <commit hash ID 7 chars>
# USE THIS WITH CAUTION: Hard reset: lose commmits and progress. and revert back in time. non reversible
git reset --hard <commith hash ID 7 chars>
# GIT REVERT: reset with message, gives a new commit and moves ahead in time
git revert <commit hashID 7 chars>
# The commit does not go away, but you end up in a higher leading branch tip. git log will give you details that earlier commits are not thrown away but still there.
# this is a safer way to not avoid losing information. and keep record of bad commits
# IF THERE ARE MERGE CONFLICTS:
# resolve them, use git add, git remove <filename.extn> and git revert --continue
# NOW you ll be prompted to add a message
# GIT REVERT IS useful in collaboration
### GIT CORE END ###
##############
# GITHUB
# Hosting platform service for git repos to be put up in cloud - good for collaboration
# Requires internet and an account
# good to backup your work in your local machine
# Others can have access to your work and they'll improve
# GitLab, BitBucket are other services
# Free version - Action minutes/permonth is limited
# Social media for code development and collaboration
# Scanning vulnerablities
# Why GitHub?
# Collaboration
# Opportunity to contribute to open source projects
# Gives you exposure to mentors
#GITHUB BASICS START
# Cloning: (part of git)
# git init - starts a new project
# git clone <URL> - downloads the online repository (GitHub, GitLab etc) to your local machine
# you'll get all the commits and all the
# CHECK whether before starting, do not have any local git repo in a folder
git clone https://github.com/gabrielecirulli/2048.git
# nOW you have a git repo with all the history
# PERMISSIONS:
# if public repo, you can download without any permission
# to push your changes, you need permissions
# Register in github with same email address as $ git config --global user.email
# Generate a SSH Key (secure shell protocol) to upload your work
# from ~/ execute:
ssh-keygen -t ed25519 -C "venkat2797@gmail.com"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
cat ~/.ssh/id_ed25519.pub
# copy the contents to your github settings > Access > SSH and GPC keys > Add SSH
# GET STARTED WITH GITHUB
# 1. create a github repo
# 2. setup a remote connect your local repo
# 3. push up the changes to github
# OR
# 1. create a brand new repo in github
# 2. clone it to local machine
# 3. Work locally
# 4. Push changed to github
# 1st way. you have a loacl repo, and an empty github repo, now do this:
git remote add <name> <url>
# to check if any remote available in your local repo and give a verbose output,
git remote -v
git remote --verbose
# Example
# First Get a token(classic) from the github website
# USer ProfilePic > Settings > Developer Settings > Personal Access Tokens > Tokens (classic) > Create Tokens (classic) > fill the form to get the token
git remote add gitnotes111 https://YOUR_PERSONAL_ACCESS_TOKEN@github.com/vsangiah/git-github-notesREPO_NAME.git
# OR use a SSH key
git remote add origin git@github.com:vsangiah/git-github-notes.git
# to rename a remote,
git remote rename gitnotes111 origin
# to removea a remote
git remote remove origin
# NOW push your local changes to the web
git push origin main
# thats it
# you can also push another branch while not in that branch
git push origin feature2branch
# github gives all verbose stats:
# last time when pushed
# the current branch is NN commits ahead
# recent commits that affected each file
# link to visually see all commits
# Discuss the commits by adding comments and revisit them later
# Closer look:
# git push origin master
# creates a new master branch in empty git repo
# next time you execute this push, any commits in local master branch gets pushed to remote (origin)
# you can push local master to something else in remote
# git push origin featurebranch:master
# OR
# git push origin master:newfeature
# -u option: UPSTREAM
git push -u origin master
# Do this once and upstrean master will be connected to local and will be remembered
# Next time you need to just do
git push
# 2nd way: clone a online github repo to local
# create a new repo in github
# check status of current folder in local machine, you should not be in a git repo
git clone HTTPS_REPO_LINK
# check git status and git remoter now:
git status
git remote
# this gives a default remote named origin and is having the fetch, push link
# UNDERSTAND remote tracking branch vs local branch:
# Take a GitHub repo, clone it to your machine:
# There are two branch references:
# 1. main
# 2. origin/main
# They'll be referencing the tip at the beginning but they can diverge after sometime when local commits are made and no remote pushes are made
# Use this command to see the remote tracking branch
git branch -r
# it will show you origin/main and other online branches.
# git branch will only show you the local branch
# Local repo knows about the online branches
# After doing few local commits, issuing git status will inform you that you are NN commits ahead of origin/main; remote reference doesnt move. Even git log will show the two references for HEAD: local machine's reference and the remote reference
# How to connect the specific online github branch to local
# After git cloning, you are left with main ( that is pointing to -> origin/main)
git branch -r
# get to know the REMOTE_BRANCH_NAME
git switch
REMOTE_BRANCH_NAME
# It automatically creates a new local branch and will track the equivalent remote branch and you can work on the local repo
# FETCHING ANF PULLING:
# Imagine you are working on a sb for a long time and there will be jobs going ahead. Soon you'll be obliged to get the latest revision
#GIT FETCH - get latest revision but it will be done in local repository (no need of merging our working directory)
#GIT PULL - Get latest revision to workspace
# SEQUENCE DIAGRAM:
#
# WORKSPACE ---git add---> STAGING_AREA ---git commit---> LOCAL_REPOSITORY ---git push---> REMOTE_REPOSITORY
# REMOTE_REPOSITORY ---git fetch---> LOCAL_REPOSITORY ---git pull---> WORKSPACE
# cmd:
git fetch REMOTE_NAME
git fetch origin
# Default REMOTE_NAME assumed to be origin
git fetch
# While youre working with a repo for a long time, the online version goeas ahead. What this fetch cmd will do is that it kind of creates a local branching of the latest origin/main's HEAD, so your current local directory will not be disturbed, instead you'll have access to origin/main's state.
# Demo:
# 1. make online github changes and get to a advanced commit state for main
# 2. in your local working dir:
git status
# 3. local directory will say branch main is up to date with origin/main
# 4. Lying. Use git fetch to help yourselves get to know if origin/main has advanced or not
git fetch origin
git fetch origin REMOTE_BRANCH_NAME
# 4. NOW check git status, you'll see, how many commits you are behind
git status
# 5. TO get a sneak peak:
git checkout origin/REMOTE_BRANCH_NAME
# 6. Get to working state get back to the local branch you were working on
git switch LOCAL_BRANCH_NAME
# PULLING:
# First swtich to the branch you want to pull changes down to.
git pull REMOTE REMOTE_BRANCH_NAME
git pull
# this will fetch and merge the latest changes from the online github repo to your local machine's working directory
# mostly this will result in merge conflicts. you need to resolve them
# So the practice is that:
# 1) Clone GitHub Repo
# 2) Work on it locally for few days: some NN commits will be already in online
# 3) Try git fetch - get to know the changes in a local repository. Make your local machine aware of online changes
# 4) Now most probably those changes are not related to your feature work.
# 5) So keep working and when you decide push. Try again git fetch, you ll find more new changes in online github.
# 6) NOW git pull and merge your changes and theirs (Similar to developer's p4 workflow) [but be wary of your current branch]
# 7) NOW you'll be able to push: git push origin main
# GIT PULL shorthand command will pickup the corresponding remote branch while in an appropriate local branch