-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotes_GitCommands.txt
More file actions
163 lines (118 loc) · 9.88 KB
/
Notes_GitCommands.txt
File metadata and controls
163 lines (118 loc) · 9.88 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
GIT and GitHub
--------------
git commit --> snapshots of the code/project or copy and paste the code
git branch --> pointer to the commit or "I want to include the work of this commit and all parent commits."
git checkout <name> --> to check inside branch while committing the code --->> alternative of checkout is "git switch" ---->>> git checkout -b <branchName> -- to create a new branch and checkout into it.
git merge --> combining two branch work into one
------------------------------------------------------
####Note : to understand merge
------------------------------------------------------
Make a new branch called bugFix
Checkout the bugFix branch with git checkout bugFix
Commit once
Go back to main with git checkout
Commit another time
Merge the branch bugFix into main with git merge
------------------------------------------------------
git rebase
--------------------------------------------
####Note : to understand rebase
--------------------------------------------
Checkout a new branch named bugFix
Commit once
Go back to main and commit again
Check out bugFix again and rebase onto main
------------------------------------------------------------------------------------------------------------------------
########################################################################################################################
------------------------------------------------------------------------------------------------------------------------
git checkout main^ --> (^) carat sign point towards first nearest parent..
Note (^^) --> this will go to grand-parent
git reset and git revert --> Reverse the commits made in the git tree (reset works for local machine and revert works for remote machine)
------------------------------------------------------------------------------------------------------------------------------------------
##########################################################################################################################################
------------------------------------------------------------------------------------------------------------------------------------------
git cherry pick --> This command will copy a series of commits below your current location.
e.g. git cherry-pick <commit> <commit> .... <commit>
----------------------------------------------------------------------------------------------------------------------------------------------------
git interactive Rebase --> similar to cherry-pick however when we don't know which commits are to be moved then interactive rebase helps there.
interactive rebase dialog --> in this dialog we can perform two things :-
1. Reordering of commits by changing their order (by dragging and dropping)
2. Choosing all commits or drop specific ones
extra operations done in git interactive rebase are :- Squashing (Combining) commits, Amending commit message and edit the commit/ commit messages.
e.g. git rebase -i HEAD~<number of commits to use>
----------------------------------------------------------------------------------------------------------------------------------------------------
Note :- Branches are easily mutated, often temporary, and always changing.
----------------------------------------------------------------------------
git tags --> it somewhat permanently marked a commits as a milestone which later on can be referred like a branch
e.g. git tag <tagName> <commit>
Note :- tags serve as an anchor in the codebase.
-------------------------------------------------------------------------------------------------------------------
git describe --> it shows the closest anchor in the codebase with respect to your relative position.
e.g. git describe <reference> --> reference means anything which git can resolve into the commit, by default it would be the recently checked out location/commit (HEAD).
and output of above command wil be in below format :
<tag>_<numsCommit>_g<hash> --> <tag> -- the closest tag in history, <numsCommit> -- total number of commits away the tag exist and <hash> -- the hash of the commit
------------------------------------------------------------------------------------------------------------------
Note :- git bisect --> it is used to find the commit which introduced bug into the project by using binary search.
------------------------------------------------------------------------------------------------------------------
--> git rebase <branch1> <branch2> -->> branch1 - current location / HEAD
--> git branch -f <branch> [<branch>/<commit>] -->> to forcefully go from one branch to another branch or commit.
------------------------------------------------------------------------------------------------------------------
**Git Remote --
#benefits :-
1. remote serve as a backup.
2. coding become socail with the help of remote.
git clone --> it will create a local copy of the remote repository
note:- remote branches exist on a local repository, not on remote repository.
-->> naming convention of remote branch is <remoteName>/<branchName> --> eg. o/main
------------------------------------------------------------------------------------
git fetch --> it does two things:
1. download the commits, from remote repository into local repository, which are missing.
2. update the remote branch
--> it does not change the state of local branches or the work in those branches.
git pull --> it is a shorthand operator for [git fetch + merge of the following branch which just fetch]
--------------------------------------------------------------------------------------------------------------------------------------------------
Note :- This command (git fakeTeamwork) may not be legal in this git, but just for understanding it is used.
git fakeTeamwork --> basically it is making a fake changes in remote repository, which seems same as a co-worker has done into the repository
--------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------
git push --> it uploads your changes to a specified remote
Note :- Remember to clone before you push!
-----------------------------------------------------------
----------------------------------------------------------------------------------
Diverged Work :- Remember to do git pull before performing git push, always!
Various ways to come out of diverged work/ambiguity of pushing into the remote :-
1. git fetch & git rebase & git push
2. git fetch & git merge & git push
3. git pull --rebase & git push
4. git pull & git push
Note :- The process of (1 & 3) and (2 & 4) will be same.
----------------------------------------------------------------------------------
Pull Request --> this scenario happens when you are not allowed to push anything into main branch by yourself, so you sent a pull request to your senior through which he/she will review your work and then commit.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Rebasing vs Merging --->> which to use while updating the work from/in remote ---->> can't stick to one, one can have it's own choice on this, howsoever rebasing is what i prefer.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Remote Tracking :
1st Way) git checkout -b <branchName> o/main ---> this command will create a branch naming <branchName> and will set it to track o/main (remote).
2nd Way) git branch -u o/main <branchName> --> this command will assign/set the branch <branchName> to track o/main (remote). note :- <branchName> branch should be one of the existing branch.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Push Arguments :-
git push <remote> <place> --> <place> defines from where the commits are coming and this commits will be pushed into the remote repository.
e.g. git push origin main
<place> arguments :-
git push origin <source>:<destination> --> this is known as colon refspec. (Refspec maps the branch in local repository to a branch in remote repository).
<source> -- local branch and <destination> -- remote
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Fetch Arguments :-
git fetch <remote> <place> --> <place> defines from where the commits will be fetched and this commits will be added to the local remote branch.
e.g. git fetch origin foo
<place> arguments :-
git fetch origin <source>:<destination> ---->> <source> -- remote and <destination> -- local branch
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The command written below can be applied on both git push and git fetch :-
no source parameter :-
git push origin :<destination> --> this command will delete the <destination> branch in remote.
git fetch origin :<destination> --> this command will create a new branch named <destination> in th local repository/machine.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Pull Arguments :-
git pull = git fetch + git merge
so, git pull origin <source>:<destination> --> git fetch origin <source>:<destination>; git merge <destination>;