整理一份github的fork的用法,以及相关的操作命令,重点是在于fork完成了之后,两个仓库之间如何同步代码,如何保持代码相同
比如仓库A和仓库B,B是从A上面fork出来的,然后分别在A,B进行了开发:
1.这个时候想把A,B之间的修改相互同步
2.如果A修改了10个,只想同步5个到B,怎么操作
3.B修改了10个,想同步3个到A ,怎么操作
需求:团队里推了使用github这种网站来协同开发,但是出现了个问题:fork后的代码无法 与源分支代码同步,导致fork分支的代码只有自己写的那部分,而不是各个分支merge之后的版本
源项目(ORIGINAL_REPOSITORY):
github.com/zhengjiaao/…
Fork后的项目(YOUR_FORK):
github.com/zhengjiaao/…
一、两个仓库之间如何同步代码,如何保持代码相同
方式一:在源A仓库或Fork的B仓库上执行同步
1、将源项目代码同步到Fork出来的个人项目上:
git clone Fork的分支url
git remote -v
git remote add upstream 替换成源项目url
git remote -v
git fetch upstream
git checkout master
git merge upstream/master
git push origin master
复制代码
演示:
源项目的仓库(ORIGINAL_REPOSITORY)添加fork文件,将fork文件同步到fork后的项目仓库(YOUR_FORK)上
下拉 fork后的仓库 'YOUR_FORK' 代码
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git remote -v
origin https://github.com/zhengjiaao/YOUR_FORK.git (fetch)
origin https://github.com/zhengjiaao/YOUR_FORK.git (push)
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git remote add upstream https://github.com/zhengjiaao/ORIGINAL_REPOSITORY.git
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git remote -v
origin https://github.com/zhengjiaao/YOUR_FORK.git (fetch)
origin https://github.com/zhengjiaao/YOUR_FORK.git (push)
upstream https://github.com/zhengjiaao/ORIGINAL_REPOSITORY.git (fetch)
upstream https://github.com/zhengjiaao/ORIGINAL_REPOSITORY.git (push)
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git fetch upstream
From https://github.com/zhengjiaao/ORIGINAL_REPOSITORY
* [new branch] master -> upstream/master
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git checkout master
Already on 'master'
Your branch is up to date with 'origin/master'.
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git merge upstream/master
Already up to date.
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git push origin master
Everything up-to-date
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git push
Everything up-to-date
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git fetch upstream
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/zhengjiaao/ORIGINAL_REPOSITORY
eb3614a..92539d4 master -> upstream/master
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git checkout master
Already on 'master'
Your branch is up to date with 'origin/master'.
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git merge upstream/master
Updating eb3614a..92539d4
Fast-forward
fork.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 fork.txt
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 331 bytes | 331.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/zhengjiaao/YOUR_FORK.git
eb3614a..92539d4 master -> master
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
复制代码
同步后的效果图:
2、将Fork出来的个人项目同步到源项目上:
git clone 源项目分支url
git remote -v
git remote add upstream 替换成Fork分支url
git remote -v
git fetch upstream
git checkout master
git merge upstream/master
git push origin master
复制代码
演示:
fork后的项目的仓库(YOUR_FORK)上添加fork2文件,将fork2文件同步到源仓库(ORIGINAL_REPOSITORY)上
下拉 源仓库'ORIGINAL_REPOSITORY' 代码
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/ORIGINAL_REPOSITORY (master)
$ git remote -v
origin https://github.com/zhengjiaao/ORIGINAL_REPOSITORY.git (fetch)
origin https://github.com/zhengjiaao/ORIGINAL_REPOSITORY.git (push)
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/ORIGINAL_REPOSITORY (master)
$ git remote add upstream https://github.com/zhengjiaao/YOUR_FORK.git
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/ORIGINAL_REPOSITORY (master)
$ git remote -v
origin https://github.com/zhengjiaao/ORIGINAL_REPOSITORY.git (fetch)
origin https://github.com/zhengjiaao/ORIGINAL_REPOSITORY.git (push)
upstream https://github.com/zhengjiaao/YOUR_FORK.git (fetch)
upstream https://github.com/zhengjiaao/YOUR_FORK.git (push)
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/ORIGINAL_REPOSITORY (master)
$ git fetch upstream
From https://github.com/zhengjiaao/YOUR_FORK
* [new branch] master -> upstream/master
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/ORIGINAL_REPOSITORY (master)
$ git checkout master
Already on 'master'
Your branch is up to date with 'origin/master'.
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/ORIGINAL_REPOSITORY (master)
$ git merge upstream/master
Already up to date.
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/ORIGINAL_REPOSITORY (master)
$ git push
Everything up-to-date
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/ORIGINAL_REPOSITORY (master)
$ git fetch upstream
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 2 (delta 1), reused 2 (delta 1), pack-reused 0
Unpacking objects: 100% (2/2), done.
From https://github.com/zhengjiaao/YOUR_FORK
92539d4..4a61703 master -> upstream/master
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/ORIGINAL_REPOSITORY (master)
$ git checkout master
Already on 'master'
Your branch is up to date with 'origin/master'.
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/ORIGINAL_REPOSITORY (master)
$ git merge upstream/master
Updating 92539d4..4a61703
Fast-forward
fork2.txt | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 fork2.txt
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/ORIGINAL_REPOSITORY (master)
$ git push
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 287 bytes | 287.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/zhengjiaao/ORIGINAL_REPOSITORY.git
92539d4..4a61703 master -> master
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/ORIGINAL_REPOSITORY (master)
复制代码
同步后的效果图:
方式二:分别git push到A仓库和B仓库
1、将A仓库代码同步到Fork出来的B仓库上:
git clone Fork的分支url
git remote -v
git remote add upstream 替换成源项目url
git remote -v
git fetch upstream
git checkout master
git merge upstream/master
git push
git push upstream master
复制代码
演示:
源项目的仓库(ORIGINAL_REPOSITORY)添加fork文件,将fork文件同步到fork后的项目仓库(YOUR_FORK)上
下拉 fork后的仓库 'YOUR_FORK' 代码
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git remote -v
origin https://github.com/zhengjiaao/YOUR_FORK.git (fetch)
origin https://github.com/zhengjiaao/YOUR_FORK.git (push)
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git remote add upstream https://github.com/zhengjiaao/ORIGINAL_REPOSITORY.git
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git remote -v
origin https://github.com/zhengjiaao/YOUR_FORK.git (fetch)
origin https://github.com/zhengjiaao/YOUR_FORK.git (push)
upstream https://github.com/zhengjiaao/ORIGINAL_REPOSITORY.git (fetch)
upstream https://github.com/zhengjiaao/ORIGINAL_REPOSITORY.git (push)
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git fetch upstream
From https://github.com/zhengjiaao/ORIGINAL_REPOSITORY
* [new branch] master -> upstream/master
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git checkout master
Already on 'master'
Your branch is up to date with 'origin/master'.
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git merge upstream/master
Already up to date.
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git push origin master
Everything up-to-date
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git push
Everything up-to-date
复制代码
修改B本地仓库fork文件,将fork文件同步到B远程仓库和A远程仓库:
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git add .
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git commit -m "your_fork 修改fork文件"
[master 0b44e55] your_fork 修改fork文件
1 file changed, 8 insertions(+), 1 deletion(-)
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 371 bytes | 371.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/zhengjiaao/YOUR_FORK.git
4a61703..0b44e55 master -> master
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git push upstream master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 371 bytes | 185.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/zhengjiaao/ORIGINAL_REPOSITORY.git
4a61703..0b44e55 master -> master
可以看出我第一次是直接git push,没有指定远程库名称,默认推送到了gitosc中,开始还以为一次性推送到了2个服务器呢,剩下的github需要指定名称推送
反过来,也可以在将出来的ForkB仓库代码同步到A仓库上
方式三:git push 一次性推送到A和B仓库上(推荐)
找到项目根目录下.git/config文件,可以看到添加的有origin和upstream两个仓库。
config文件:
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[remote "origin"]
url = https://github.com/zhengjiaao/YOUR_FORK.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[remote "upstream"]
url = https://github.com/zhengjiaao/ORIGINAL_REPOSITORY.git
fetch = +refs/heads/*:refs/remotes/upstream/*
复制代码
移除仓库:git remote rm 仓库名称
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git remote rm origin
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git remote rm upstream
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git remote add all https://github.com/zhengjiaao/YOUR_FORK.git
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git remote set-url --add all https://github.com/zhengjiaao/ORIGINAL_REPOSITORY.git
复制代码
config文件:
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[remote "all"]
url = https://github.com/zhengjiaao/YOUR_FORK.git
fetch = +refs/heads/*:refs/remotes/all/*
url = https://github.com/zhengjiaao/ORIGINAL_REPOSITORY.git
复制代码
修改fork文件内容,测试一次性提交到两个仓库
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git add .
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git commit -m "测试git push all --all"
[master 069ffcf] 测试git push all --all
1 file changed, 1 insertion(+), 1 deletion(-)
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git push all -all
error: did you mean `--all` (with two dashes ?)
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git push all --all
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 318 bytes | 318.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To https://github.com/zhengjiaao/YOUR_FORK.git
0b44e55..069ffcf master -> master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 318 bytes | 318.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To https://github.com/zhengjiaao/ORIGINAL_REPOSITORY.git
0b44e55..069ffcf master -> master
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
复制代码
提交成功!-->去github上查看两个仓库都有提交的信息。
方式四:一次性推送到A和B仓库上(修改.git/config配置文件)
查看.git/config配置文件:
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[remote "all"]
url = https://github.com/zhengjiaao/YOUR_FORK.git
fetch = +refs/heads/*:refs/remotes/all/*
url = https://github.com/zhengjiaao/ORIGINAL_REPOSITORY.git
复制代码
删除all
git remote rm all
复制代码
查看.git/config配置文件:
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
复制代码
直接修改.git/config配置文件:
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[remote "all"]
url = https://github.com/zhengjiaao/YOUR_FORK.git
url = https://github.com/zhengjiaao/ORIGINAL_REPOSITORY.git
复制代码
修改fork文件内容,测试一次性提交到两个仓库
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git remote rm all
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git add .
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git commit -m "测试修改config配置文件"
[master e3c0277] 测试修改config配置文件
1 file changed, 1 insertion(+)
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
$ git push all --all
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 354 bytes | 354.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To https://github.com/zhengjiaao/YOUR_FORK.git
069ffcf..e3c0277 master -> master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 354 bytes | 354.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To https://github.com/zhengjiaao/ORIGINAL_REPOSITORY.git
069ffcf..e3c0277 master -> master
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/YOUR_FORK (master)
复制代码
上传成功!
二、仓库A和仓库B只同步部分文件
比如仓库A和仓库B,B是从A上面fork出来的,然后分别在A,B进行了开发:
1.这个时候想把A,B之间的修改相互同步
2.如果A修改了10个,只想同步5个到B,怎么操作
3.B修改了10个,想同步3个到A ,怎么操作
A远程仓库修改了10个,只想同步5个到远程B仓库
方式一:10个文件都上传到A的远程仓库,然后同步5个指定文件到B的远程仓库
参考:下方的 三、git克隆或下载一个A仓库单个文件夹,并push到B仓库里
方式二:10个文件只上传5个指定的文件上传到远程A上,然后远A同步到远程B上
参考:下方的 三、git克隆或下载一个A仓库单个文件夹,并push到B仓库里
三、git克隆或下载一个A仓库单个文件夹,并push到B仓库里
仓库(YOUR_FORK):github.com/zhengjiaao/…
克隆文件:'YOUR_FORK/fork.txt' 或 克隆文件夹:'YOUR_FORK/文件夹/*' 是克隆'文件夹'下的所有文件
注意:.git/info/sparse-checkout文件每一行是一个文件或文件夹
使用:echo 'fork.txt' >> .git/info/sparse-checkout
echo '文件夹/*' >> .git/info/sparse-checkout
现在想只克隆YOUR_FORK/fork.txt文件,本地新建一个文件test文件夹:
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/test (master)
$ git init
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/test (master)
$ git config core.sparsecheckout true
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/test (master)
$ echo 'fork.txt' >> .git/info/sparse-checkout
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/test (master)
$ git remote add origin https://github.com/zhengjiaao/YOUR_FORK.git
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/test (master)
$ git pull origin master
复制代码
效果:
克隆一个文件成功!
下面将这个fork.txt文件git push 到自己的github仓库上
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/test (master)
$ git remote -v
origin https://github.com/zhengjiaao/YOUR_FORK.git (fetch)
origin https://github.com/zhengjiaao/YOUR_FORK.git (push)
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/test (master)
$ git remote add upstream https://github.com/zhengjiaao/ORIGINAL_REPOSITORY.git
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/test (master)
$ git remote -v
origin https://github.com/zhengjiaao/YOUR_FORK.git (fetch)
origin https://github.com/zhengjiaao/YOUR_FORK.git (push)
upstream https://github.com/zhengjiaao/ORIGINAL_REPOSITORY.git (fetch)
upstream https://github.com/zhengjiaao/ORIGINAL_REPOSITORY.git (push)
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/test (master)
$ git fetch upstream
From https://github.com/zhengjiaao/ORIGINAL_REPOSITORY
* [new branch] master -> upstream/master
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/test (master)
$ git fetch upstream
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/test (master)
$ git checkout master
Already on 'master'
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/test (master)
$ git merge upstream/master
Already up to date.
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/test (master)
$ git push upstream master
Everything up-to-date
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/test (master)
$ git checkout master
Already on 'master'
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/test (master)
$ git merge upstream/master
Already up to date.
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/test (master)
$ git pull origin master
From https://github.com/zhengjiaao/YOUR_FORK
* branch master -> FETCH_HEAD
Updating e3c0277..b407bf8
Fast-forward
fork.txt | 1 +
fork2.txt | 3 +++
2 files changed, 4 insertions(+)
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/test (master)
$ git push upstream master
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 459 bytes | 229.00 KiB/s, done.
Total 4 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To https://github.com/zhengjiaao/ORIGINAL_REPOSITORY.git
e3c0277..b407bf8 master -> master
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/test (master)
注意:第一次克隆了文件,如果还想再克隆其它的文件,则需要克隆的项目有提交记录,不然无法再次克隆成功!
Administrator@ZhengJiaAo MINGW64 /j/IdeaTestProject/GitHub/test (master)
$ git pull origin master
From github.com/zhengjiaao/…
branch master -> FETCH_HEAD
Already up to date.
四、仓库源A和Fork的仓库B 只同步部分文件
说明:A远程仓库和B远程仓库的分支,github:B仓库是从A仓库Fork出来的,且删除了B仓库的dev分支
A远程仓库所有分支:
remotes/origin/dev
remotes/origin/master
remotes/origin/pro
B远程仓库所有分支:
remotes/origin/master
remotes/origin/pro
假如:A远程仓库的pro分支commit 10次,B远程仓库pro分支commit 5次
1、A远程仓库的commit 第2次提交的内容 同步到B远程仓库
2、B远程仓库的第3次commit内容提交到 A远程仓库中
思路:创建本地仓库,且与多个远程仓库关联,再关联远程分支
方式:创建一个本地仓库YOUR_FORK_AB_pro,git init初始化并分别关联'B远程仓库的pro分支和关联A远程仓库的pro分支',B_pro,A_pro
#初始化your_fork_AB_pro本地仓库
$ git init
#关联多个远程仓库
$ git remote add A https://github.com/zhengjiaao/YOUR_FORK.git
$ git remote add B https://github.com/zhengjiaao/YOUR_FORK_pro.git
#查看本地仓库关联的所有远程仓库地址
$ git remote -v
#更新远程仓库数据信息到本地仓库your_fork_AB_pro,不是将远程仓库代码下拉到本地
$ git fetch A pro
$ git fetch B pro
#分别创建本地分支且关联的远程分支 A_pro 和 B_pro
$ git checkout -b A_pro A/pro
$ git checkout -b B_pro B/pro
#查看所有分支
$ git branch -a
A_pro
* B_pro
remotes/A/dev
remotes/A/master
remotes/A/pro
remotes/B/pro
现在本地仓库有两个分支A_pro和B_pro,我们就可以对这两个分支进行操作,例如:合并操作-->合并某次的commit,合并某个文件或文件夹等
例如:合并本地分支的某个文件或文件夹
#合并本地分支的某个文件或文件夹
#如:想要将分支B_pro下public文件下所有文件和view下index.html文件合并到分支A_pro
#首先切换到A仓库的分支A_pro
$ git checkout A_pro
#合并B仓库的B_pro分支, 注意:因为public下可能有多层目录,所有不能使用public/*
$ git checkout B_pro public/** view/index.html
复制代码
例如:合并某次的commit提交内容
$ git checkout A_pro
$ git log
commit 68cfab5b9c4b536b6d42117d476308685fbef7ad (HEAD -> A_pro, A/pro)
Author: Zhengjiaao <1263598336@qq.com>
Date: Tue Oct 15 09:53:16 2019 +0800
your_fork:新增fork10文件
commit 3c03d1c376ccc8c58d1f232c5e0f57cdeba8df53
Author: Zhengjiaao <1263598336@qq.com>
Date: Tue Oct 15 09:52:46 2019 +0800
your_fork:新增fork9文件
commit a1ad40b91d87e4ec30c869e404fabca56a7e47ea
Author: Zhengjiaao <1263598336@qq.com>
Date: Tue Oct 15 09:52:23 2019 +0800
your_fork:新增fork8文件
$ git checkout B_pro
$ git cherry-pick 3c03d1c376ccc8c58d1f232c5e0f57cdeba8df53
复制代码
B仓库的B_pro分支合并A仓库的A_pro分支某次commit的效果图:
iEven
GitHub