Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git rebase --merge with only some of the commits

Tags:

git

I did a few commits, say commit A, B, C Now I want to push only commit A and B to the remote repo. (And keep commit C to be merged and pushed at a later time) Before I push, I need to run:

git fetch origin master
git rebase --merge FETCH_HEAD

problem is the above commands will ask me to resolve conflicts of commit C as well, which I'm not planning to push now.

Is it possible to do the rebase without having to deal with commit C.

Thanks

like image 974
AmirW Avatar asked Jun 28 '12 18:06

AmirW


2 Answers

Saving before the rebase

The easiest way to save the commit so you can postpone dealing with the conflicts would be to tag it so you have a reference to the commit later:

$ git tag save-commitC <sha1>

Rebasing without commit C

After you've saved a reference, rebase without the commit:

$ git rebase --interactive --merge FETCH_HEAD

It should open your editor to file looking like this:

pick <sha1> <message>
pick <sha1> <message>
pick <sha1> <message>
...

To delete commit C from this branch's history, simply remove or comment out the commit's pick <sha1> <message> line.

Note that commits are listed in reverse order, with the oldest commits first, the most recent ones last. The first commit on the list should be the first commit that is not an ancestor of FETCH_HEAD.

Once you've rebased without C, go ahead and push:

$ git push origin master

Reapplying the commit

When you're ready to apply it on the rebased branch:

$ git cherry-pick save-commitC
   # resolve merge conflicts
$ git commit
$ git tag -d save-commitC
like image 200
vergenzt Avatar answered Oct 19 '22 16:10

vergenzt


You could reset to commit b, then stash your changes, push, and then reapply your stash.

git reset HEAD~1
git stash
(rebasing or whatever other intermediate steps required go here)
git push
git stash pop
like image 30
limscoder Avatar answered Oct 19 '22 17:10

limscoder