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.
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.
Begin interactive rebase against the current state of the remote server.
git checkout private
git rebase -i origin/public
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With