Git总结

本文最后更新于:1 年前

Git总结

Git学习参考:

https://git-scm.com/book/zh/v2

Git分区

git分区

一、Git操作本地项目

1. 初始化仓库

先进入项目文件夹,通过命令 git init 把这个目录变成git可以管理的仓库

1
git init

2. 查看仓库状态

1
git status

3. 把更新的文件添加到版本库中

使用命令 git add .添加到暂存区里面去,不要忘记后面的小数点 “.”,意为添加文件夹下的所有文件

1
git add .

4. 提交更新

用命令 git commit告诉Git,把文件提交到仓库。引号内为提交说明。

1
git commit -m 'first commit'

5. 查看提交记录

1
2
3
git log
git log -p <file>
git blame <file>

6. commit之前查看更新内容,文件差异

1
git diff

二、Git分支管理

1. Git查看分支

参考:

git同步远程分支 https://blog.csdn.net/u010227042/article/details/114684601

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ git branch -a
* main
master
remotes/origin/main

git branch -a #查看本地及远程的所有分支
git branch -r #查看远程所有分支
git branch #查看本地所有分支
git fetch #将某个远程主机的更新,全部取回本地:
git checkout 分支 #切换分支:
git push origin -d 分支名 #删除远程分支:
git branch -d 分支名 #删除本地分支
git remote show origin #查看远程分支和本地分支的对应关系
git remote prune origin #删除远程已经删除过的分支

2. 查看每一个分支的最后一次提交

1
2
3
4
$ git branch -v
iss53 93b412c fix javascript issue
* master 7a98805 Merge branch 'iss53'
testing 782fd34 add scott to the author list in the readmes

3. Git创建切换分支

创建本地分支,并切换到分支:

1
2
3
4
5
6
7
8
9
10
11
12
# 原文链接:Git-分支-分支的新建与合并
# https://git-scm.com/book/zh/v2/Git-%E5%88%86%E6%94%AF-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6

# 新建与合并
$ git checkout -b iss53
Switched to a new branch "iss53"

# 它是下面两条命令的简写:
# 创建分支
$ git branch iss53
# 切换分支
$ git checkout iss53

4. 删除分支:

(1) 查看所有分支

1
2
3
4
$ git branch -a
* main
master
remotes/origin/main

(2) 删除本地分支

1
2
3
4
5
git branch -D BranchName

其中-D也可以是--delete,如:

git branch --delete BranchName

(3) 删除本地的远程分支(解除关联)

1
2
3
4
5
6
# 先查看查看远程库信息
$ git remote -v
origin git@github.com:xxx/xxx.git (fetch)
origin git@github.com:xxx/xxx.git (push)
# 然后,根据名字删除,比如删除origin:
$ git remote rm origin

(4) 远程删除Git服务器上的分支

1
2
3
4
5
6
7
git push origin -d BranchName

其中-d也可以是--delete,如:

git push origin --delete BranchName

注意:git命令区分大小写,例如-D和-d在不同的地方虽然都是删除的意思,并且它们的完整写法都是--delete,但简易写法用错大小写会执行失败。

5. 修改分支名

1
2
3
4
5
6
7
8
9
参考:https://blog.csdn.net/weixin_42343307/article/details/125648147
1、修改本地分支名称
git branch -m oldBranchName newBranchName

2、将本地分支的远程分支删除
git push origin :oldBranchName

3、将改名后的本地分支推送到远程,并将本地分支与之关联
git push --set-upstream origin newBranchName

三、Git操作远程项目

1. Git关联远程项目

1
2
3
git remote add origin 你的远程库地址。
如:
git remote add origin https://github.com/xxx/xxx.git

注:“origin” 并无特殊含义

远程仓库名字 “origin” 与分支名字 “master” 一样,在 Git 中并没有任何特别的含义一样。 同时 “master” 是当你运行 git init 时默认的起始分支名字,原因仅仅是它的广泛使用, “origin” 是当你运行 git clone 时默认的远程仓库名字。 如果你运行 git clone -o booyah,那么你默认的远程分支名字将会是 booyah/master

2. Git获取远程库与本地同步合并

push之前先pull远程代码,如果远程库不为空必须做这一步,否则后面的提交会失败。

1
git pull --rebase origin develop

或者:

1
git pull origin develop

3. 把本地库的内容推送到远程

使用 git push命令,实际上是把当前分支develop推送到远程。第一次推送内容使用如下命令,执行此命令后会要求输入用户名、密码,验证通过后即开始上传。

1
git push -u origin develop

以后提交可以使用:

1
git push origin serverfix

