简体   繁体   English

Git - Merge vs Rebase用于我的工作流程

[英]Git - Merge vs Rebase for my workflow

I've been doing some reading on how both the git merge and git rebase operations work, and I think I have a very basic understanding of the differences. 我一直在阅读关于git mergegit rebase操作如何工作的一些内容,我认为我对这些差异有一个非常基本的了解。 I've seen the diagrams :-) Despite that, I'm still not clear on what would be the best of the two to use for my current worflow. 我已经看过图表了:-)尽管如此,我仍然不清楚两者中最好用的是什么才能用于我目前的工作流程。

My work is using perforce as it's SCM system, but I'm using git locally to keep track of local changes, do refactoring, and a bunch of other cool stuff that git can bring to bring to the table. 我的工作是使用perforce作为它的SCM系统,但我在本地使用git来跟踪本地更改,进行重构,以及git可以带来的一堆其他很酷的东西。 I know there already exists a tool to help facility working with git and perforce (ex p4-git) but I don't necessarily want/need that overhead, so I'm trying to keep things as simple as possible. 我知道已经存在一个工具来帮助设备使用git和perforce(ex p4-git),但我不一定想要/需要这些开销,所以我试图让事情变得尽可能简单。 Here's a brief description of my current workflow for creating local git branches and eventually integrating back into our main perforce depot: 以下是我目前创建本地git分支并最终集成到我们的主要perforce仓库的工作流程的简要说明:

  1. I have a master git branch, which does a nightly p4 sync to our codebase. 我有一个 git分支,它与我们的代码库进行夜间p4同步 After the perforce sync, I commit all changes to the master branch. 在perforce同步之后,我将所有更改提交到主分支。 In effect, my master git branch is essentially a snapshot of the latest code committed to our perforce mainline. 实际上,我的 git分支实际上是提交给我们的perforce主线的最新代码的快照。

  2. For local changes I'm working on, I always create a git branch first, and checkout this branch while working on the change. 对于我正在进行的本地更改,我总是首先创建一个git分支 ,并在处理更改时检查此分支。

  3. Every now and then I want to update my branch to the latest from the master . 时不时地,我想更新我的分支从最新的。 Until now I've just been issuing a git merge master command to do that and it's been working out fine. 到目前为止,我刚刚发布了一个git merge master命令来做到这一点,并且它一直很好。

  4. When I'm ready to commit to the actual perforce depot, I merge my branch back into my master branch, by checking out the master and issuing git merge BRANCH and then submit using regular perforce commands 当我准备好承诺实际的perforce仓库时,我将我的分支合并回我的分支,通过检查主服务器并发出git merge BRANCH然后使用常规perforce命令提交

Given my workflow, should I really be using a git rebase master command for Step#3 instead of a git merge master ? 鉴于我的工作流程,我是否真的应该使用步骤#3的git rebase master命令而不是git merge master From my understanding of the rebase command, this would only be necessary if say our perforce mainline (remote depot) was branched, and I wanted to create a new master based off this branch (say I call it master-newbranch) and apply my changes to this new branch. 根据我对rebase命令的理解,只有当我们的perforce主线 (远程仓库)被分支时,这才有必要,并且我想基于这个分支创建一个新的主人 (比如我称之为master-newbranch)并应用我的更改到这个新的分支。 I would need to rebase off this branch first? 我需要先衍合过这个分支?

In general, does my current workflow make sense, or have I already picked up some bad habits? 一般来说,我目前的工作流程是否有意义,或者我已经养成了一些坏习惯?

I have much the same workflow and prefer using git rebase -i master. 我有相同的工作流程,更喜欢使用git rebase -i master。 This keeps all the perforce resyncs at the bottom of the change list. 这使所有perforce resyncs保持在更改列表的底部。 Thus it appears I checked out the latest version and made a ton of changes. 因此,我看来我检查了最新版本,并进行了大量的更改。 Also only changes that pertain to the branch show up after a resync. 此外,只有在重新同步后才会显示与分支相关的更改。 Seems more intuitive, but it sounds more like style thing than a correctness thing. 看起来更直观,但听起来更像是风格而不是正确的东西。 ~Ben 〜奔

You should not (necessarily) be using rebase over merge in this case. 在这种情况下,你不应该(必然)使用rebase而不是merge。 Remember, rebase essentially re-writes your history. 请记住,rebase基本上会重写您的历史记录。 It's useful in cleaning up multiple branches and providing a more linear history, but from your use case you're not gaining anything by using rebase. 它有助于清理多个分支并提供更线性的历史记录,但从您的用例中,您不会通过使用rebase获得任何东西。 The behavior you've described is a normal git workflow which merge was designed for. 您描述的行为是一个正常的git工作流,其合并是为此而设计的。

The tricky thing about rebase is when you're rebasing (re-writing history) commits that you've already pushed. 关于rebase的一个棘手的事情是当你重新定位(重写历史)你已经推动的提交时。 This can cause major headaches in collaboration with others, but you're using perforce for collaboration, so you're unlikely to run into this problem. 这可能会导致与其他人合作时出现严重问题,但您正在使用perforce进行协作,因此您不太可能遇到此问题。

Rebase is rewriting your history. Rebase正在重写您的历史记录。 So really depends on how you want to keep your history. 所以真的取决于你想如何保持你的历史。 Rebase keeps history more clearer in general. Rebase使历史记录更加清晰。

History shows what has happened in your project. 历史记录显示了项目中发生的情况。 But, you don't have to expose everything you have done. 但是,您不必暴露您所做的一切。 Imagine you are writing a book. 想象一下,你正在写一本书。 You don't have to show users your writing history including draft versions. 您不必向用户显示您的写作历史记录,包括草稿版本。

In my case, I usually prefer rebase over merge when it comes to merging master to branches. 就我而言,在合并master和branches时,我通常更喜欢rebase而不是merge。

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

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