GIT FORK 相关操作

GIT FORK 相关操作

fork 了别人的仓库后,原作者又更新了仓库,如何将自己的代码和原仓库保持一致?

假设远程源仓库为A,自己fork后的远程仓库为B,自己本地的代码仓库为C

  1. 给 fork下来的仓库 配置一个 remote源

    一般来说从自己远程仓库B去拉代码后就会有remote

    使用 git remote -v 查看远程状态。

    git remote -v
    origin  https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
    origin  https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
  2. 添加一个将被同步给 fork 远程的上游仓库A

    1
    git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git

再次查看状态确认是否配置成功。

1
2
3
4
5
6
git remote -v
git remote -v
origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (fetch)
upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (push)
  1. 执行同步fork操作

    从上游仓库A fetch 分支和提交点,传送到本地,并会被存储在一个本地分支 upstream/master
    git fetch upstream,默认会将远程所有的分支fetch下来

    1
    git fetch upstream
    1
    2
    3
    4
    5
    6
    7
    remote: Counting objects: 41, done.
    remote: Compressing objects: 100% (41/41), done.
    remote: Total 41 (delta 17), reused 0 (delta 0)
    Unpacking objects: 100% (41/41), done.
    From https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY
    * [new branch] dev -> upstream/dev
    * [new branch] master -> upstream/master

    使用git fetch后,并不会将新的内容更新到工作区域的文件中,所以可以通过下面的命令来比较差异

    1
    git diff master origin/master
  1. 将upstream的代码合并到本地仓库C上

    1. 先同步origin 源到本地,git pull origin master

    2. 合并upstream到本地分支,git merge upstream/master
      使用git merge upstream/master命令,把 upstream/master 分支合并到本地 master 上

      git merge upstream/master

  2. 如果要将fork分支的修改提交到上游远程分支,申请提交pull request


 

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×