这里有些工作被简化了。 Git 自动将 serverfix 分支名字展开为 refs/heads/serverfix:refs/heads/serverfix, 那意味着,“推送本地的 serverfix 分支来更新远程仓库上的 serverfix 分支。” 我们将会详细学习 Git 内部原理refs/heads/ 部分, 但是现在可以先把它放在儿。你也可以运行 git push origin serverfix:serverfix, 它会做同样的事——也就是说“推送本地的 serverfix 分支,将其作为远程仓库的 serverfix 分支” 可以通过这种格式来推送本地分支到一个命名不相同的远程分支。 如果并不想让远程仓库上的分支叫做 serverfix,可以运行 git push origin serverfix:awesomebranch 来将本地的 serverfix 分支推送到远程仓库上的 awesomebranch 分支。

四、Git Clone远程代码

1
2
3
git clone -b mutilrecall http://gitlab.avc.domain/ttengine/ttengine.git
或者:
git clone https://github.com/xxx/xxx.git

clone远程仓库到制定目录:

1
git clone xxx.git "指定目录"

五、Git合并分支

1
2
3
4
5
6
7
8
9
# 查看分支
$ git branch
* master
newtest
# 合并某分支到当前分支:git merge <name>
$ git merge newtest
# 合并完之后就可以删除掉不需要的分支了
$ git branch -d newtest
Deleted branch newtest (was c1501a2).

1.查看分支

$ git branch

  • master
    newtest

2.合并某分支到当前分支:git merge

$ git merge newtest

3.合并完之后就可以删除掉不需要的分支了

$ git branch -d newtest
Deleted branch newtest (was c1501a2).

4.关于merge回滚操作参考:

https://blog.csdn.net/adsl624153/article/details/81155664

5.执行完merge操作后,没有修改代码

⑴ 查看merge操作的上一个提交记录的版本号

git reflog

⑵ 回滚到merge之前的状态

git reset –hard 版本号

参考:https://blog.csdn.net/songxi_bo/article/details/123384233

回退命令:
$ git reset –hard HEAD^ 回退到上个版本
$ git reset –hard HEAD~3 回退到前3次提交之前,以此类推,回退到n次提交之前
$ git reset –hard commit_id 退到/进到 指定commit的sha码

强推到远程:

$ git push origin HEAD –force

6.git将某分支的某次提交合并到另一分支

https://www.cnblogs.com/myitnews/p/12363154.html

1) 首先,切换到develop分支

git log 命令,查找需要合并的commit记录,比如commitID:7fcb3defff

1
2
3
# 切换分支
$ git checkout master
$ git log

2) 然后,切换到master分支,

使用 git cherry-pick 7fcb3defff 命令,就把该条commit记录合并到了master分支,这只是在本地合并到了master分支

1
2
3
# 切换分支
$ git checkout master
$ git cherry-pick 7fcb3defff

3) 最后,git push 提交到master远程

(可以用命令也可以用工具的push操作),至此,就把develop分支的这条commit所涉及的更改合并到了master分支。

1
$ git push -u origin master

7.git 同步远程仓库远程分支至本地

https://blog.csdn.net/qq_21398131/article/details/131048478

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1、查看分支
git branch -a

* master
remotes/origin/HEAD -> origin/master
remotes/origin/last_hard
remotes/origin/master

2.将本地分支与远程保持同步
git fetch

3.新建本地分支,名称与远程仓库分支名称一致,并同步远程分支
git checkout -b last_hard origin/last_hard

