说明:
整理一份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):https://github.com/zhengjiaao/ORIGINAL_REPOSITORY.git
Fork后的项目(YOUR_FORK):https://github.com/zhengjiaao/YOUR_FORK.git
一、两个仓库之间如何同步代码,如何保持代码相同
方式一:在源A仓库或Fork的B仓库上执行同步
1、将源项目代码同步到Fork出来的个人项目上:
代码:
#拉取Fork出来的分支
git clone Fork的分支url
#注意:进入项目根目录,执行下面操作
#查看所有远程库(remote repo)的远程url
git remote -v
#添加源分支url
git remote add upstream 替换成源项目url
#查看所有远程库(remote repo)的远程url
git remote -v
#从源分支获取最新的代码
git fetch upstream
#切换到主分支
git checkout master
#合并本地分支和源分支,本地库和远程的github原仓库同步
git merge upstream/master
#push到fork分支,本地的仓库提交到github
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)
$
同步后的效果图:
your_fork_1.png
2、将Fork出来的个人项目同步到源项目上:
代码:
#拉取源项目分支
git clone 源项目分支url
#注意:进入项目根目录,执行下面操作
#查看所有远程库(remote repo)的远程url
git remote -v
#添加Fork分支url
git remote add upstream 替换成Fork分支url
#查看所有远程库(remote repo)的远程url
git remote -v
#从源分支获取最新的代码
git fetch upstream
#切换到主分支
git checkout master
#合并本地分支和源分支,本地库和远程的github原仓库同步
git merge upstream/master
#push到fork分支,本地的仓库提交到github
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)
$
同步后的效果图:
original_repository_1.png
方式二:分别git push到A仓库和B仓库
1、将A仓库代码同步到Fork出来的B仓库上:
代码:
#拉取Fork出来的分支
git clone Fork的分支url
#注意:进入项目根目录,执行下面操作
#查看所有远程库(remote repo)的远程url
git remote -v
#添加源分支url
git remote add upstream 替换成源项目url
#查看所有远程库(remote repo)的远程url
git remote -v
#从源分支获取最新的代码
git fetch upstream
#切换到主分支
git checkout master
#合并本地分支和源分支,本地库和远程的github原仓库同步
git merge upstream/master
#push到fork分支,本地的仓库提交到github的B仓库上
git push
#push到A远程仓库上
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):https://github.com/zhengjiaao/YOUR_FORK.git
克隆文件:'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
效果:
spatse-checkout.png
克隆一个文件成功!
下面将这个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 https://github.com/zhengjiaao/YOUR_FORK
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/proB远程仓库所有分支:
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提交内容
#合并某次的commit提交内容
#例如:将A_pro分支某次commit提交的内容合并到B_pro
#切换到A_pro分支
$ git checkout A_pro
#查看A_pro分支 commit提交的日志
$ 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文件
#退出日志 英文状态下按Q
#将A_pro分支 ‘新增fork9文件’这次commit提交的内容 合并到B_pro
#切换到B_pro分支
$ git checkout B_pro
#选择A_pro分支 ‘新增fork9文件’这次commit的id合并到B_pro分支上,当然不会把A_pro分支 ‘新增fork9文件’commit之前的,也就是‘新增fork8文件’这次commit所提交的文件包含在内,仅合并"新增fork9文件"的commit。
$ git cherry-pick 3c03d1c376ccc8c58d1f232c5e0f57cdeba8df53
B仓库的B_pro分支合并A仓库的A_pro分支某次commit的效果图:
git合并分支效果.png
网友评论