forked from bhattbhavesh91/cheat_sheets
-
Notifications
You must be signed in to change notification settings - Fork 6
/
all_about_git.txt
231 lines (174 loc) · 6.63 KB
/
all_about_git.txt
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
Installation
= sudo apt-get install git
Difference between GIT & SVN
= https://softwareengineering.stackexchange.com/questions/315252/why-does-everyone-use-git-in-a-centralized-manner
Difference between Git and GitHub
= https://stackoverflow.com/questions/13321556/difference-between-git-and-github
Version Number
= git --version
= which git
Configuration
= git config –-system
= git config –-global (user – global to the user)
= git config
Example
= git config --global user.name "Bhavesh Bhatt"
= git config --global user.email "[email protected]"
= git config --global core.editor "vi"
= git config --global color.ui true
To View
= git config --list
Go To Home Directory
= cd ~ and ls -la
we can see a file .gitconfig
= cat .gitconfig
To Install Auto-completion of GIT
= cd ~
= curl -OL https://github.com/git/git/raw/master/contrib/completion/git-completion.bash
= mv ~/git-completion.bash ~/.git-completion.bash
= vi .bashrc
= if [ -f ~/.git-completion.bash ]; then
source ~/.git-completion.bash
fi
Help
= git help
Initialize a repo
Go to the directory and type
= git init
= git add .
= git commit -m "Message"
= git log
3 Tree Architecture
= repository
= staging index
= working
git add = working -> staging
git commit = staging -> repository
Adding files
= git status
report back difference between working directory, staging index and repository.
= git add second_file.txt third_file.txt
= git reset HEAD second_file.txt
Removes the given file from staging index back to working directory.
= git log
Information about the commits
How to view difference in files
= git diff
https://stackoverflow.com/questions/10950412/what-does-1-1-mean-in-gits-diff-output
Difference is
Staging index - Working directory
--- a/first_file.txt
+++ b/first_file.txt
@@ -1,2 +1,3 @@
This is my first file
Line No 2
+My name is BHavesh
--- file name is the repository
+++ file name is the one in working directory
- means "old", as we usually invoke it as diff -u old new
@@ -1,4 +1,2 @@
that is to say to which lines the diff is referred
for file -, line 1 to 4;
for file +, line 1 to 2
= git diff --staged
difference between repository - staging index
= git diff HEAD
difference between repository - working directory
= git rm file_name
removes the file from staging and working directory.
then do a git commit to remove it from the repo
= git rm --cached file1.txt
removes from repository and not the working directory.
= git mv fi1ename1 filename2
renames file1 to file2 and then you can commit it
= git commit -am "Changed number"
commit all the changes from working directory to repo and also update the staging area.
Undo changes
- copy all changes from current branch of repo to the working directory
= git checkout -- index.html
Unstaging a file (reset the change from repo to staging area)
= git reset HEAD f1.txt
= git commit --amend -m "Hello"
This is used to amend a change from staging to the last committed repo!
For example if I make a small change in one of the file and I don't want to create
a new repo for it then I use the above command to ammend it to the commit pointed by HEAD.
= git checkout SHA_value(10 so digits) -- index.html
Copy the changes from the particular commit to the staging area.
git revert command used to revert back all the changes that we made
= git revert SHA_value
Undo multiple commits
git reset
-- soft -> moves head pointer but doesnot change staging index or working directory.
-- mixed -> moves head pointer and changes staging index but doesnot change working directory.
-- hard -> moves head pointer but also makes both staging and working directory the same
To remove untracked file or unwanted file
= git clean -f
To ignore some files while committing or putting in staging directory
= touch .gitignore
https://github.com/github/gitignore/blob/master/Python.gitignore
git config --global core.excludefile ~/.gitignore_global
gitignore_global is the file which includes all the files to exclude
Ignoring a file included in repository
add file.txt in staging and commit it.
Open .gitignore add the file name = file.txt
git rm --cached file.txt - will remove file from staging index but will leave the file as it is in repo and working directory.
now when we do git status it won't show anything.
To track empty directory
= Create an empty file inside the folder with the following name ".gitkeep"
= Now check git status and we can see that the directory is included.
Navigating the commit tree files
= git ls-tree HEAD
= git ls-tree HEAD~ (Parent Commit)
= git ls-tree HEAD~2 (Grandparent Commit)
git log --oneline (Summary of all commits)
To view the changes in commit
= git show SHA_value
To comparing commits
= git diff SHA_value1..SHA_value2
= git diff SHA_value1 (Compares commit with SHA_value1 with working directory)
Branches
= git branch (shows all the branches)
= git branch new_feature (create a new branch)
= git checkout new_feature (To switch to new branch)
= git checkout -b new_feature(Create and check out the new branch)
= git diff master..new_feature (Compare between two branches)
= git branch -m new_feature seo_title (Renaming Branches)
= git branch -d branch_to_delete (Deleting the Branch) - you cannot delete if you are on that branch
Merging code
= git merge branch_to_be_merged
= git merge --abort (To abort merge)
Stash
= git stash save "Hello"
= git stash list
= git stash show -p stash@{0}
= git stash pop stash_number
= git stash apply stash_number
= git stash clear
= git stash drop stash_number
GitHub
= To push to git hub the command is push and pull it its fetch and then merge.
= git remote add origin https://github.com/bhattbhavesh91/exploring_c_programming.git
= git remote -v (To see which site we are fectching or pushing the git)
= git remote rm origin
= git push -u origin master
= git pull origin master
= git clone www.......git (address should end with .git)
pushing an existing repository from the command line
git remote add origin www. ..... .git
git push -u origin master
= https://serverfault.com/questions/255346/what-does-master-mean-in-git-push-origin-master
Adding SSH Key to a machine
- https://help.github.com/articles/connecting-to-github-with-ssh/
- This should work fine
SHA1 is made up of what?
https://gist.github.com/masak/2415865
SHA1 value appears to be - d6cd1e2bd19e03a81132a23b2025920577f84e37
How is computed -
= The source tree of the commit (which unravels to all the subtrees and blobs)
= The parent commit sha1
= The author info
= The committer info (right, those are different!)
= The commit message
Some Links -
= https://askubuntu.com/questions/527551/how-to-access-a-git-repository-using-ssh
= https://stackoverflow.com/questions/1338728/delete-commits-from-a-branch-in-git