4.同步所有远程分支(不需要)
git branch -r | grep -v ‘->’ | while read remote; do git branch --track “KaTeX parse error: Expected '}', got '#' at position 8: {remote#?origin/}" "remote”; done

5.将本地所有分支与远程保持同步
git fetch --all

6.最后拉取所有分支代码
git pull --all

六、Git 回滚操作

参考:https://www.jianshu.com/p/c55958563f5a

Git撤销&回滚操作(git reset 和 get revert)
Git 的工作流
工作区:在 git add xx 之前,在自己当前分支所修改的代码内容!
暂存区:已经 git add xxx 进去,且没有执行 git commit xxx 的。
本地分支:已经 git commit -m xxx 提交到本地分支的。
远程分支:git push origin HEAD:refs/for/master HEAD 是本地,master是远程分支

代码回滚
在上传代码到远程仓库的时候,不免会出现问题,任何过程都有可能要回滚代码:

1、在工作区的代码(checkout ~ 修改工作区文件)
git checkout – a.txt # 丢弃某个文件,或者
git checkout – . # 丢弃全部

注意:git checkout – . 丢弃全部,也包括:新增的文件会被删除、删除的文件会恢复回来、修改的文件会回去。这几个前提都说的是,回到暂存区之前的样子。对之前保存在暂存区里的代码不会有任何影响。对commit提交到本地分支的代码就更没影响了。当然,如果你之前压根都没有暂存或commit,那就是回到你上次pull下来的样子了。

2、代码 git add 到缓存区,并未 git commit 提交(reset ~ 修改暂存区文件)
git reset HEAD .
git reset HEAD a.txt

注意:这个命令仅改变暂存区,并不改变工作区,这意味着在无任何其他操作的情况下,
工作区中的实际文件同该命令运行之前 无任何变化

3、代码 git commit 到本地分支,但没有 git push 到远程 (git reset –hard ~ commit 之后)
git log # 得到你需要回退一次提交的commit id
git reset –hard # 回到其中你想要的某个版本
git reset –hard HEAD^ # 回到最新的一次提交
git reset HEAD^ # 此时代码保留,回到 git add 之前

4、代码 git push 把修改提交到远程仓库 (git reset || git revert)
(1)通过git reset是直接删除指定的commit
git log # 得到你需要回退一次提交的commit id
git reset –hard
git push origin HEAD –force # 强制提交一次,之前错误的提交就从远程仓库删除

(2) 通过git revert是用一次新的commit来回滚之前的commit
git log # 得到你需要回退一次提交的commit id
git revert # 撤销指定的版本,撤销也会作为一次提交进行保存

(3) git revert 和 git reset的区别
(a). git revert是用一次新的commit来回滚之前的commit,此次提交之前的commit都会被保留( 会有 两次 commit id);
(b). git reset是回到某次提交,提交及之前的commit都会被保留,但是此commit id之后的修改都会被删除 ( 只有一次 commit id)

作者:画圆
链接:https://www.jianshu.com/p/c55958563f5a
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

七、git回滚操作工作流

  1. 回滚至之前的版本
1
2
$ git log
$ git reset --hard b23f8e17aaa24f6ae95b6c333f06d9261df2525e
  1. 回滚至之后的版本
1
2
3
4
5
6
7
8
9
10
$ git reflog  # 显示之前操作
b23f8e1 (HEAD -> master) HEAD@{0}: reset: moving to b23f8e17aaa24f6ae95b6c333f06d9261df2525e
ef26ac6 HEAD@{1}: reset: moving to ef26ac6324424dbb549bc6a30866312b96679558
4e2bb67 HEAD@{2}: commit: add 约吗模块
b23f8e1 (HEAD -> master) HEAD@{3}: commit: add 短视频模块
ef26ac6 HEAD@{4}: commit: v2
7111058 HEAD@{5}: commit (initial): v1

# 回到某个版本
$ git reset --hard 4e2bb67

八、标签

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
chris@DESKTOP-26G0Q2K MINGW64 /e/gitrepos/person (main)
$ git tag -a v1 -m "第一版"

chris@DESKTOP-26G0Q2K MINGW64 /e/gitrepos/person (main)
$ git log
commit 744b74ad6d2c092b9de5ae15281f41090ffa1b6b (HEAD -> main, tag: v1, origin/main)
Author: 小可爱的书包 <42034225+Chris-z-su@users.noreply.github.com>
Date: Wed Jul 27 21:45:44 2022 +0800

add README.md

commit 3e5286d0ef5a8f3d83a9b306f0ea38599118511c
Author: chris <chris_z_su@163.com>
Date: Wed Jul 27 21:34:24 2022 +0800

init project...

chris@DESKTOP-26G0Q2K MINGW64 /e/gitrepos/person (main)
$ git push origin --tags
Enumerating objects: 1, done.
Counting objects: 100% (1/1), done.
Writing objects: 100% (1/1), 164 bytes | 164.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:gitstudy-org/person.git
* [new tag] v1 -> v1

chris@DESKTOP-26G0Q2K MINGW64 /e/gitrepos/person (main)
$

九、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
# 1. 切换到 main 分支
git checkout main

# 2. 确保 main 分支是最新的
git pull origin main

# 3. 创建一个新的临时分支
git checkout -b temp-branch

# 4. 从 dev 分支 cherry-pick c、e、f 提交
git cherry-pick <hash_of_commit_c>
git cherry-pick <hash_of_commit_e>
git cherry-pick <hash_of_commit_f>

# 5. 压缩(squash)这三个提交为一个提交
git rebase -i HEAD~3

# 在编辑器中将第二行和第三行的 pick 改为 squash,然后保存并退出
pick <hash_of_commit_c> Commit message for c
squash <hash_of_commit_e> Commit message for e
squash <hash_of_commit_f> Commit message for f

# 6. 切换回 main 分支并 cherry-pick 合并后的提交
git checkout main
git log temp-branch # 查找合并后的提交哈希值
git cherry-pick <merged_commit_hash>

# 7. 推送更新后的 main 分支到远程仓库
git push origin main

# 8. 清理临时分支
git branch -D temp-branch

9.1 git rebase

git rebase –continue

git rebase -i HEAD~3

9.2 git cherry-pick

git cherry-pick 7dfa6ce 6307d77 61481c1 9dfb79d 56de05f 报错

1
2
3
4
5
PS E:\gitrepos\myblog> git cherry-pick 7dfa6ce 6307d77 61481c1 9dfb79d 56de05f
error: cherry-pick is already in progress
hint: try "git cherry-pick (--continue | --abort | --quit)"
fatal: cherry-pick failed
PS E:\gitrepos\myblog>

解决:

git cherry-pick拆开多次执行

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
PS E:\gitrepos\myblog> git cherry-pick 7dfa6ce
[temp-branch 130e0e3] 需求2:提交代码
Date: Sun May 19 19:17:42 2024 +0800
1 file changed, 29 insertions(+)
create mode 100644 2024051902.tx
PS E:\gitrepos\myblog> git cherry-pick 6307d77
[temp-branch d5be0ce] 需求2:更新1
Date: Sun May 19 19:22:47 2024 +0800
1 file changed, 1 insertion(+), 1 deletion(-)
PS E:\gitrepos\myblog> git cherry-pick 61481c1
[temp-branch 7a3ff39] 需求2:更新2
Date: Sun May 19 19:22:59 2024 +0800
1 file changed, 1 insertion(+)
PS E:\gitrepos\myblog> git cherry-pick 9dfb79d
[temp-branch 165c609] 需求2:更新3
Date: Sun May 19 19:25:01 2024 +0800
1 file changed, 1 deletion(-)
PS E:\gitrepos\myblog> git cherry-pick 56de05f
[temp-branch b606d63] 需求2:更新4
Date: Sun May 19 19:25:41 2024 +0800
1 file changed, 3 insertions(+)
PS E:\gitrepos\myblog>







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
pick 130e0e3 需求2:提交代码
s d5be0ce 需求2:更新1
s 7a3ff39 需求2:更新2
s 165c609 需求2:更新3
s b606d63 需求2:更新4

# Rebase 5250f3e..b606d63 onto 5250f3e (5 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
# commit's log message, unless -C is used, in which case
# keep only this commit's message; -c is same as -C but
# opens the editor
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# create a merge commit using the original merge commit's
# message (or the oneline, if no original merge commit was
# specified); use -c <commit> to reword the commit message
# u, update-ref <ref> = track a placeholder for the <ref> to be updated
# to this position in the new commits. The <ref> is
# updated at the end of the rebase
#
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.

九十、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
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
chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (master)
$ git log
commit 4e2bb67211b05c54be839106a024ae6974439d21 (HEAD -> master)
Author: chris <chris_z_su@163.com>
Date: Wed Jul 13 20:55:29 2022 +0800

add 约吗模块

commit b23f8e17aaa24f6ae95b6c333f06d9261df2525e
Author: chris <chris_z_su@163.com>
Date: Wed Jul 13 20:53:24 2022 +0800

add 短视频模块

commit ef26ac6324424dbb549bc6a30866312b96679558
Author: chris <chris_z_su@163.com>
Date: Wed Jul 13 20:20:34 2022 +0800

v2

commit 7111058e366216ad19018a09c8ac4d4438867098
Author: chris <chris_z_su@163.com>
Date: Wed Jul 13 20:18:53 2022 +0800

v1

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (master)
$ git branch -a
* master

# 创建dev分支
chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (master)
$ git branch dev

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (master)
$ git branch -a
dev
* master

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (master)
$ git checkout dev
Switched to branch 'dev'


# 在dev分支的正常开发
chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (dev)
$ git status
On branch dev
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: index.html

no changes added to commit (use "git add" and/or "git commit -a")

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (dev)
$ git add .

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (dev)
$ git status
On branch dev
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: index.html


# 提交未开发完成的部分代码
chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (dev)
$ git commit -m "add shangcheng 50%"
[dev 3c134b7] add shangcheng 50%
1 file changed, 1 insertion(+)

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (dev)
$ git status
On branch dev
nothing to commit, working tree clean

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (dev)
$

# 切换到主分支,用于修复bug
chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (dev)
$ git checkout master
Switched to branch 'master'

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (master)
$

# 新建bug001分支,紧急修复生产bug
chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (master)
$ git branch bug001

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (master)
$ git branch
bug001
dev
* master

# 切换到bug001分支
chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (master)
$ git checkout bug001
Switched to branch 'bug001'

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (bug001)
$ git status
On branch bug001
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: index.html

no changes added to commit (use "git add" and/or "git commit -a")

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (bug001)
$ git add .

# 修复完bug,提交代码到bug分支
chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (bug001)
$ git commit -m "修复bug"
[bug001 23b0c49] 修复bug
1 file changed, 1 insertion(+), 1 deletion(-)

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (bug001)
$ git status
On branch bug001
nothing to commit, working tree clean

# 切换到主分支,进行合并bug分支的代码
chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (bug001)
$ git checkout master
Switched to branch 'master'

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (master)
$ git branch
bug001
dev
* master

# 合并bug001分支的代码到master(当前)主分支
chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (master)
$ git merge bug001
Updating 4e2bb67..23b0c49
Fast-forward
index.html | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (master)
$ git log
commit 23b0c49c245101dcba6b3e274b1a62239492104b (HEAD -> master, bug001)
Author: chris <chris_z_su@163.com>
Date: Sat Jul 16 18:55:22 2022 +0800

修复bug

commit 4e2bb67211b05c54be839106a024ae6974439d21
Author: chris <chris_z_su@163.com>
Date: Wed Jul 13 20:55:29 2022 +0800

add 约吗模块

commit b23f8e17aaa24f6ae95b6c333f06d9261df2525e
Author: chris <chris_z_su@163.com>
Date: Wed Jul 13 20:53:24 2022 +0800

add 短视频模块

commit ef26ac6324424dbb549bc6a30866312b96679558
Author: chris <chris_z_su@163.com>
Date: Wed Jul 13 20:20:34 2022 +0800

v2

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (master)
$ git branch
bug001
dev
* master

# bug修复完成后,删除掉不再使用的分支
chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (master)
$ git branch -d bug001
Deleted branch bug001 (was 23b0c49).

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (master)
$ git branch
dev
* master

# 切换到dev分支继续完成开发工作
chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (master)
$ git checkout dev
Switched to branch 'dev'

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (dev)
$ git status
On branch dev
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: index.html

no changes added to commit (use "git add" and/or "git commit -a")

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (dev)
$ git add .

# dev分支的工作完成,提交代码
chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (dev)
$ git commit -m "add shangcheng 100%"
[dev f77d471] add shangcheng 100%
1 file changed, 1 insertion(+), 1 deletion(-)

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (dev)
$ git status
On branch dev
nothing to commit, working tree clean

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (dev)
$ git log
commit f77d4710c3fb6d6618255ac24afeee5039448743 (HEAD -> dev)
Author: chris <chris_z_su@163.com>
Date: Sat Jul 16 19:01:44 2022 +0800

add shangcheng 100%

commit 3c134b7b96cdce6a27cde78ffb96316a1e9d6ede
Author: chris <chris_z_su@163.com>
Date: Sat Jul 16 18:52:34 2022 +0800

add shangcheng 50%

commit 4e2bb67211b05c54be839106a024ae6974439d21
Author: chris <chris_z_su@163.com>
Date: Wed Jul 13 20:55:29 2022 +0800

add 约吗模块

commit b23f8e17aaa24f6ae95b6c333f06d9261df2525e
Author: chris <chris_z_su@163.com>
Date: Wed Jul 13 20:53:24 2022 +0800

add 短视频模块

# 切换到master分支,进行合并dev分支的代码
chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (dev)
$ git checkout master
Switched to branch 'master'

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (master)
$ git branch
dev
* master

# 合并dev分支的代码到master(当前)分支
# 合并出现冲突,需要手动修改文件解决冲突
chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (master)
$ git merge dev
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (master|MERGING)
$ git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)

Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: index.html

