Git 常用命令

git 官方文档
https://git-scm.com/book/zh/v2/

官方推荐书籍 progit_v2.1.36.pdf

github 搜索技巧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
proj_name  in:name                                                #项目名
proj_name in:name,description, #项目名 或描述
proj_name in:name,description,readme #项目名 或描述 或readme文件中
proj_name stars:>=9000 #星数大于9000
proj_name forks:>=9000 #fork数大于9000
proj_name forks:2000..5000 #fork数指定范围
proj_name forks:2000..5000 stars:>9000 #同时限定
stars:>2000 extension:c language:c #根据语言搜索项目
location:shanghai language:c #搜索某地区的人
awesome redis #搜索精品
awesome nginx #搜索精品
https://github.com/antirez/redis/blob/unstable/src/ae.c#L12 #高亮某一行
https://github.com/antirez/redis/blob/unstable/src/ae.c#L12-L23 #高亮某一片
在repo界面按下t键 #参考github快捷键

git config 配置

1
2
3
4
5
6
7
8
9
10
git config --local  对某个仓库
git config --global 当前用户的所有仓库
git config --system 当所有用户的所有仓库

git config --global user.name "user1"
git config --global user.email "123@123.com"
git config --global core.editor vim
git config --global color.ui true
git config --global --edit
git config --global --list

将本地项目与远程项目关联

1
2
3
4
5
6
7
chunli@ubuntu:~/git$ git init
chunli@ubuntu:~/git$ git remote add origin https://github.com/xxxx/xxxx.git #设置仓库关联
chunli@ubuntu:~/git$ git push --set-upstream origin master #设置分支关联
chunli@ubuntu:~/git$ git push -u origin master #设置分支关联,也可以这样写
chunli@ubuntu:~/git$ git add
chunli@ubuntu:~/git$ git commit
chunli@ubuntu:~/git$ git push

清理工作区

1
2
chunli@ubuntu:~$ git checkout  .  # 恢复被修改的文件
chunli@ubuntu:~$ git clean -d -f # 删除没有被版本控制的文件

git 文件管理操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

git init [dir] 目录项可选
git add file_1 将文件添加到暂存区
git reset HEAD file 将添加到缓存区的文件file从暂存区移除,所做的修改保留到本地

git add -u 将已经纳入管理的文件, 添加到暂缓区
git diff 默认比较文件, 也可以比较分支
git commit -m "log info" 创建提交
git commit -am "log info" 被管理的已修改的,等效于 git add xxx && git commit -m "xxxx"

git mv
git rm xxxx
git rm --cached xxx 从版本控制里删除,但不删除工作区
git status
git reset HEAD 回退到最新版本
git reset xxxx 回退到xx版本,不清空工作区
git reset --hard xxx 回退到xx版本,并清空工作区
git checkout -- files... 将工作区的文件还原
git checkout -- files... 将版本库的文件还原到暂存区,再到工作区(如果暂存区文件被reset)

git diff

1
2
3
4
git diff
chunli@ubuntu:~/git$ git diff #工作区与暂存区比较
chunli@ubuntu:~/git$ git diff HEAD #工作区与版本库比较
chunli@ubuntu:~/git$ git diff --cached #暂存区与版本库比较

工作区与暂存区 管理:

1
2
3
4
5
6
7
8
9
chunli@ubuntu:~/git$ echo "hello git" >> main.c
chunli@ubuntu:~/git$ git add main.c
chunli@ubuntu:~/git$ echo "hello git" >> main.c
chunli@ubuntu:~/git$ git add main.c
chunli@ubuntu:~/git$ echo "hello bash" >>
chunli@ubuntu:~/git$ git checkout -- main.c # 将暂存区覆盖到工作区, "hello bash" 被丢失
chunli@ubuntu:~/git$ git diff # 无反应
chunli@ubuntu:~/git$ git diff 分支名 # 可显示差异
chunli@ubuntu:~/git$ git reset HEAD main.c # 删除暂存区的内容,保留工作区

