Github上fork项目后与原项目保持同步

本文介绍了一种高效的方法来同步GitHub上fork的项目与其原作者的更新。通过建立本地中介库,实现从原项目拉取更新并合并到自己fork的项目中,避免了删除和重新fork的繁琐步骤。

其实网上这类似的教程很多,但是由于每个描述不同,大家接受的方式也不同

有时候我们会发现,在几个月前fork来的代码,在原作者更新后,我们享受不到更新带来的便利

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

1.将自己仓库中原来fork的项目删掉  (delete deletethis repository),然后在重新到原作者仓库中(fork)一份最新代码(不推荐方式

这似乎达到了我们的最终目的,虽然有时候我们做事是为了达到某种目的而不看过程,但不尽然,有时候复杂的的过程

往往会伴随着你意想不到的收获,为什么有人被称之为神(O(∩_∩)O哈哈~,开个玩笑),当然这得看每个人的生活方

式了,总仁者见仁智者见智,这里我不多做赘述。


2.方法是在本地建立两个库的中介,把两个远程库都clone到本地,然后拉取原项目更新到本地,合并更新,最后push到你的github就完成。推荐方式

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

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

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

1
$ git remote -v

z

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

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

z

(4)这个hunter名字随便取哦,这次有两个远程分支咯,我们继续 fetch

然后把原项目更新的内容fetch到本地:git fetch hunter

1
$ git fetch hunter

(5)查看下分支:

1
$ git branch -av

z

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

1
2
$ git checkout master
$ git merge hunter/master

z

push

1
$ git push -u origin master

(7)merge后本地的仓库已经是最新的咯,然后push到你的github就同步完成。


1.可能使用到的一些git命令

检查本地仓库git状态   git status
添加仓库修改            git add . (添加所有修改)  / git add ~/file(添加具体文件)
确认提交修改               git commit 这里会出现一个面板,里边主要用来说明本次修改的相关信息
检查更新                      git checkout
将对本地仓库的修改提交到github仓库 git push -u origin master


### 如何同步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() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值