no changes added to commit (use "git add" and/or "git commit -a")

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (master|MERGING)
$ git add .

# 解决完冲突后,提交代码
chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (master|MERGING)
$ git commit -m "解决bug和新功能合并的冲突问题"
[master 71f0fa7] 解决bug和新功能合并的冲突问题

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (master)
$ git status
On branch master
nothing to commit, working tree clean

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (master)
$ git log
commit 71f0fa7c950d1d8215156719f120291a64e80bba (HEAD -> master)
Merge: 23b0c49 f77d471
Author: chris <chris_z_su@163.com>
Date: Sat Jul 16 19:07:05 2022 +0800

解决bug和新功能合并的冲突问题

commit f77d4710c3fb6d6618255ac24afeee5039448743 (dev)
Author: chris <chris_z_su@163.com>
Date: Sat Jul 16 19:01:44 2022 +0800

add shangcheng 100%

commit 23b0c49c245101dcba6b3e274b1a62239492104b
Author: chris <chris_z_su@163.com>
Date: Sat Jul 16 18:55:22 2022 +0800

修复bug

commit 3c134b7b96cdce6a27cde78ffb96316a1e9d6ede
Author: chris <chris_z_su@163.com>
Date: Sat Jul 16 18:52:34 2022 +0800


chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (master)
$