游离HEAD提交, 不创建分支

1
2
3
4
chunli@ubuntu:~/git$ git checkout  318ab8159a
chunli@ubuntu:~/git$ vim main.c
chunli@ubuntu:~/git$ git commit -am "detached HEAD "
chunli@ubuntu:~/git$ git checkout master

版本回退

1
2
3
4
5
6
chunli@ubuntu:~/git$ git reset --hard HEAD               #--hard 丢弃工作区内容
chunli@ubuntu:~/git$ git reset --hard HEAD^
chunli@ubuntu:~/git$ git reset --hard HEAD^^
chunli@ubuntu:~/git$ git reset --hard HEAD^^^
chunli@ubuntu:~/git$ git reset --hard HEAD~2
chunli@ubuntu:~/git$ git reset --hard xxxx

版本回退,溯源

版本回退,每个commit只能记住父ID, 不知道子ID,所以能往前退, 不能往后退。

1
2
3
4
5
chunli@ubuntu:~/git$ git reflog                  #根据操作日志,获取最新的提交在哪里
4e9f559 (HEAD -> master) HEAD@{0}: commit: 123
4d5788d HEAD@{1}: reset: moving to 4d5788d47b725f810d0e0c198f45dba972e3bd51
618097e (origin/master, origin/HEAD) HEAD@{2}: clone: from https://gitlab.li-chunli.top:20443/root/mycode.git
chunli@ubuntu:~/git/mycode$

查看日志

1
2
3
4
5
chunli@ubuntu:~/Git/$ git log
chunli@ubuntu:~/Git/$ git log --oneline
chunli@ubuntu:~/Git/$ git log -n 4
chunli@ubuntu:~/Git/$ git log --all 显示所有分支
chunli@ubuntu:~/Git/$ git log --graph

重要概念:

HEAD 指向分支
分支指向提交
提交是一个个的对象

分支的增删改查

1
2
3
4
5
6
7
8
9
10
11
12
13
chunli@ubuntu:~/Git$ git branch                     #查看分支
chunli@ubuntu:~/Git$ git branch bug_fix #创建分支
chunli@ubuntu:~/git$ git branch -m bug_fix dev #分支改名
chunli@ubuntu:~/Git$ git checkout bug_fix #切换到某分支
chunli@ubuntu:~/Git$ git checkout - #切换到最近操作的分支
chunli@ubuntu:~/Git$ git branch -d bug_fix #删除不需要的分支(没有提交文件,或内容已经合并)
chunli@ubuntu:~/Git$ git branch -D bug_fix #删除不需要的分支(有提交过文件)
chunli@ubuntu:~/Git$ git checkout -b bug_2 #创建并切换到新分支
chunli@ubuntu:~/Git$ git diff bug_2 #分支的比较
chunli@ubuntu:~/Git$ git merge bug_2 #其他合并到本分支
chunli@ubuntu:~/Git$ git branch -v #显示分支的最新的信息
chunli@ubuntu:~/Git$ git branch -av #显示分支的最新的信息
chunli@ubuntu:~/Git$ git branch -avv #显示分支的最新的信息

分支冲突,分支互相合并

演示:
master <– branch 合并冲突
dev <– mster fast-forward

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
显示分支的信息
chunli@ubuntu:~/Git$ git branch -v
bug_2 d9e51a6 new main
* master d9e51a6 new main


修改主分支的文件,手动产生一个冲突
chunli@ubuntu:~/Git$ cat main.c
#include <stdio.h>
int main()
{
return 0;
}
chunli@ubuntu:~/Git$ git add -u
chunli@ubuntu:~/Git$ git commit -m "master main.c"
[master 194c3a8] master main.c
1 file changed, 1 insertion(+), 1 deletion(-)
chunli@ubuntu:~/Git$


