git fork的使用

https://www.jianshu.com/p/d73dcee2d907
在这里插入图片描述

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

git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
再次查看状态确认是否配置成功。

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)

3. 执行同步fork操作

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

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

4.将upstream的代码合并到本地仓库C上

4.1 分支切换&同步自己的远程仓库

首先在本地代码C上从master创建分支git checkout -b dev
执行git pull origin dev:dev或者git pull origin master 从自己的远程仓库B上拉取最新的代码

4.2 执行合并upstream操作

使用git merge upstream/master命令,把 upstream/master 分支合并到本地 master 上
如果想同步远程仓库A非master源的代码到自己仓库B上来,比如说远程远有两个分支,master和dev则
合并非master分支的代码, 使用git merge upstream/dev命令,把 upstream/dev 分支合并到本地 dev 上

这样就完成了同步,并且不会丢掉本地修改的内容。

git merge upstream/master
 Updating a422352..5fdff0f
 Fast-forward
  README                    |    9 -------

Import upstream branch into fork

Github: Import upstream branch into fork

  1. Make sure you’ve pulled the new upstream branch into your local repo:
  • First, ensure your working tree is clean (commit/stash/revert any changes)
  • Then, git fetch upstream to retrieve the new upstream branch
 git fetch upstream
  1. Create and switch to a local version of the new upstream branch (newbranch):
git checkout -b newbranch upstream/newbranch
  1. When you’re ready to push the new branch to origin:
git push -u origin newbranch

The -u switch sets up tracking to the specified remote (in this example, origin)

Error in git: You can only push your own commits in this repository

参考Error in git: You can only push your own commits in this repository
GitLab: You cannot push commits for . You can only push commits that were committed with one of your own verified emails

结论是有以下几个方法:

  1. 要gitlab管理员修改这个仓库的设置,去掉对提交者验证限制
  2. 修改历史,把提交者改成自己的账号

另外,可以使用GitLab的Mirroring repositories功能(Settting->Repository->Mirroring repositories),使用https,从另一个仓库pull到当前仓库,进行同步,覆盖。此时,要求当前的功能在独有分支进行,否则会被内容会被覆盖。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值