################################################################
# Git操作远程仓库
# 将本地仓库连接到远程仓库
# origin表示远程仓库的别名
chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (master)
$ git remote add origin git@github.com:Chris-z-su/myblog.git

# 将本地的master的分支推送到远程的main分支,这里推送失败是由于本地和远程的分支名不一致(?可能)
# 这里是简化了的命令,原始命令是:git push origin serverfix:awesomebranch,来将本地的serverfix分支推送到远程仓库上的awesomebranch分支
# 如果只是 git push -u origin main,那么就是将本地的main推送到远程仓库的main分支,但是本地没有main分支,所以会报错?
chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (master)
$ git push -u origin main
error: src refspec main does not match any
error: failed to push some refs to 'github.com:Chris-z-su/myblog.git'

# 将本地的master分支名修改为main
chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (master)
$ git branch -M main

# 推送
chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (main)
$ git push -u origin main
Enumerating objects: 31, done.
Counting objects: 100% (31/31), done.
Delta compression using up to 8 threads
Compressing objects: 100% (29/29), done.
Writing objects: 100% (31/31), 3.61 KiB | 923.00 KiB/s, done.
Total 31 (delta 8), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (8/8), done.
To github.com:Chris-z-su/myblog.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.

# 将本地的dev分支推送到远程仓库
chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (main)
$ git push -u origin dev
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: Create a pull request for 'dev' on GitHub by visiting:
remote: https://github.com/Chris-z-su/myblog/pull/new/dev
remote:
To github.com:Chris-z-su/myblog.git
* [new branch] dev -> dev
branch 'dev' set up to track 'origin/dev'.

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (main)
$


