Rebasing a branch which is public

I'm failing to understand how to use git-rebase, and I consider the following example.

Let's start a repository in ~/tmp/repo:

$ git init

Then add a file foo

$ echo "hello world" > foo

which is then added and committed:

$ git add foo
$ git commit -m "Added foo"

Next, I started a remote repository. In ~/tmp/bare.git I ran

$ git init --bare

In order to link repo to bare.git I ran

$ git remote add origin ../bare.git/
$ git push --set-upstream origin master

Next, lets branch, add a file and set an upstream for the new branch b1:

$ git checkout -b b1
$ echo "bar" > foo2
$ git add foo2
$ git commit -m "add foo2 in b1"
$ git push --set-upstream origin b1

Now it is time to switch back to master and change something there:

$ echo "change foo" > foo
$ git commit -a -m "changed foo in master"
$ git push

At this point in master the file foo contain changed foo, while in b1 it is still hello world. Finally, I want to sync b1 with the progress made in master.

$ git checkout b1
$ git fetch origin
$ git rebase origin/master

At this point git st returns:

# On branch b1
# Your branch and 'origin/b1' have diverged,
# and have 2 and 1 different commit each, respectively.
#   (use "git pull" to merge the remote branch into yours)
#
nothing to commit, working directory clean

At this point the content of foo in the branch b1 is change foo as well. So what does this warning mean? I expected I should do a git push, git suggests to do git pull... According to this answer, this is more or less it, and in his comment @FrerichRaabe explicitly say that I don't need to do a pull. What's going on here? What is the danger, how should one proceed? How should the history be kept consistent? What is the interplay between the case described above and the following citation:

Do not rebase commits that you have pushed to a public repository.

taken from pro git book.

I guess it is somehow related, and if not I would love to know why. What's the relation between the above scenario and the procedure I described in this post.


The reason you do not want to rebase commits that you have pushed to a public repository is because the git-rebase command changes history.

Now, what does that mean and why is it bad? First, I would suggest reading this section of the Git book. From that you'll learn that commits consist of a pointer to a tree object (snapshot of the files) and a pointer to the parent commit. Now when you "change history" by rebasing commits on top of new commits, you're changing the parent pointer of commits you've already made, which in turns changes the id of your commits.

The reason this is bad, is that if you share your commits publicly, and others start additional work based on those commits, then you go an change those commits, your trees are no longer in sync.

You can see all of this by issuing some git-log commands as you perform your example. I ran these right before running the rebase command:

$ git log --pretty=oneline origin/master
9b077261d1619803213201d5c7cefb757eb66b67 Changed foo in master
911ce5b247e79682ec9f73ad9a15fd3167b7e76d Added foo

$ git log --pretty=oneline origin/b1
63a57ef54e301314a9dab38de0cd9d88c59a5fba added foo2 in b1
911ce5b247e79682ec9f73ad9a15fd3167b7e76d Added foo

$ git log --pretty=oneline b1
63a57ef54e301314a9dab38de0cd9d88c59a5fba added foo2 in b1
911ce5b247e79682ec9f73ad9a15fd3167b7e76d Added foo

And now after performing the rebase, origin/master and origin/b1 are the same, but b1 is now:

$ git log --pretty=oneline b1
6687c64c37db0ee21a4d87e45d6ccb0913b8686d added foo2 in b1
9b077261d1619803213201d5c7cefb757eb66b67 Changed foo in master
911ce5b247e79682ec9f73ad9a15fd3167b7e76d Added foo

You'll notice that the "added foo2 in b1" commit has a different id than in the previous log commands. If you commit this change to your public repo, you now have two commits that have the same work done in them and it causes problems.

Now assume, instead of rebasing b1 on top of master, you just merged master into b1, your log would look like this:

$ git checkout b1
$ git merge origin/master
$ git log --pretty=oneline b1
518eb2dc6b2da0ff43ddd6837332031cc00eaad1 Merge remote-tracking branch 'origin/master' into b1
9b077261d1619803213201d5c7cefb757eb66b67 Changed foo in master
63a57ef54e301314a9dab38de0cd9d88c59a5fba added foo2 in b1
911ce5b247e79682ec9f73ad9a15fd3167b7e76d Added foo

You'll notice the extra commit that represents the merge of the two previous commits. This history can now be shared and everyone will be happy.

git-log --graph can also help to shed some additional light as to what is going on.


Late at the party, but here is the answer for posterity:

git rebase is meant to be used locally. It re-writes history, which allows for a very nice "main line", but is dangerous in a multi-user environment

If:

  • you are the only one using the remote repository,
  • AND you are using it in one workspace only,

Then it might make sense to do this by forcing the history to be re-written. Do your rebase and then:

git push -f origin remotebranch

If any of these assumptions was not met, you may live to regret this action :-)

Read more in this blog post