3

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. 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. 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. Here's a brief description of my current workflow for creating local git branches and eventually integrating back into our main perforce depot:

  1. I have a master git branch, which does a nightly p4 sync to our codebase. After the perforce sync, I commit all changes to the master branch. In effect, my master git branch is essentially a snapshot of the latest code committed to our perforce mainline.

  2. For local changes I'm working on, I always create a git branch first, and checkout this branch while working on the change.

  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.

  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

Given my workflow, should I really be using a git rebase master command for Step#3 instead of a 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. 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?

4
  • 1
    Very interested to see how this question goes - can't comment on your actual question, but would it not be prudent to do a p4 sync to master immediately before you try to merge your branch into master? Commented Jan 5, 2011 at 20:46
  • 1
    Something that comes to mind is this post from Junio C. Hamano (the maintainer of git). It's intended for pure-git users, not perforce-git situations like you're in, but it still may offer some insight.
    – Tyler
    Commented Jan 6, 2011 at 7:01
  • MatrixFrog - very interesting article, thanks for the link! After reading it, I think I generally agree with what Junio is saying, I shouldn't really by refreshing my branch with the latest changes from master. Instead, when the feature is ready to go I can merge it back into the master before submitting. The only issue with this is that I don't have my "data" under git control (20GB+ of binary data to track), and occasionally data will change which breaks the code (bad coding, I know!) which forces me to refresh my branch from the master.
    – Lucky Mike
    Commented Jan 6, 2011 at 22:25
  • cristobalito - You're correct. I left that step out but yes I always do a p4 sync in the master immediately before bringing in the branch. That reminds me of the other reason I first need to merge the master into the branch - when I do a "diff" between the master and branch I only want to see the files I've changed, not everything that's changed between when I branched and what's been updated in the master. After that, I can do this trick "git diff branch master --name-only | p4 -x - open" which will get git to spit out filenames of anything I've changed, and then open them with perforce
    – Lucky Mike
    Commented Jan 6, 2011 at 22:30

3 Answers 3

1

You should not (necessarily) be using rebase over merge in this case. Remember, rebase essentially re-writes your history. 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. The behavior you've described is a normal git workflow which merge was designed for.

The tricky thing about rebase is when you're rebasing (re-writing history) commits that you've already pushed. 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.

1
  • It seems like rebase is unnecessary and probably undesirable in my use-case. It's good for me to be able to look back at my local history to track my changes/progress (one of the reasons I added git to my workflow in the first place).
    – Lucky Mike
    Commented Jan 7, 2011 at 18:45
1

I have much the same workflow and prefer using git rebase -i master. This keeps all the perforce resyncs at the bottom of the change list. 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

0

Rebase is rewriting your history. So really depends on how you want to keep your history. Rebase keeps history more clearer in general.

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.