修改某分支的文件,手动产生一个冲突
chunli@ubuntu:~/Git$ git checkout bug_2
chunli@ubuntu:~/Git$ cat main.c
#include <stdio.h>
int main()
{
return 1;
}
chunli@ubuntu:~/Git$ git add -u
chunli@ubuntu:~/Git$ git commit -m "bug_2 main.c"
[bug_2 28b71ab] bug_2 main.c
1 file changed, 1 insertion(+), 1 deletion(-)
chunli@ubuntu:~/Git$
chunli@ubuntu:~/Git$


合并到主分支, 提示有冲突
chunli@ubuntu:~/Git$ git checkout master
chunli@ubuntu:~/Git$ git diff bug_2 #显示分支冲突
chunli@ubuntu:~/Git$ git merge bug_2 #合并完成
chunli@ubuntu:~/Git$ vim main.c #手动修改,解决冲突
chunli@ubuntu:~/Git$ git add main.c #add表示解决了冲突
chunli@ubuntu:~/Git$ git commit -m "master merge"



切换到分支, Fast-forward!!!
chunli@ubuntu:~/Git$ git checkout -
chunli@ubuntu:~/Git$ git merge master
Updating 28b71ab..a3f826f
Fast-forward
main.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
chunli@ubuntu:~/Git$

stash 保留工作区

stash 保留单个工作区

1
2
3
4
chunli@ubuntu:~/git$ git stash         #保留工作区到stash,工作区与暂存区恢复commit的状态
chunli@ubuntu:~/git$ git pop #恢复工作区并删除stash
chunli@ubuntu:~/git$ git stash list #列出保存的stash
chunli@ubuntu:~/git$ git stash drop #删除某个stash

stash 保留多个工作区(不常用)这多个都是基于同一个commit

1
2
3
4
5
6
7
8
chunli@ubuntu:~/git$ echo "void fun1(){}">> main.c
chunli@ubuntu:~/git$ git stash
chunli@ubuntu:~/git$ echo "void fun2(){}">> main.c
chunli@ubuntu:~/git$ git stash save "二改" #stash 可以有名字
chunli@ubuntu:~/git$ git stash list
stash@{0}: WIP on dev: e4b0319 3
stash@{1}: WIP on dev: e4b0319 3
chunli@ubuntu:~/git$ cat main.c #保存到stash后, 文件内容没有变化

恢复工作区

1
2
3
4
5
chunli@ubuntu:~/git$ git pop                     #恢复并删除stash
chunli@ubuntu:~/git$ git stash apply
chunli@ubuntu:~/git$ git stash apply stash@{1} #恢复指定stash,并不删除stash
chunli@ubuntu:~/git$ git stash drop
chunli@ubuntu:~/git$ git stash drop stash@{0}

标签(发布里程碑)

1
2
3
4
5
6
7
8
9
10
11
12
chunli@ubuntu:~/git$ git tag  ver1.0                   #创建轻量级标签
chunli@ubuntu:~/git$ git tag -a ver2.0 -m "里程碑1" #创建附注标签
chunli@ubuntu:~/git$ git tag #查看所有标签
chunli@ubuntu:~/git$ git tag -l "*1.0*" #查找标签
chunli@ubuntu:~/git$ git show ver1.0 #查看某个标签的详细信息
chunli@ubuntu:~/git$ git push origin <tagname> #推送某个标签
chunli@ubuntu:~/git$ git push origin --tags #推送所有标签
chunli@ubuntu:~/git$ git tag -d <tagname> #删除标签
chunli@ubuntu:~/git$ git push origin --delete <tagname>#删除远程标签
chunli@ubuntu:~/git$ git push origin :refs/tags/xxxx #删除远程标签
chunli@ubuntu:~/git$ git checkout 2.0.0 #检出标签
chunli@ubuntu:~/git$ git blame <files> #显示文件变更过程

查看远程分支信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
chunli@ubuntu:~/git$ git clone https://gitlab.li-chunli.top:20443/root/mycode.git
chunli@ubuntu:~/git$ cd mycode/
chunli@ubuntu:~/git/mycode$ git remote show
origin
chunli@ubuntu:~/git/mycode$ git remote show origin
* remote origin
Fetch URL: https://gitlab.li-chunli.top:20443/root/mycode.git
Push URL: https://gitlab.li-chunli.top:20443/root/mycode.git
HEAD branch: master
Remote branch:
master tracked
Local branch configured for 'git pull':
master merges with remote master
Local ref configured for 'git push':
master pushes to master (up to date)
chunli@ubuntu:~/git/mycode$


