Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rebase instead of fast forward

I'm new to GIT and I can't figure out if my question already has an answer. Please read it carefully before marking it as duplicate.

I have a branch, let's call it public, which is pushed and in sync with origin. At one point, I create another branch, private, which pops out of public, because I want to do a lot of work which I want to remain secret. So I only work on private and I do, let's say, 10 commits, until I finally arrive to a state which can be made public.

Now, I obviously don't want to do a fast-forward merge of public because all the intermediary commits would appear on the remote server. How can I do a rebase given that the public branch has not diverged from the point where private was created.

Clearly, I want to create a new commit on the public branch that contains all the modifications from the 10 private commits. I know that translates into a rebase, but I don't know how to do it.

like image 269
Bogdan Alexandru Avatar asked Dec 10 '22 23:12

Bogdan Alexandru


1 Answers

First of all, the fact that public is tracking a remote branch that lives on origin does not mean that the local branch public is equivalent to the remote branch origin/public.

Because of git's distributed nature, you can make a bunch of commits on your local public branch without anything making it to the server, as long as you do not push.

With respect to what you are trying to do given the current state of your system, here are some detailed steps, assuming you do not care about keeping your original 10 commits.

  1. Begin interactive rebase against the current state of the remote server.

    git checkout private
    git rebase -i origin/public
    
  2. An editor will pop up. The specific editor will depend on the platform and the environmental variable settings. Inside this editor, you will see the hashes of your 10 commits, with the word "pick" in the left hand column. For the first row, keep pick. For the subsequent rows, replace pick with squash. Save the file and quit the editor.

  3. At this point, git will have merged the 10 commits into a single commit, so we can merge into public and push.

    git checkout public
    git merge private
    git push
    

If you wanted to keep your original 10 commits on the local private branch, you can instead do a fast-forward merge onto the local public branch and rebase there instead.

git checkout public
git merge private
git rebase -i origin/public
# Do the same rebase steps as above
git push
like image 142
merlin2011 Avatar answered Dec 29 '22 05:12

merlin2011