简体   繁体   English

如何推/拉Git rebase

[英]How to push/pull Git rebase

I'd like to use git rebase so as to cleanly merge a feature in the master branch (in less commits or at least at the top of the change log). 我想使用git rebase以便干净地合并主分支中的一个功能(在较少的提交中或至少在更改日志的顶部)。 Note that I'm the only one working on the repository . 请注意, 我是唯一一个在存储库上工作的人

After reading Git workflow and rebase vs merge questions , I found git rebase would be pretty nice and like Micah I'd like to git push rebased changes simply because I'm working on them from different places (ex: my notebook, my home, another PC somewhere...) 看完后Git的工作流程和重订VS合并的问题 ,我发现git rebase会很不错,像米卡我想git push ,只是因为我对他们的工作从不同的地方重订基期的变化(例如:我的笔记本,我的家,另一台电脑......)

So here are two solutions (to the bi-directional ugly merge): 所以这里有两个解决方案(对于双向难看的合并):

  1. Using git push -f to push, and then pulling on other machine, but how to cleanly get the latest version on other machines? 使用git push -f推送,然后拉动其他机器,但如何在其他机器上干净利落地获取最新版本?
  2. Using merge to merge master changes to the feature branch, git push/pull, and once mature, do a single rebase (in one or more commits cleanly) 使用merge将主更改合并到功能分支,git push / pull,并且一旦成熟,就执行一个rebase(在一个或多个提交中干净利落)

(2) would be like below: (2)如下所示:

git co -b feature-a
... change files
git push origin feature-a
... moving to another PC
git pull origin feature-a
... change files
git merge master
... change files (not the "special rebase")
git rebase master
git co master
git merge feature-a
git branch -d feature-a
git push origin :feature-a

Which solution do you think would work? 您认为哪种解决方案有效? I haven't tried either of them so far (mostly by fear of making my log more messy). 到目前为止,我还没有尝试过其中任何一种(主要是因为害怕让我的日志变得更加混乱)。

I always make sure I commit and push (-f) everything from any machine I leave. 我总是确保我提交并推送(-f)我离开的任何机器上的所有内容。

When I arrive at some other machine: 当我到达其他机器时:

 git fetch -v
 git checkout mybranch # Already checked out with old HEAD
 git reset --hard origin/mybranch

This works well because I know the other "me" at different computers consistently commits and pushes before I leave (And therefore there are no unpushed changes on the machine I arrive at) 这很有效,因为我知道在我离开之前,不同计算机上的其他“我”一直在提交和推送(因此我到达的机器上没有未按下的更改)

Remember that git rebase replays changes and creates new commits. 请记住, git rebase重放更改并创建新的提交。 By rebasing and forcing pushes all over the place, you're going against the grain of the tool. 通过重新定位和强制推动整个地方,你将违背工具的粒度。 Note how the "Recovering from an upstream rebase" section of the git rebase documentation begins (with added emphasis): 请注意git rebase文档中的“从上游rebase恢复”部分是如何开始的(更加强调):

Rebasing (or any other form of rewriting) a branch that others have based work on is a bad idea: anyone downstream of it is forced to manually fix their history. 重新定位(或任何其他形式的重写)其他人基于其工作的分支是一个坏主意:它下游的任何人都被迫手动修复其历史记录。 This section explains how to do the fix from the downstream's point of view. 本节介绍如何从下游的角度进行修复。 The real fix, however, would be to avoid rebasing the upstream in the first place. 然而,真正的解决方法是首先避免重新定位上游。

Even though you're the sole developer, you'll still be others (from the perspective of one repo) when working in other clones. 即使您是唯一的开发人员,在其他克隆中工作时,您仍然会成为其他人(从一个回购的角度来看)。 As you've seen, this workflow is a hassle. 如您所见,这个工作流程很麻烦。

Let your changes cook in branches. 让你的变化在分支中烹饪。 When a branch is ready for primetime, then rebase, merge it into master, and delete its topic branch. 当分支准备好进入黄金时段时, 然后重新绑定,将其合并到主服务器中,并删除其主题分支。 Your life will be easiest if you keep branches' lifetimes short and their scopes narrow. 如果你让分支的寿命缩短,范围缩小,你的生活将变得最简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM