Bambrow's Blog

Half as much, twice as elegant.

Git 学习笔记

基本概念

  • 工作区(Working Directory):正在工作的文件,也就是在电脑里能看到的目录,也被称作 untracked 区域。使用 git status 后可以在 untracked files 里看到相应的改动。
  • 暂存区(Staging Area or Index):一般存放在 .git 目录下的 index 文件(.git/index)中,所以我们把暂存区有时也叫作索引(index)。此时 Git 会追踪并存储相应的改动。使用 git add 会触发将文件从工作区移动到暂存区的操作。
  • 版本库(Local Repository):也叫本地版本库,在隐藏目录 .git 里面,存储了所有的 checkpoints 和 commits。将文件从暂存区移动到版本库,需要使用 git commit 操作。对于 commit 操作而言,Git 会追踪截至上次 commit 以来的所有改动,并存储为一个检查点。在 commit 结束后,相应的文件会从暂存区移除。
  • 远程仓库(Remote):托管代码的远程服务器。

常用命令

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
# git config
## list config
git config -l
git config --list
git config --list --global
git config --list --local
## edit config file
git config -e --global
git config -e --local
## edit user info
git config --global user.name [name]
git config --global user.email [email]
git config --local user.name [name]
git config --local user.email [email]
## add config
git config --global --add [key] [value]
git config --local --add [key] [value]
## get config
git config --global --get [key] [value]
git config --local --get [key] [value]
## clear config
git config --unset --global user.name
git config --unset --global user.email
git config --unset --local user.name
git config --unset --local user.email

# git init/clone
## init with current directory
git init
## init with new directory
git init [dir]
## clone
git clone [url]
git clone [url] [dir]
git clone [url] -b [branch] [dir]
git clone --depth 1 [url] -b [branch] [dir]

# git remote
## show remote
git remote -v
git remote show [remote]
## add remote
git remote add [remote] [url]
## rename remote
git remote rename [old-remote] [new-remote]
## remove remote
git remote remove [remote]
## modify remote url
git remote set-url [remote] [url]
## git pull/push
git pull [remote] [branch]
git pull --rebase [remote] [branch]
git pull --no-rebase [remote] [branch]
git pull --merge [remote] [branch]
git push [remote] [branch]
git push [remote] --force
git push [remote] --all
## git revert, create a new commit to revert
git revert [commit-id]
## revert without asking for a commit message
git revert [commit-id] --no-edit

# git fetch
git fetch [remote]
git fetch [remote] [branch]

# git checkout
## checkout to existing branch
git checkout [branch]
## git branch + git checkout
git checkout -b [branch]
git checkout -b [branch] [commit-id]
git checkout -b [branch] [tag]
## checkout to previous branch
git checkout -
## track remote branch
git checkout --track [remote]/[branch]
## restore staging to working area
git checkout [path]
## restore commit to staging and working area
git checkout [commit-id] [path]
## git restore
git restore [path]
git restore --staged [path]
git restore --source [commit-id] [path]

# git reset
## reset staging area, keep working area unchanged
git reset [path]
git reset --mixed [path]
## reset staging area to centain commit
git reset [commit-id]
git reset --mixed [commit-id]
## reset staging and working area
git reset --hard
## reset HEAD
git reset --soft [commit-id]
## reset HEAD, staged area and working area
git reset --hard [commit-id]
## HEAD
HEAD # current version
HEAD^ | HEAD~1 # last version
HEAD^^ | HEAD~2 # 2 versions before
HEAD^^^ | HEAD~3 # 3 versions before
## for example, undo a commit that has not been pushed
git reset --soft HEAD~
git reset --soft [commit-id]
## or undo the commit and throw away the code
git reset --hard [commit-id]

# git branch
## list branches
git branch
git branch -v
git branch -r
git branch -a
## create branch
git branch [branch]
git branch [branch] [commit-id]
## track branch
git branch --track [branch] [remote-branch]
## set upstream
git branch --set-upstream [branch] [remote-branch]
## rename branch
git branch -m [old-branch] [new-branch]
## force rename
git branch -M [old-branch] [new-branch]
## delete branch
git branch -d [branch]
## force delete
git branch -D [branch]
## delete remote branch
git branch -dr [remote]/[branch]
git push [remote] --delete [branch]
## git merge
git merge [branch]
git merge --no-commit [branch]
git merge --continue
git merge --abort
## git cherry pick
git cherry-pick [commit-id]
git cherry-pick --no-commit [commit-id]
git cherry-pick --continue
git cherry-pick --skip
git cherry-pick --abort
## git rebase
git rebase [branch]
git rebase -i [branch]
git rebase [branch1] [branch2]
git rebase --continue
git rebase --skip
git rebase --abort

# git add
## add files to staging area
git add [path]
git add [path1] [path2] ...
git add .
## add updated and deleted files to staging area
git add -u [path]
git add --update [path]
## add new, updated and deleted files to staging area
git add -A [path]
git add --all [path]
## add interactively
git add -i [path]
git add --interactive [path]

# git rm/mv
## stop tracking and remove files
git rm [path1] [path2] ...
## stop tracking
git rm --cached [path]
## rename file
git mv [file] [new-file]

# git commit
## commit staging area to local repository
git commit
git commit -m [message]
git commit -v
## git add -u + git commit
git commit -a -m [message]
## amend commit and/or message
git commit --amend
git commit --amend -m [message]
git commit --amend --author [author]
git commit --ammend [path1] [path2] ...

# git diff/show
## show difference between current and staging area
git diff
git diff --name-only
## show difference between staging and last commit
git diff --cached
git diff --staged
## show difference after certain commit
git diff [commit-id]
git diff HEAD
## compare branches
git diff [branch1] [branch2]
git diff [branch1]...[branch2]
git diff [branch1] [branch2] -- [file]
## git show
git show
git show [commit-id]
git show --name-only [commit-id]
git show [commit-id]:[file]

# git status
git status

# git stash
git stash
git stash list
git stash pop
git stash drop
## stash with confirmation of every change
git stash push -p

# git log
## show logs
git log
git log --stat
git log --oneline
git log --pretty
git log [commit-id]
git log -[number]
git log [tag] HEAD --pretty=format:%s
git log -n1 -p
## search logs
git log -S [keyword]
## show logs for a file
git log --follow [file]
git whatchanged [file]
git log -p [file]
## git shortlog
git shortlog -sn
## git blame
git blame [file]
## git reflog
git reflog
git reflog show [branch]
git reflog show main@{0}
git reflog show --all
git reflog stash
git diff stash@{0} main@{0}
git diff main@{0} main@{1.day.ago}

# git tag
## print all tags
git tag
## add tag
git tag [tag]
git tag [tag] [commit-id]
git tag -a [tag] -m [message] [commit-id]
## checkout tag
git checkout [tag]
## show tag info
git show [tag]
## delete tag
git tag -d [tag]
## delete remote tag
git push [remote] --delete [tag]
## push tag
git push [remote] [tag]
git push [remote] --tags