chunli@ubuntu:~/git/mycode$ echo "## 副标题" >> README.md
chunli@ubuntu:~/git/mycode$ git commit -am "## 副标题"
chunli@ubuntu:~/git/mycode$ git push
Everything up-to-date
chunli@ubuntu:~/git/mycode$

git pull,git push

1
2
3
4
5
chunli@ubuntu:~/git/mycode$ git  pull
Already up to date.
chunli@ubuntu:~/git/mycode$ git push
Everything up-to-date
chunli@ubuntu:~/git/mycode$

本地分支与远程分支的对应关系

1
2
3
4
5
6
7
8
chunli@ubuntu:~/git/mycode$ git branch -avv
* master 4e9f559 [origin/master: ahead 4] 123
remotes/origin/BUG_1 575f7d7 2
remotes/origin/HEAD -> origin/master
remotes/origin/bug_1 575f7d7 2
remotes/origin/master 618097e 删除 子模块
remotes/origin_cat/master 7a00051 Add new file
chunli@ubuntu:~/git/mycode$

远程冲突解决:

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

提交时 提示被拒绝
chunli@ubuntu:~/git/mycode_2$ git push
To ssh://gitlab.li-chunli.top:2022/root/mycode.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'ssh://git@gitlab.li-chunli.top:2022/root/mycode.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
chunli@ubuntu:~/git/mycode_2$

获取最新代码
chunli@ubuntu:~/git/mycode_2$ git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From ssh://gitlab.li-chunli.top:2022/root/mycode
4d69a3c..8ec8871 master -> origin/master
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
chunli@ubuntu:~/git/mycode_2$


手动合并
chunli@ubuntu:~/git/mycode_2$ vim README.md
chunli@ubuntu:~/git/mycode_2$ git commit -am "2"
[master 575f7d7] 2

推送
chunli@ubuntu:~/git/mycode_2$ git push
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 423 bytes | 423.00 KiB/s, done.
Total 4 (delta 1), reused 0 (delta 0)
To ssh://gitlab.li-chunli.top:2022/root/mycode.git
8ec8871..575f7d7 master -> master
chunli@ubuntu:~/git/mycode_2$



git pull == > git fetch + git merge

git 别名:

1
2
3
4
chunli@ubuntu:~/git/mycode$ git config --global  alias.AA "BB"
chunli@ubuntu:~/git/mycode$ git config --global alias.AB "BB CCC"
chunli@ubuntu:~/git/mycode$ git config --global --unset alias.AA
chunli@ubuntu:~/git/mycode$ git config --global --unset alias.AB

创建分支 并推送,拉取(旧方法)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
chunli@ubuntu:~/git/mycode$ git checkout -b  dev
chunli@ubuntu:~/git/mycode$ vim dev.txt
chunli@ubuntu:~/git/mycode$ git add dev.txt
chunli@ubuntu:~/git/mycode$ git commit -m "dev"
chunli@ubuntu:~/git/mycode$ git push --set-upstream origin dev
chunli@ubuntu:~/git/mycode$ git push #再不会提示出错
chunli@ubuntu:~/git/mycode$ git branch -av
* dev 7014e7b dev
master 575f7d7 2
remotes/origin/HEAD -> origin/master
remotes/origin/dev 7014e7b dev
remotes/origin/master 575f7d7 2
chunli@ubuntu:~/git/mycode$


另外一个人拉取代码,并追踪此分支
chunli@ubuntu:~/git/mycode_2$ git pull
chunli@ubuntu:~/git/mycode_2$ git branch -av
chunli@ubuntu:~/git/mycode_2$ git checkout -b dev origin/dev
chunli@ubuntu:~/git/mycode_2$

创建本地分支与远程分支不同名

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
chunli@ubuntu:~/git/mycode_3$ git  checkout -b bug_1
Switched to a new branch 'bug_1'
chunli@ubuntu:~/git/mycode_3$

chunli@ubuntu:~/git/mycode_3$ git push --set-upstream origin bug_1:BUG_1
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: To create a merge request for BUG_1, visit:
remote: https://gitlab.li-chunli.top:20443/root/mycode/-/merge_requests/new?merge_request%5Bsource_branch%5D=BUG_1
remote:
To https://gitlab.li-chunli.top:20443/root/mycode.git
* [new branch] bug_1 -> BUG_1
Branch 'bug_1' set up to track remote branch 'BUG_1' from 'origin'.
chunli@ubuntu:~/git/mycode_3$

此时 git push 会报错
chunli@ubuntu:~/git/mycode_3$ git push
fatal: The upstream branch of your current branch does not match
the name of your current branch. To push to the upstream branch
on the remote, use

git push origin HEAD:BUG_1

To push to the branch of the same name on the remote, use

git push origin bug_1

To choose either option permanently, see push.default in 'git help config'.
chunli@ubuntu:~/git/mycode_3$




查看分支
chunli@ubuntu:~/git/mycode_3$ git branch -avv
* bug_1 575f7d7 [origin/BUG_1] 2
master 575f7d7 [origin/master] 2
remotes/origin/BUG_1 575f7d7 2
remotes/origin/HEAD -> origin/master
remotes/origin/master 575f7d7 2
chunli@ubuntu:~/git/mycode_3$

正确的处理
chunli@ubuntu:~/git/mycode_3$ git push origin HEAD:BUG_1
Everything up-to-date
chunli@ubuntu:~/git/mycode_3$

chunli@ubuntu:~/git/mycode_3$ git push origin bug_1:BUG_1
Everything up-to-date
chunli@ubuntu:~/git/mycode_3$

创建分支 并推送,拉取(新方法)

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
unli@ubuntu:~/git/mycode$ git checkout -b  dev_2
chunli@ubuntu:~/git/mycode$ vim 123.txt
chunli@ubuntu:~/git/mycode$ git add 123.txt
chunli@ubuntu:~/git/mycode$ git commit -am "123"
chunli@ubuntu:~/git/mycode$ git push -u origin dev_2
chunli@ubuntu:~/git/mycode$

另外一个人拉取代码,并追踪此分支
chunli@ubuntu:~/git/mycode_2$ git pull
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From ssh://gitlab.li-chunli.top:2022/root/mycode
* [new branch] dev_2 -> origin/dev_2
Already up to date.
chunli@ubuntu:~/git/mycode_2$ git checkout --track origin/dev_2
Branch 'dev_2' set up to track remote branch 'dev_2' from 'origin'.
Switched to a new branch 'dev_2'
chunli@ubuntu:~/git/mycode_2$ git branch -av
dev 7014e7b dev
* dev_2 ca5dace 123
master 575f7d7 2
remotes/origin/HEAD -> origin/master
remotes/origin/dev 7014e7b dev
remotes/origin/dev_2 ca5dace 123
remotes/origin/master 575f7d7 2
chunli@ubuntu:~/git/mycode_2$

删除远程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

删除远程分支,方法1:
chunli@ubuntu:~/git/mycode_2$ git push origin :dev
chunli@ubuntu:~/git/mycode_2$


删除远程分支,方法2:
chunli@ubuntu:~/git/mycode_3$ git branch -avv
* master 575f7d7 [origin/master] 2
remotes/origin/HEAD -> origin/master
remotes/origin/branch_1 289a400 branch_1
remotes/origin/master 575f7d7 2
chunli@ubuntu:~/git/mycode_3$ git push origin --delete branch_1
To https://gitlab.li-chunli.top:20443/root/mycode.git
- [deleted] branch_1
chunli@ubuntu:~/git/mycode_3$ git branch -avv
* master 575f7d7 [origin/master] 2
remotes/origin/HEAD -> origin/master
remotes/origin/master 575f7d7 2
chunli@ubuntu:~/git/mycode_3$


