git rebase 到远程更新

发布于 2024-09-03 02:44:46 字数 688 浏览 5 评论 0原文

我与一个使用 git 进行源代码管理的小团队合作。最近,我们一直在做主题分支来跟踪功能,然后将它们合并到本地主库中,然后将它们推送到远程服务器上的中央 git 存储库。当 master 中没有进行任何更改时,这非常有效:我创建主题分支,提交它,将其合并到 master 中,然后推送。万岁。

然而,如果有人在我之前推送到原点,我的提交就不会快进。因此,合并提交随之发生。当主题分支需要与本地主分支合并以确保我的更改适用于现在的代码时,也会发生这种情况。因此,我们最终会得到无处不在的合并提交和与友谊手镯相媲美的 git 日志。

因此,变基是显而易见的选择。我想要的是:

  • 创建包含多个提交的主题分支
  • 签出 master 并拉动(快进,因为我还没有提交到 master)
  • 将主题分支变基到 master 的新头
  • 将主题变基到 master 上(因此主题从 master 开始) head),将主人带到我的主题 head

我目前的做法如下:

git checkout master
git rebase master topic_1
git rebase topic_1 topic_2
git checkout master
git rebase topic_2
git branch -d topic_1 topic_2

有没有更快的方法来做到这一点?

I work with a small team that uses git for source code management. Recently, we have been doing topic branches to keep track of features then merging them into master locally then pushing them to a central git repository on a remote server. This works great when no changes have been made in master: I create my topic branch, commit it, merge it into master, then push. Hooray.

However, if someone has pushed to origin before I do, my commits are not fast-forward. Thus a merge commit ensues. This also happens when a topic branch needs to merge with master locally to ensure my changes work with the code as of now. So, we end up with merge commits everywhere and a git log rivaling a friendship bracelet.

So, rebasing is the obvious choice. What I would like is to:

  • create topic branches holding several commits
  • checkout master and pull (fast-forward because i haven't committed to master)
  • rebase topic branches onto the new head of master
  • rebase topics against master(so the topics start at masters head), bringing master up to my topic head

My way of doing this currently is listed below:

git checkout master
git rebase master topic_1
git rebase topic_1 topic_2
git checkout master
git rebase topic_2
git branch -d topic_1 topic_2

Is there a faster way to do this?

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

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

发布评论

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

评论(4

雨轻弹 2024-09-10 02:44:47

随着时间的推移,我发现我最喜欢的解决方案是:

git checkout topic
# make [n] commits
git rebase -i HEAD~[n] #clean up time
git checkout master
git pull --rebase
git checkout topic
git rebase master
git checkout master
git merge topic
git push origin

I have found, over time, my favorite solution is:

git checkout topic
# make [n] commits
git rebase -i HEAD~[n] #clean up time
git checkout master
git pull --rebase
git checkout topic
git rebase master
git checkout master
git merge topic
git push origin
三月梨花 2024-09-10 02:44:47

您还可以仅针对最新的 master 进行变基

git checkout topic_1
git rebase refs/remotes/origin/master

,这消除了拉取的需要(至少直到您的功能分支的 EOL 为止)。我们的流程使用 GitHub 拉取请求,因此我永远不需要在本地执行此操作。

You can also just rebase against the up-to-date master

git checkout topic_1
git rebase refs/remotes/origin/master

Which obviates the need for the pull (at least until the EOL of your feature branch). Our process uses GitHub pull requests so I never need to do that locally.

維他命╮ 2024-09-10 02:44:47

对于我们的团队,我们设置了一些效果很好并且根本不使用 rebase 的东西。

我们减少了 Jira 票证的工作量,不需要太长时间,通常需要 1-2 天的时间。每个开发人员为每个票证创建一个分支并在该分支上工作。当准备好共享时,该信息被推送到中央服务器。

中央服务器由 hudson CI 服务器监控,该服务器提取更改、合并所有更新的分支、重建软件、运行测试并将所有内容推送到中央主 git 存储库。

从那里我们将其拉回我们的存储库。我们定期(即每隔几天)将我们的工作分支与中央主站合并,以保持它们的“关闭”。

由于没有人在“合并”机器上工作,并且除了 CI 服务器之外没有人接触主服务器,因此我们很少会遇到合并问题(大约占提交的 1-2%)。通过清理工作区,这些问题可以在构建服务器上快速解决。我们发现,通过保持分支较短并在推送之前与远程主服务器合并,我们可以避免其中的大部分问题。

我还喜欢合并比变基更强大,并且需要更少的返工。

For our team we set something up which works very well and does not use rebase at all.

We cut our work in Jira tickets which do not take too long, typically 1-2 days effort. Each dev creates a branch per ticket and works on this branch. When ready to share this is pushed to the central server.

The central server is monitored by a hudson CI server which pulls the changes, merges all updated branches, rebuilds the software, runs the tests and pushes everything to the central master git repository.

From there we pull it back to our repositories. We do regularly (i.e. every couple of days) merge our working branches with the central master to keep them 'close'.

Since nobody is working on the 'merging' machine and nobody other than the CI server touches the master, we have very infrequently merging issues (in about 1-2% of the commits). And these are quickly resolved on the build server by cleaning the workspace. We found we could have avoided most of these by keeping the branches short and merging with the remote master before pushing.

I also fond that merging is much more robust than rebasing and requires a lot less of rework.

狠疯拽 2024-09-10 02:44:46

你知道git pull --rebase吗?当您拉取时,它会变基而不是合并,并防止合并提交污染您的历史记录。

您还可以使用 branch..rebasebranch.autosetuprebase 配置选项将其设置为分支的默认行为。

Do you know about git pull --rebase? It rebases rather than merging when you pull, and prevents merge commits from polluting your history.

You can also set it up as the default behaviour for a branch with the branch.<name>.rebase and branch.autosetuprebase config options.

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