Github上fork的项目怎么更新同步原项目

本文介绍如何在GitHub上更新已fork项目的代码至最新状态。通过两种方法实现:一是删除并重新fork;二是通过本地仓库合并远程更新,后者为推荐做法。

Github上fork的项目怎么更新同步原项目

在GitHub上我们会去fork别人的一个项目,然后在自己的GitHub中会生成一个副本,自己将副本clone到电脑本地上可以修改代码,修改之后push提交到远端remote中的origin,发现自己的GIthub副本中的代码改变了,而作者变更的代码没有同步下来,这时你需要合并原作者的最新代码过来, 让你的项目变成最新的。

一般有两种做法,第一种做法可谓简单粗暴,但却不是最好的做法,说明如下:

  • 第一种:将自己仓库中原来fork的项目删掉 (delete deletethis repository),然后在重新到原作者仓库中(fork)一份最新代码(不推荐方式),这似乎达到了我们的最终目的,但是自己改变的代码没合并进来。
  • 第二种:在本地建立两个库的中介,把两个远程库都clone到本地,然后拉取原项目更新到本地,合并更新,最后push到你的github就完成。(推荐方式)

具体实现:

(1)准备一个本地目录,并克隆自己fork的项目到本地。

$ git clone https://github.com/jp1017/FastAndroid.git

(2)然后cd 进入仓库(这里我直接进入项目根目录下),执行命令:

$ git remote -v

可以在cad控制台查看本地项目目录,如下:

图1.png

(3)我们可以看到,只有我们自己的远程仓库,下面clone原项目到该仓库

$ git remote add hunter https://github.com/huntermr/FastAndroid.git
$ git remote -v

hunter 相当于一个别名,名字随便取。


图2.png

(4)这时有两个远程分支,我们继续 fetch,然后把原项目更新的内容fetch到本地

$ git fetch hunter

(5)查看下分支:

$ git branch -av
图3.png

(6)一个本地分支master,三个远程分支,画红线的就是要合并的 merge

$ git checkout master
$ git merge hunter/master
图4.png

(7)如果有冲突的话,需要丢掉本地分支:

$ git reset –hard hunter/master 

(8)这时你的当前本地的项目变成和原作者的主项目一样了,可以把它提交到你的GitHub库

$ git commit -am ‘更新到原作者的主分支’
$ git push origin 
$ git push -u origin master -f –强制提交
### 如何同步ForkGitHub项目到最新的上游代码 为了使 forkGitHub 项目项目的最新版本保持一致,需执行一系列 Git 命令来完成此操作。 #### 添加上游仓库 首先,在本地存储库中添加指向项目的链接作为“upstream”。这允许从始位置获取更新。命令如下所示: ```bash git remote add upstream https://github.com/Halukisan/llm-universe.git ``` 确认已成功添加上游源可通过 `git remote -v` 查看所有远程连接的信息[^3]。 #### 获取并合并上游变更 接着,利用 `git fetch upstream` 来下载来自上游的新提交而不立即集成它们;之后使用 `git merge upstream/main` 或者针对特定分支(如 `main`),把新内容融入当前工作目录中的对应分支内。如果遇到任何冲突,则需要手动调整受影响的部分直至解决为止,并通过常规方式继续流程——即先标记更改 (`git add`) 再创建新的提交记录(`git commit`)[^2]。 #### 更新至远程服务器 最终阶段涉及推送这些变动回个人托管于 GitHub 上面的那个副本里去。为此目的而设计的一条指令便是 `git push origin main` ,它会将先前所做的全部改动发送给指定的目标分支[^4]。 ```python import os def sync_fork_with_upstream(): """ Synchronizes a local and remote forked repository with its upstream source. This function assumes that you have already cloned your fork locally, added the 'upstream' remote, fetched changes from it, merged those into your branch resolving any conflicts as necessary, committed these merges, then pushed them back up to your own fork on GitHub. """ try: # Add an upstream remote if not exists yet remotes = os.popen('git remote').read().splitlines() if "upstream" not in remotes: print("Adding upstream...") os.system('git remote add upstream https://github.com/Halukisan/llm-universe.git') # Fetch all branches of the upstream project without merging automatically print("\nFetching updates from upstream...") os.system('git fetch upstream') # Merge the latest version of the default branch ('main') from upstream into our current one print("\nMerging upstream's main branch into ours...\n") os.system('git merge upstream/main') # Pushing updated codebase including resolved conflicts onto personal fork at GitHub print("\nPushing synchronized state back to my fork\n") os.system('git push origin main') print("Sync completed successfully!") except Exception as e: print(f"There was an error during synchronization process:\n{str(e)}") if __name__ == "__main__": sync_fork_with_upstream() ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值