研发完毕,上线

1
2
3
4
5
6
7
8
9
1. 将dev分支合并到master分支,进行上线
git checkout master
git merge dev
git push origin master

2. 把dev分支也推送到远程dev分支
git checkout dev
git merge master
git push origin dev

补充:

1
2
3
4
5
6
7
8
9
# 从远程仓库拉取代码到本地
git pull origin dev

# 相当于:
git fetch origin dev + git merge origin/dev


# 替换为rebase:
git fetch origin dev + git rebase origin/dev

1. git rebase第一种情形

1
2
3
4
5
6
$ git rebase -i HEAD~3  # 合并当前记录往前的三条记录

$ git rebase -i 版本号 # 合并当前记录到指定版本号的记录

# 执行之后,在编辑页面,修改 三条记录中的最后两个,pick --> s,表示后两条记录都合并第一条记录,保存并退出。
# 进入第二个编辑页面,修改记录提交log,修改为最终要输出的log,保存并退出。

注意:不要rebase已经提交到远程仓库中的记录!!!只合并本地commit的记录,不合并已经push的记录。

参考:https://blog.csdn.net/small_white_123/article/details/121563248

img

示例:

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
chris@DESKTOP-26G0Q2K MINGW64 /e/study/Git/pro_rebase (master)
$ git log
commit 1bc5766ebe804641542df9a84785d59611e1a352 (HEAD -> master)
Author: chris <chris_z_su@163.com>
Date: Sun Jul 17 18:40:17 2022 +0800

v4

commit a18b6bcee563ed169b180554fdb1ee56a4a78cfc
Author: chris <chris_z_su@163.com>
Date: Sun Jul 17 18:39:53 2022 +0800

v3

commit 61caa11b91b00342969f3d9d0365e14b09f6eb51
Author: chris <chris_z_su@163.com>
Date: Sun Jul 17 18:39:22 2022 +0800

v2

commit 10abab45ebd5e9cc54bb6a135519588c7697fbc1
Author: chris <chris_z_su@163.com>
Date: Sun Jul 17 18:38:49 2022 +0800

v1

chris@DESKTOP-26G0Q2K MINGW64 /e/study/Git/pro_rebase (master)
$ git rebase -i HEAD~3
[detached HEAD b21df84] v2 & v3 & v4
Date: Sun Jul 17 18:39:22 2022 +0800
3 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 2.py
create mode 100644 3.py
create mode 100644 4.py
Successfully rebased and updated refs/heads/master.

chris@DESKTOP-26G0Q2K MINGW64 /e/study/Git/pro_rebase (master)
$ git log
commit b21df84899c1fb05d232ba9349440eb5ac14a2e2 (HEAD -> master)
Author: chris <chris_z_su@163.com>
Date: Sun Jul 17 18:39:22 2022 +0800

v2 & v3 & v4

commit 10abab45ebd5e9cc54bb6a135519588c7697fbc1
Author: chris <chris_z_su@163.com>
Date: Sun Jul 17 18:38:49 2022 +0800

v1

