使用 git rebase 定期将分支同步到 master

发布于 2024-09-02 02:06:54 字数 674 浏览 5 评论 0原文

我有一个 Git 存储库,其中的分支几乎从不更改(没有其他人对其做出贡献)。它基本上是主分支,删除了一些代码和文件。有了这个分支,我可以轻松打包项目的精简版本,而不必每次都手动删除代码和文件。

我一直在使用 git rebase 来使该分支与主分支保持同步,但是当我尝试在变基后推送分支时,我总是收到此警告:

To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.  See the 'Note about
fast-forwards' section of 'git push --help' for details.

然后我使用 git push --force 它有效,但我觉得这可能是不好的做法。我想快速轻松地使这个分支与主分支保持“同步”。有更好的方法来处理这项任务吗?

更新

请参阅此主题以获取完整的说明和解决方案:

git rebase 和 git push:非快进,为什么使用?

I have a Git repository with a branch that hardly ever changes (nobody else is contributing to it). It is basically the master branch with some code and files stripped out. Having this branch around makes it easy for me to package up a leaner version of my project without having to strip out the code and files manually every time.

I have been using git rebase to keep this branch up to date with the master but I always get this warning when I try to push the branch after rebasing:

To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.  See the 'Note about
fast-forwards' section of 'git push --help' for details.

I then use git push --force and it works but I feel like this is probably bad practice. I want to keep this branch "in sync" with the master quickly and easily. Is there a better way of handling this task?

Update

See this topic for a full explanation and solution:

git rebase and git push: non-fast forward, why use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁绊已千年 2024-09-09 02:06:54

诀窍是:

  • 当你重新设置你的“模板”分支时,你改变了它的历史记录(不同的 SHA1),这意味着任何推送都不会是快进的(没有“新 SHA1”要添加,而是一个全新的集合) SHA1 来替换)
  • 然后将该分支推送到 非裸仓库(现在默认情况下会被阻止)

这是一个不好的做法:

  • 如果其他人依赖于获取该分支(这里不是这种情况),
  • 如果您依赖于该分支的工作树的内容您推送到的远程存储库(因为该内容,如果设置为表示模板分支,可能与您刚刚推送的实际模板分支不同步)

如果这两点不是问题,您可以继续。
但是(或更)“正确”的处理方法是有一个中间裸存储库(在远程服务器上)要推送到,然后从该裸存储库获取/拉取模板分支到您所在的其他服务器需要该模板项目。

The trick is:

  • when you rebase your "template" branch, you change its history (different SHA1), meaning any push won't be a fast-forward one (there a no "new SHA1" to add, but an entirely new set of SHA1 to replace)
  • you then push that branch to a non-bare repo (which is now by default prevented)

It is a bad practice:

  • if other people depend on fetching that branch (which is not the case here)
  • if you depend on the content of the working tree of the remote repo to which you push to (because that content, if set to represent the template branch, could be not in sync with the actual template branch you just pushed)

If those two points are not an issue, you could go on.
But the (or a more) "proper" way to deal with that would be to have an intermediate bare repo (on the remote server) to push to, and then fetching/pulling the template branch from that bare repo to other server where you need that template project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文