远程分支删除了,本地分支同步一下
chunli@ubuntu:~/git/mycode_2$ git remote prune origin

git submodule 子模块

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
子模块-添加
chunli@ubuntu:~/git$ git clone https://gitlab.li-chunli.top:20443/root/mycode.git
chunli@ubuntu:~/git/mycode$ git submodule add https://gitlab.li-chunli.top:20443/root/mycat.git #添加子模块
chunli@ubuntu:~/git/mycode$ git status #提醒有文件需要提交
chunli@ubuntu:~/git/mycode$ git commit -m "添加 子模块"
chunli@ubuntu:~/git/mycode$ git push

子模块-更新

方法1:
到子模块目录执行: git pull

方法2:
chunli@ubuntu:~/git/mycode$ git submodule foreach git pull

更新之后, 提示有修改请提交
chunli@ubuntu:~/git/mycode$ git status
chunli@ubuntu:~/git/mycode$ git commit -am "更新 子模块"
chunli@ubuntu:~/git/mycode$ git push


子模块-克隆
方法1: 手动克隆
chunli@ubuntu:~/git$ git clone https://gitlab.li-chunli.top:20443/root/mycode.git
chunli@ubuntu:~/git/mycode$ ll mycat/ # 可以看到此时子模块内容是空的
chunli@ubuntu:~/git/mycode$ git submodule init
chunli@ubuntu:~/git/mycode$ git submodule update --recursive
chunli@ubuntu:~/git/mycode$ ll mycat/ # 可以看出内容已经完整

方法1: 递归克隆
chunli@ubuntu:~/git$ git clone --recurse-submodules https://gitlab.li-chunli.top:20443/root/mycode.git
chunli@ubuntu:~/git$ cd mycode/
chunli@ubuntu:~/git/mycode$ ll mycat/ # 可以看出内容已经完整
chunli@ubuntu:~/git/mycode$

子模块-删除
chunli@ubuntu:~/git/mycode$ git rm -rf mycat/
chunli@ubuntu:~/git/mycode$ git rm -rf .gitmodules
chunli@ubuntu:~/git/mycode$ git status # 提示两项被删除
chunli@ubuntu:~/git/mycode$ git commit -m "删除 子模块"
chunli@ubuntu:~/git/mycode$ git push
chunli@ubuntu:~/git/mycode$

git subtree 子模块

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

准备两个仓库, 将mycat放入mycode中:
https://gitlab.li-chunli.top:20443/root/mycode.git
https://gitlab.li-chunli.top:20443/root/mycat.git


添加 subtree --squash 避免日志污染
chunli@ubuntu:~/git$ git clone https://gitlab.li-chunli.top:20443/root/mycode.git
chunli@ubuntu:~/git$ cd mycode/
chunli@ubuntu:~/git/mycode$ git remote add origin_cat https://gitlab.li-chunli.top:20443/root/mycat.git $
chunli@ubuntu:~/git/mycode$ git subtree add --prefix=modules_cat origin_cat master #[添加] 拉取到本地
chunli@ubuntu:~/git/mycode$ git subtree add --prefix=modules_cat origin_cat master --squash #[添加] 也可以这样写 squash加不加,以后要一致
chunli@ubuntu:~/git/mycode$ git subtree add --prefix=modules_cat git_url master --squash #[添加] 也可以直接带上url,不推荐
chunli@ubuntu:~/git/mycode$ git subtree add -P modules_cat origin_cat master --squash #[添加] 也可以这样写 -P --prefix


子项目有更新,拉取到本地
chunli@ubuntu:~/git/mycode$ git subtree pull --prefix=modules_cat origin_cat master --squash # 拉取到本地
chunli@ubuntu:~/git/mycode$ git subtree pull --prefix=modules_cat URL master --squash # 或者指定URL