chris@DESKTOP-26G0Q2K MINGW64 /e/study/Git/pro_rebase (master)
$

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
git log --graph  # 以流程的样式显示log日志

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (main)
$ git log --graph
* commit b21a68988b6b78e4bfa15964b1cc6b3b1cb89769 (HEAD -> main, origin/main, origin/dev, dev)
| Author: chris <chris_z_su@163.com>
| Date: Sat Jul 16 21:43:39 2022 +0800
|
| 开发功能2,开发完成
|
* commit 7eea26214a82d361a17f107853833c2c0da31dc9
| Author: chris <chris_z_su@163.com>
| Date: Sat Jul 16 21:42:39 2022 +0800
|
| 开发功能1
|
* commit 989b6ebec2368c4ac1843dc54dfcfc3b09f842cc
|\ Merge: 71f0fa7 25166ba
| | Author: chris <chris_z_su@163.com>
| | Date: Sat Jul 16 19:49:23 2022 +0800
| |
| | Merge branch 'dev'
| |
| * commit 25166bae4ce37818fd2153422acec3302f48e041
| | Author: chris <chris_z_su@163.com>
| | Date: Sat Jul 16 19:48:50 2022 +0800
| |
| | update README
| |
* | commit 71f0fa7c950d1d8215156719f120291a64e80bba
|\| Merge: 23b0c49 f77d471
| | Author: chris <chris_z_su@163.com>
| | Date: Sat Jul 16 19:07:05 2022 +0800
| |
| | 解决bug和新功能合并的冲突问题
| |
| * commit f77d4710c3fb6d6618255ac24afeee5039448743
| | Author: chris <chris_z_su@163.com>
| | Date: Sat Jul 16 19:01:44 2022 +0800
| |
| | add shangcheng 100%
| |
| * commit 3c134b7b96cdce6a27cde78ffb96316a1e9d6ede
| | Author: chris <chris_z_su@163.com>
| | Date: Sat Jul 16 18:52:34 2022 +0800
| |
| | add shangcheng 50%
| |
* | commit 23b0c49c245101dcba6b3e274b1a62239492104b
|/ Author: chris <chris_z_su@163.com>
| Date: Sat Jul 16 18:55:22 2022 +0800
|
| 修复bug
|
* commit 4e2bb67211b05c54be839106a024ae6974439d21
| Author: chris <chris_z_su@163.com>
| Date: Wed Jul 13 20:55:29 2022 +0800
|
| add 约吗模块
|
* commit b23f8e17aaa24f6ae95b6c333f06d9261df2525e
| Author: chris <chris_z_su@163.com>
| Date: Wed Jul 13 20:53:24 2022 +0800
|
| add 短视频模块
|
* commit ef26ac6324424dbb549bc6a30866312b96679558
| Author: chris <chris_z_su@163.com>
| Date: Wed Jul 13 20:20:34 2022 +0800
|
| v2
|
* commit 7111058e366216ad19018a09c8ac4d4438867098
Author: chris <chris_z_su@163.com>
Date: Wed Jul 13 20:18:53 2022 +0800

v1

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (main)
$



# 格式化简洁显示
chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (main)
$ git log --graph --pretty=format:"%h %s"
* b21a689 开发功能2,开发完成
* 7eea262 开发功能1
* 989b6eb Merge branch 'dev'
|\
| * 25166ba update README
* | 71f0fa7 解决bug和新功能合并的冲突问题
|\|
| * f77d471 add shangcheng 100%
| * 3c134b7 add shangcheng 50%
* | 23b0c49 修复bug
|/
* 4e2bb67 add 约吗模块
* b23f8e1 add 短视频模块
* ef26ac6 v2
* 7111058 v1

chris@DESKTOP-26G0Q2K MINGW64 /e/study/myblog (main)
$

image-20220717190615276

2. git rebase第二种情形

先git rebase master,后git merge dev

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
chris@DESKTOP-26G0Q2K MINGW64 /e/study/Git/pro_rebase (dev)
$ touch dev1.py

chris@DESKTOP-26G0Q2K MINGW64 /e/study/Git/pro_rebase (dev)
$ git add .

chris@DESKTOP-26G0Q2K MINGW64 /e/study/Git/pro_rebase (dev)
$ git commit -m "dev branch commit 1"
[dev 4415ff1] dev branch commit 1
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 dev1.py

chris@DESKTOP-26G0Q2K MINGW64 /e/study/Git/pro_rebase (dev)
$ git checkout master
Switched to branch 'master'

chris@DESKTOP-26G0Q2K MINGW64 /e/study/Git/pro_rebase (master)
$ touch master1.py

chris@DESKTOP-26G0Q2K MINGW64 /e/study/Git/pro_rebase (master)
$ git add .

chris@DESKTOP-26G0Q2K MINGW64 /e/study/Git/pro_rebase (master)
$ git commit -m "master 1"
[master ae53d98] master 1
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 master1.py

chris@DESKTOP-26G0Q2K MINGW64 /e/study/Git/pro_rebase (master)
$ git checkout dev
Switched to branch 'dev'

chris@DESKTOP-26G0Q2K MINGW64 /e/study/Git/pro_rebase (dev)
$ git rebase master
Successfully rebased and updated refs/heads/dev.

chris@DESKTOP-26G0Q2K MINGW64 /e/study/Git/pro_rebase (dev)
$ git checkout master
Switched to branch 'master'

chris@DESKTOP-26G0Q2K MINGW64 /e/study/Git/pro_rebase (master)
$ git merge dev
Updating ae53d98..a3ed5bf
Fast-forward
dev1.py | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 dev1.py

