-
Notifications
You must be signed in to change notification settings - Fork 8
/
git
239 lines (178 loc) · 10.8 KB
/
git
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
git tuto: https://www.youtube.com/watch?v=7erfDC1ALvs
//working directory - in file system
//tracked - added once
//git index, staged - after add
git config --global user.email [email protected] //--global is default
git config --local uesr.email username @host.com //local overwrites global
git config --list //shows current directory config
cat ~/.gitconfig //global config file
cat .git/config //local config file
//create a local folder, then merge with remote
git init
git remote add origin <SSH> //origin = my local name for the remote?
git remote -v // shows remote repositories
git remote rm <name> //removes this remote; git remote remove <name>
git pull origin master //fetches changes of the MASTER branch of ORIGIN remote to current branch of local machine
//= git fetch origin + git merge master
git remote show //shows only names
git remote show origin //shows detailed informations
git remote rename <rm1> <rm2> //rename a remote
//start with cloning a remote repo
git clone <ssh> <directory-name> //names the local folder as remote repo name if <directory-name> is not given. by default clones the master branch
git clone -b b1 <ssh> test //clones the b1 branch of the remote ssh to a local folder named TEST
git add [filename]
git add . //add all
git add * //add all
git add -A //add all, "git add ." doesn't work if a file is deleted, then this command must be used
git ls-files //local file info
git ls-remote //remote file info
git checkout -- <filename> //discard all unstaged changes
git status
git status -s
git status --short
//to view changes
git diff //shows local changes, unstaged
//add the file
git diff --cached; git diff --staged //shows staged changed
//commit
git log -p <hash> //hash is optional
git log --patch //same
git log -p -2 //shows last 2
git log --stat -2 //shows how many files were changed/removed/added
git log --pretty=oneline -2 //in one line, also has short/full/fuller value
git log --oneline
git log --pretty=format:"%h - %an, %ar : %s" //hash, username, time, change
git log -pretty=oneline --graph //shows branching graph
git log -pretty=oneline --abbrev-commit -2 //doesn't show full hash, only first few characters
git log --since=1.day //1.hour, yesterday, 2018-11-19
//--until, --before, --after
git log --author=kfoozminus --grep=test //this author, commit-message contans this keyword
git log -1 HEAD //last commit of the HEAD
git commit -m "<message>"
git commit -a -m "<message" //skip the staging/"add" part = add -A + commit - ONLY WORKS FOR TRACKED FILES, that means, files that was "add"ed at least once
git commit --amend -m "<message>" //amends new changes to latest commit, destructs the latest commit. shouldn't amend with a push.
git commit --amend --no-edit //keeps the previous message
//remove from file system, but not from git
rm <file>
//remove from git, removes from file system too
git rm <file>
//remove from just git
git rm --cached <file>
//renaming
git mv <file1> <file2> //file1 is tracked file, renames in both git and file system
//= mv <file1> <file2> + git rm <file1> + git add <file2>
git log //change history, contains commit-hash
git log -p <commit-hash> //shows changes in this commit
git push origin master //origin = remomte repo link(saved in local config file, master = branch name of the remote repo --- means push to MASTER branch of the ORIGIN remote, from current branch of local machine
git push --set-upstream origin master //first time push
.gitignore //this file contains all filenames that we don't want to add to git
git branch --list
git branch //shows local branches
git branch -r //shows remote branches
git branch -a //shows all branches
git branch -vv //shows tracking branches - and which remote branch it tracks
git branch b1 //creates a new branch named b1, cloned from current branch
git checkout b1 //go to b1 branch
git checkout -b b1 //git branch b1 + git checkout b1
git checkout - //go to previous branch
git branch -d b1 //delete a branch, if it has unmerged changes - shows warning
git branch -D b1 //force delete the branch b1
git branch -m <b1> //rename current branch
git merge <b1> //merge b1 to current branch
//then I can delete b1 branch, as my current branch points to that ref
//if b1 doesn't have any file, that current branch has, it will merge (3-way merging --> two branch tips + common ancestor) - and it will create a new commit
//new snapshot = new commit
//if b1 has everything that current-branch has, current-branch tip will fast-forward, no new commit
git rebase <b1> //makes b1 the new base of current master - that way we can avoid three-way merge and do a fast-forward
git rebase <base> <b1> //makes <base> the new base of <b1> - without having to checkout
git rebase --onto <base> <b1> <b2> //take b2, figure out the patches since it diverged from b1, ans make <base> the new base of these patches
git rebase remote/<b1> //progit page 98 - complex
git pull --rebase //git fetch + git rebase remote/<b1>
git config --global pull.rebase true //makes "git pull" behave like the previous command
//fixes the problem of rebasing
//best practice is to use rebase in private work - that has never been made public(which other people might have pulled)
git branch -v //shows last commit of every branch
git branch --merged //shows list of branches that are merged to current branch
git branch --no-merged //shows list of branches that are not merged to current branch
git branch --no-merged <b1> //shows list of branches that are not merged to <b1> branch
git fetch //fetches origin
git fetch --all //fetches all remotes
git fetch <remote-name>
git fetch origin <b1> //fetches ORIGIN's branch B1
git checkout <b1> //check if local has this branch, if not checks remote's branch, creates a local branch named b1, cloned from the remote branch
//if I want to commit changes to a remote branch from local machine without adding the branch
git checkout origin/b1 //works kinda like browsing a repo's branch, not added to local machine
git commit -m "message"
git push origin HEAD:b1 //using HEAD because nothing other than HEAD refers to this commit - this command will push HEAD commit to origin's b1
//this also works - if I checkout to a commit using commit hash and push
//this command will push this HEAD to origin's b1
//if I have a new local branch and remote doesn't have any branch with same name
git push origin <local-branch> //will create a new remote branch with the same name
//if remote has a branch named <branch>
git push origin b1 //updates b1
//if this branch has upstream set by-
git push --set-upstream origin <branch>
//then
git push //don't have to mention remote's branch name everytime we push
git push origin serverfix:serverfix //take local's serverfix and push to remote's serverfix
git push origin serverfix:awesome //take local's serverfix and push to remote's awesome
//progit page 87
git push origin :<b1> //delete remote's branch
git push origin --delete <b1> //delete remote's branch
git checkout -b origin/b1 //creates a local branch named "origin/b1", cloned from current branch, not remote's branch
//If I want to create a branch cloned from remote's branch
//fetch
git checkout <remote>/<remote-branch> //detached HEAD at remote's branch
git checkout -b <local-branch> //creates a new local branch, like remote's branch
git checkout -b <local-branch> origin/<branch> //creates a <local-branch> from origin/<branch>
//shortcut for the previous two
git checkout --track origin/<branch> //creates a local branch from origin/branch with same name - shortcut for previous - only can't name local's explicitly
git checkout <b1> //shortcut for the previous shortcut - first it will check if local has b1, if it doesn't, then if remote has exactly same name branch, then local will create it like remote's
//with these command, both push/pull has these local-remote upstream set
git branch <local> --set-upstream-to <remote/branch> //tracks the remote branch - that will set only pull???
git branch -vv //shows tracking branches - and which remote branch it tracks
//so if a branch doesn't track any remote branch, it cannot pull
git stash save 'stash message' //message is optional, "git stash" works too
git stash list //shows stash lists
git stash apply //makes the 'last stash' dirty
git stash apply stash@{0} //you can drop the last term, by default it takes the latest stash
git stash drop stash@{1} //delete a stash
git stash pop stash@{2} //apply + drop
git cherry-pick <commit-hash> //apply <commit-hash>'s changes
git revert <commit-hash> //undo this commit's changes
//conflicts happen when two developers make changes in same file
git checkout --theirs--. //keep remote's changes
git checkout --ours--. //keep local changes
//otherwise manually
//after commit
git reset --hard <commit-hash> //resets index and working tree everything after <commit-hash> - uncommit + unadd + delete the file
git reset --soft <commit-hash> //doesn't reset, changes head - uncommit. file is "to be committed"
git reset --mixed <hash> //--mixed is default. resets only index. uncommit + unadd. file is "to be added"
//creating tags
git tag //lists tags
git tag -l "v8.*" //lists tags with that wildcard expression
git tag --list "v8.*"
git tag v8.0.1 //create a tag to the commit HEAD is referencing - lightweight tag
git tag -a v8.0.1 -m "tag message" //create annotated tags to HEAD, saves tagger/time/message - best practice
git tag -a <tag-identifier> <hash> //taggin old commits
git tag -a -f <tag-identifier> //use used <tag-identifier> for another commit, -f means force, it will remove the tag from previous commit
git push origin <tag-id> //push this new tag to remote
git push origin --tags
git push --tags //push all tags that are not in remote
git checkout <tag-id> //checks out to this tag
//This puts the repo in a detached HEAD state.
//This means any changes made will not update the tag.
//They will create a new detached commit.
//This new detached commit will not be part of any branch and will only be reachable directly by the commits SHA hash.
//Therefore it is a best practice to create a new branch anytime you're making changes in a detached HEAD state.
git checkout -b <new-branch> <tag-id> //create new branch from this tag
git tag -d <tag-id> //deletes this tag
git show <tag-id> //shows details of this tag
git config --global alias.st status //type "git st" for "git status"
git clean //clean all 'add'ed local changes
git remote set-url origin <new-ssh> //changes remote link
//questions
if I checkout to origin/b1 directly and commit a change, can I create a branch from current state? (and maybe push?)
how to avoid auto merge?
if two different branches changes different part of a same file, will it conflict during merge???
progit page 98 - git rebase remote/<b1>