tags:

views:

49

answers:

1

I've just learned about rebase, and I've been strongly warned not to rebase commits after they have been pushed to a remote. I want to make sure I'm not going to explode my repository

Example commit history

foo               W---X---Y  
                 /         \
master  A---B---C---D---E---Z---F---G
  • Here, all commits on the master branch will likely have been pushed to origin/master.

  • Z is an automatic commit made by git-merge by merging the completed foo branch (Y) with the current master (E).

  • Since the completion of the foo module, master has made some updates (F and G).


Here's how I'd like to use rebase.

It's time to make some updates on the foo branch, but it's out of sync with the master branch.

  1. Is it ok to simply git rebase master to include F and G commits on the latest foo branch?

  2. Can you modify my diagram to show me what my commit history will look like afterward?

+1  A: 
  1. Hypothetically, yes, it is okay to do because git will detect that foo has already merged to master and will fast-forward foo to the state master is in. This is the same thing that git merge master would do, and also means that, quite simply, foo is the same thing as master.

    Knowing that, however, I would just use git merge here, since that makes more logical sense to me (you use merge to do fast-forwards, it even has an --ff-only flag if you want to make sure you're not doing a real merge). The time to use rebase is when you're working on foo and have several commits there (and haven't pushed it) and someone has committed to master. Rebase will bring your branch up to date without doing a merge commit.

  2. I could, but it would be the same diagram, but with foo now pointing to master, or more accurately, commit G. No new commits are created, so the repository would stay the same with the exception of moving what foo references.

PS. I tested this locally on a fake repository to make sure, since using a rebase in this scenario was not something I had tried before.

wuputah
+1 wuputah, thanks. I'm going to do some more testing with rebase and merge. If this ends up working, I'll mark it as accepted :)
macek
@macek: I'd amend the advice to use merge: use the command you actually mean. In this case, they're both going to fast forward, but that's not always the case. So, if you are on one branch and know that you want to incorporate all the content of another branch, use merge. Only use rebase if you actually believe you want to transplant a branch.
Jefromi