chris@DESKTOP-26G0Q2K MINGW64 /e/study/Git/pro_rebase (master)
$ git log --graph --pretty=format:"%h %s"
* a3ed5bf dev branch commit 1
* ae53d98 master 1
* a3ddbf3 Merge branch 'dev'
|\
| * 0873af3 dev branch
* | e3c346c master function
|/
* b21df84 v2 & v3 & v4
* 10abab4 v1

chris@DESKTOP-26G0Q2K MINGW64 /e/study/Git/pro_rebase (master)
$

3. git rebase第三种情形

image-20220717214824576

1
2
3
4
5
# 从远程仓库拉取代码到本地
git pull origin dev

# 替换为rebase:
git fetch origin dev + git rebase origin/dev

4. git rebase时发生冲突

git rebase时发生冲突:

① 解决冲突,手动修改冲突的文件内容

② git add .

③ git rebase –continue

5. git配置beyond compare4作为比较工具

1
2
3
4
5
6
7
8
# 配置
git config --local merge.tool bc3
git config --local mergetool.path "/usr/local/bin/bcomp"
git config --local mergetool.keepBackup false

# 使用
git mergetool

Windows手动修改

https://blog.csdn.net/sanyecao502/article/details/83352169

1.配置“.gitconfig”文件
首先我们找到Git的配置文件“.gitconfig”,我的文件在路径“Administrator/.gitconfig”下。使用使用文本编辑工具打开后添加下图文本,其中的cmd一栏写的是你电脑上“beyond compare4”所在的路径。这样我们就配置成功啦!

image-20220717222616729

2.命令调用
配置好后我们迫不及待的想要验证一下成果,我们切到Git仓库下,更改一下文件然后先使用Git默认的文本差异工具看看效果,下图,我们输入命令“git diff”后,git默认的比较工具显示出差异信息(很强大,但不够直观):

image-20220717222628531

我们再来看看调用“beyond compare4”进行文本差异比较的结果,我们首先输入命令“git difftool readme.txt”是比较工作区和暂存区中“readme.txt”的差异,然后他会询问你是否打开‘bc4’,输入y就好。(若是输入这一命令后没有任何反应,有可能是工作区和暂存区的内容是一致的,毕竟UNIX的哲学是没有消息就是最好的消息,所以你可以尝试修改一下文件,再尝试)

image-20220717222641345

image-20220717222703789

九十九、错误解决:

1、执行git命令时出现fatal: ‘origin’ does not appear to be a git repository错误

在执行git pull origin master时出现:
  fatal: ‘origin’ does not appear to be a git repository
  fatal: Could not read from remote repository.
  Please make sure you have the correct access rights and the repository exists

解决方案:

1
2
3
git remote add origin git@github:bx_reader/bx-reader-api.git

将关联远程仓库为origin

2、Reinitialized existing Git repository

“git init” 的时候出现:Reinitialized existing Git repository
解决方法:

1
可以使用 rm -rf .git,删除.git,然后在 git init 即可

3、refusing to merge unrelated histories

“git pull origin master ” 的时候出现:refusing to merge unrelated histories

解决方法:

1
2
3
可以在 “git pull origin master” 后添加 “--allow-unrelated-histories”
即整个命令行为:
git pull origin master --allow-unrelated-histories

如果你是git pull或者git push报fatal: refusing to merge unrelated histories
同理:
git pull origin master –allow-unrelated-histories
等等,就是这样完美的解决咯!

4. Git冲突:Please commit your changes or stash them before you merge

参考: https://blog.csdn.net/DDD4V/article/details/118896307

用git pull来更新代码的时候,遇到了下面的问题:

1
2
3
4
error: Your local changes to the following files would be overwritten by merge:
xxx/xxx/xxx.php
Please, commit your changes or stash them before you can merge.
Aborting

解决:

1
2
3
git stash
git pull
git stash pop

解释:

通过git stash将工作区恢复到上次提交的内容,同时备份本地所做的修改,之后就可以正常git pull了,git pull完成后,执行git stash pop将之前本地做的修改应用到当前工作区。

git stash: 备份当前的工作区的内容,从最近的一次提交中读取相关内容,让工作区保证和上次提交的内容一致。同时,将当前的工作区内容保存到Git栈中。

git stash pop: 从Git栈中读取最近一次保存的内容,恢复工作区的相关内容。由于可能存在多个Stash的内容,所以用栈来管理,pop会从最近的一个stash中读取内容并恢复。

git stash list: 显示Git栈内的所有备份,可以利用这个列表来决定从那个地方恢复。

git stash clear: 清空Git栈。此时使用gitg等图形化工具会发现,原来stash的哪些节点都消失了。


Git总结
https://chris-z-su.github.io/2020/07/11/git/Git总结/
作者
Chris
发布于
2020年7月11日
许可协议