本地对子项目的修改,推送
chunli@ubuntu:~/git/mycode$ git add xxx # 修改了子项目的代码
chunli@ubuntu:~/git/mycode$ git commit -m "xxxxx" # 提交修改
chunli@ubuntu:~/git/mycode$ git push # 提交到远程
chunli@ubuntu:~/git/mycode$ git subtree push --prefix=modules_cat origin_cat master --squash # 提交到远程
chunli@ubuntu:~/git/mycode$ git subtree push --prefix=modules_cat URL master --squash # 或者指定URL


克隆的项目已经存在subtree子模块[关联/更新/推送/]
chunli@ubuntu:~/git/mycode$ git remote add origin_cat https://gitlab.li-chunli.top:20443/root/mycat.git # 设置关联
chunli@ubuntu:~/git/mycode$ git subtree pull --prefix=modules_cat origin_cat master --squash # 拉取到本地
chunli@ubuntu:~/git/mycode$ git subtree push --prefix=modules_cat origin_cat master --squash # 提交到远程

拉取 fork 更新

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
root@singapore:~/libevent# git clone   https://github.com/li-chunli/libevent.git
root@singapore:~/libevent# cd libevent/
root@singapore:~/libevent# git remote add upstream https://github.com/libevent/libevent.git
root@singapore:~/libevent# git fetch upstream
root@singapore:~/libevent# git branch -avv #查看分支
* master 5ee507c8 [origin/master] http: implement separate timeouts for read/write/connect phase
remotes/origin/21_http_extended_methodcmp 35487dc5 Added http method extending, based on work by @miniupnp
remotes/origin/HEAD -> origin/master
remotes/origin/master 5ee507c8 http: implement separate timeouts for read/write/connect phase
remotes/origin/patches-1.4 46a1375b evhttp: fix leak from keep-alive disconnect or read/write timeout
remotes/origin/patches-2.0 4294867c Bump version to 2.0.23-beta
remotes/origin/patches-2.1 d44eb473 Merge 'official/pr/761' into patches-2.1 -- changelog fixes
remotes/upstream/master 0d2f1700 Merge #976 -- symbols check build fixes
remotes/upstream/patches-1.4 46a1375b evhttp: fix leak from keep-alive disconnect or read/write timeout
remotes/upstream/patches-2.0 19e839c7 Make it build using OpenSSL 1.1.0
remotes/upstream/patches-2.1 4c908dde Merge branch 'release-2.1.11-stable-pull' into patches-2.1
root@singapore:~/libevent#

然后查看远程库信息
root@singapore:~/libevent# git remote show
origin
upstream
root@singapore:~/libevent#
root@singapore:~/libevent# git remote show origin
* remote origin
Fetch URL: https://github.com/li-chunli/libevent.git
Push URL: https://github.com/li-chunli/libevent.git
HEAD branch: master
Remote branches:
21_http_extended_methodcmp tracked
master tracked
patches-1.4 tracked
patches-2.0 tracked
patches-2.1 tracked
Local branch configured for 'git pull':
master merges with remote master
Local ref configured for 'git push':
master pushes to master (up to date)
root@singapore:~/libevent#
root@singapore:~/libevent#
root@singapore:~/libevent#
root@singapore:~/libevent# git remote show upstream
* remote upstream
Fetch URL: https://github.com/libevent/libevent.git
Push URL: https://github.com/libevent/libevent.git
HEAD branch: master
Remote branches:
master tracked
patches-1.4 tracked
patches-2.0 tracked
patches-2.1 tracked
Local ref configured for 'git push':
master pushes to master (local out of date)
root@singapore:~/libevent#

新旧仓库 关联推送

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 较新的代码仓库
git clone git@192.168.20.98:dpi_tools/newflow.git
git checkout REGEX #检出待推送的分支


# 较旧的代码仓库
git remote add back git@192.168.101.23:infra/newflow.git
git fetch back #拉取到本地 但不检出


# 将新的仓库分支 推送到 旧的仓库分支
git push -u back REGEX


# 已完成同步