Git fork后的同步以及提交等相应操作

背景

在开源大项目中,不能直接对目标项目进行改动,需要先fork一份到自己的仓库进行开发,然后通过提交pull request的方式提交自己的贡献。

基础依赖

因为需要模拟fork的场景,所以准备了2个github账户:
一个是yang-ybb1(这个作为原开发者)、一个是yang-ybb(这个作为自己)

操作过程

  1. 首先yang-ybb1(原作者)创建了一个项目,项目名为learn-git,项目默认的分支是master分支,有一个默认文件是README.md。此时原作者新建了一个开发分支test_branch。到目前为止,原作者项目的结构如下图所示:


    2个分支:master和test_branch

    test_branch分支下只有一个README.md文件
  2. 此时yang-ybb(自己)fork了原作者的learn-git项目,此时在自己的github里面就有了learn-git项目,并且分支也是2个分支:master和test_branch,总体来说就是和原作者操作1里面的代码、分支都是一样的。

  3. 原作者在他的分支test_branch上进行了一些操作:新建了一个a.txt文件,文件内容为“这个是a.txt文件”。此时原仓库的分支和文件如下:


    在test_branch分支下面出现了一个a.txt文件

    此时在看自己的仓库里面,分支还有文件都没有变化,也就是说原作者的改动并没有同步过来。

  4. 自己的仓库需要同步原作者改动的部分。

# 首先把自己的仓库clone下来(*只需要执行一次*)
> git clone [email protected]:yang-ybb/learn-git.git

# 进入项目目录
> cd learn-git

# 查看所有分支情况,可以看到目前本地分支只有master,远程的分支也只有自己的origin/master和origin/test_branchs
> git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/test_branch

# 查看远程信息,目前只有origin的信息,没有原作者仓库的信息
> git remote -v
origin  [email protected]:yang-ybb/learn-git.git (fetch)
origin  [email protected]:yang-ybb/learn-git.git (push)

# 添加原作者仓库信息(注意这里的最后的链接是原作者git仓库的链接,yang-ybb1s)(*只需要执行一次*)
> git remote add upstream [email protected]:yang-ybb1/learn-git.git

# 再查看一下远程信息,此时就发现多了upstream字段,也就是原作者仓库的地址
> git remote -v
origin  [email protected]:yang-ybb/learn-git.git (fetch)
origin  [email protected]:yang-ybb/learn-git.git (push)
upstream    [email protected]:yang-ybb1/learn-git.git (fetch)
upstream    [email protected]:yang-ybb1/learn-git.git (push)
# 如果发现upstream添加错误了,使用git remote remove upstream删除即可,然后再重新添加

# 拉取原作者仓库的代码,可以看到拉取了upstream/master和upstream/test_branch
> git fetch upstream
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), 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 github.com:yang-ybb1/learn-git
 * [new branch]      master      -> upstream/master
 * [new branch]      test_branch -> upstream/test_branch

# 查看此时所有分支情况,新添加了2个upstream的分支
> git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/test_branch
  remotes/upstream/master
  remotes/upstream/test_branch

# 此时执行下面2句就是相当于把原作者仓库的master和本地master合并,然后提交到自己的仓库中。因为原作者仓库和自己的仓库master分支是一样的,所以都是提示“Already up to date.”
> git merge upstream/master
Already up to date.
> git push
Everything up-to-date

# 现在要做的是拉取原作者仓库的test_branch分支的最新代码,然后开发后提交到自己的仓库中
# 首先肯定是要切换到test_branch仓库中。提示错误:因为是有2个仓库都是叫test_branch,所以不知道是要切换到自己仓库的test_branch还是原作者仓库的test_branchs
> git checkout test_branch
error: pathspec 'test_branch' did not match any file(s) known to git
hint: 'test_branch' matched more than one remote tracking branch.
hint: We found 2 remotes with a reference that matched. So we fell back
hint: on trying to resolve the argument as a path, but failed there too!
hint: 
hint: If you meant to check out a remote tracking branch on, e.g. 'origin',
hint: you can do so by fully qualifying the name with the --track option:
hint: 
hint:     git checkout --track origin/
hint: 
hint: If you d like to always have checkouts of an ambiguous  prefer
hint: one remote, e.g. the 'origin' remote, consider setting
hint: checkout.defaultRemote=origin in your config.

# 根据提示,切换到原作者仓库到test_branch。这样就切换成功了。
> git checkout --track upstream/test_branch
Branch 'test_branch' set up to track remote branch 'test_branch' from 'upstream'.
Switched to a new branch 'test_branch'

# 注意这是切换到原作者仓库到test_branch分支,所以此时可以看到已经有了a.txt文件,内容也是一样的
> ll
total 16
drwxr-xr-x   5 bytedance  staff   160  8 24 21:13 ./
drwxr-xr-x+ 49 bytedance  staff  1568  8 24 20:45 ../
drwxr-xr-x  14 bytedance  staff   448  8 24 21:14 .git/
-rw-r--r--   1 bytedance  staff    11  8 24 20:45 README.md
-rw-r--r--   1 bytedance  staff    21  8 24 21:13 a.txt

# 然后自己改动一些,比如在a.txt里添加新的一行,“自己添加了新的一行”

# 查看文件改动,注意这里的upstream/test_branch提醒是从原作者仓库拉取的代码。
> git status
On branch test_branch
Your branch is up to date with 'upstream/test_branch'.
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git restore ..." to discard changes in working directory)
    modified:   a.txt
no changes added to commit (use "git add" and/or "git commit -a")

> git add a.txt
> git commit -m "fork后的一次改动提交"
[test_branch 6cace64] fork后的一次改动提交
 1 file changed, 1 insertion(+)

# 如果此时执行git push命令,后面不带参数,就会报错,因为此时push的默认仓库是原作者仓库,当然没有相应的权限,就报错了~
> git push
ERROR: Permission to yang-ybb1/learn-git.git denied to yang-ybb.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

# 所以要正确的push到自己的仓库中。这样就成功了。
> git push origin test_branch:test_branch
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 358 bytes | 358.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:yang-ybb/learn-git.git
   5398efb..6cace64  test_branch -> test_branch
此时查看自己的仓库,可以发现在test_branch下面有a.txt,并且a.txt也添加了自己的代码

巩固一下

  1. 还是先在原作者仓库里面修改,原作者在test_branch分支添加了一个b.txt文件,内容是“这是一个b.txt文件”
  2. 回到自己的开发当中,还是进入到项目文件中,查看当前状态和分支
# 由于自己之前在a.txt里面新添加了一行,所以提示领先upstream/test_branch分支,不用管,因为自己也没有权限直接给原作者仓库提交更新。
> git status
On branch test_branch
Your branch is ahead of 'upstream/test_branch' by 1 commit.
  (use "git push" to publish your local commits)
nothing to commit, working tree clean

# git pull = git fetch + git merge,这一步最容易发送冲突,如果没有冲突就可以直接放下开发;如果有冲突需要先解决冲突。
> git pull

# 自己在a.txt里面新添加一行,“在a.txt里面添加的第三行,是第二次更新原作者仓库后进行的开发”
> git add a.txt
> git commit -m "第二次改动提交"
> git push origin test_branch  # 这里省略了最后的:test_branch,是因为默认提交的本地分支是当前分支。
Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Delta compression using up to 12 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 776 bytes | 776.00 KiB/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To github.com:yang-ybb/learn-git.git
   6cace64..98b888b  test_branch -> test_branch
去网站上看已经提交成功了

你可能感兴趣的:(Git fork后的同步以及提交等相应操作)