git-github fork 后同步源仓库的新提交/向开源框架提交pr

github fork 后同步源仓库的新提交

官网参考: [Syncing a fork]: https://help.github.com/en/articles/syncing-a-fork
github fork 后同步源仓库的新提交
参考URL: https://www.cnblogs.com/Mr-O-O/p/11668464.html
git: 保持fork的项目与上游同步
参考URL: https://cloud.tencent.com/developer/article/1348597

注:先把fork的仓库clone到你本地。下面的都是基于本地仓库操作的!

  1. 查看你的本地仓库的远程仓库配置
$ git remote -v
> origin  https://github.com/YOUR_USERNAME/你的fork仓库.git (fetch)
> origin  https://github.com/YOUR_USERNAME/你的fork仓库.git (push)
  1. 配置一个远程仓库指向源仓库。 (添加上游仓库)
    $ git remote add upstream https://github.com/ORIGINAL_OWNER/源仓库.git

demo: loutus项目

git remote add upstream https://github.com/filecoin-project/lotus.git
git remote -v
  1. 再次查看你本地仓库的远程仓库配置
$ git remote -v
> origin    https://github.com/YOUR_USERNAME/你的fork仓库.git (fetch)
> origin    https://github.com/YOUR_USERNAME/你的fork仓库.git (push)
> upstream  https://github.com/ORIGINAL_OWNER/源仓库.git (fetch)
> upstream  https://github.com/ORIGINAL_OWNER/源仓库.git (push)
  1. 把远程的源仓库fetch到本地,此时远程源仓库的新提交并不在你的master分支!而是在upstream/master 分支.
$ git fetch upstream
> remote: Counting objects: 75, done.
> remote: Compressing objects: 100% (53/53), done.
> remote: Total 62 (delta 27), reused 44 (delta 9)
> Unpacking objects: 100% (62/62), done.
> From https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY>  * [new branch]      master     -> upstream/master

默认情况下,git fetch取回所有分支(branch)的更新
git fetch upstream: #抓取在著名开源项目的GitHub仓库上新的提交数据到本地仓库

  1. 切换到master分支
    git branch 查看分支
$ git checkout master
> Switched to branch 'master'
  1. 把fetch下来的 upstream/master 分支 合并到master分支!这里假设没有冲突,且你的本地没有新的提交! ( 将upstream/master merge到 本地master分支)
$ git merge upstream/master
> Updating a422352..5fdff0f
> Fast-forward
>  README                    |    9 -------
>  README.md                 |    7 ++++++
>  2 files changed, 7 insertions(+), 9 deletions(-)
>  delete mode 100644 README
>  create mode 100644 README.md
  1. 然后在master分支上 直接git push就好了。此时你fork的仓库就与源仓库一致了。
git push origin master

总结:

git fetch upstream
git checkout master
git merge upstream/master
git push origin master

github中origin和upstream的区别

官网描述:
When a repo is cloned, it has a default remote called origin that points to your fork on GitHub, not the original repo it was forked from. To keep track of the original repo, you need to add another remote named upstream.
git remote add upstream git://github.com/user/repo_name.git
在这里插入图片描述当执行git branch -v命令时,某些分支的前缀为origin ( remotes/origin/… ),而其他分支的前缀为upstream ( remotes/upstream/… )。

origin是你所克隆的原始仓库的默认名字,upstream是fork的原项目的远程仓库的默认名字。

可以用git fetch [remote-name]命令从远程仓库抓取数据到本地:

git fetch origin #抓取在你的GitHub仓库上新的提交数据到本地仓库
$ git fetch upstream #抓取在著名开源项目的GitHub仓库上新的提交数据到本地仓库

总结:

  1. 如果是 upstream repo,你只可以拉取最新代码(即 git fetch ),从而保证你本地的仓库与源仓库同步
  2. 如果是 origin repo,就是你自己的repo(自己创建的,或者 fork 的项目)你可以做 任何推拉操作(pull and push)
  3. 你可以通过 pull request 向 upstream repo 贡献代码

git 分支查看与切换


# 1.查看所有分支
> git branch -a
  
# 2.查看当前使用分支(结果列表中前面标*号的表示当前使用分支)
> git branch

# 3.切换分支
> git checkout 分支名

向开源框架提交pr

github----向开源框架提交pr的过程
原文链接:https://blog.csdn.net/vim_wj/article/details/78300239

  1. fork别人代码
  2. git clone到本地
  3. 创建分支
    git checkout -b shepf-dev

修改源码,git push
4. 提交pr
需要注意的是compare处选择刚才提交上来的分支

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章