motam79 motam79 - 20 days ago 6
Git Question

Picking the commits to merge with master

Say I have a buggy function in my master branch:

def foo(x):
return 1/x
git commit -a -m "foo is created"


I create a new branch called
bug
to debug it.

git checkout -b bug


I create some print statements for debugging, and commit:

def foo(x):
print x
return 1/x
git commit -a -m "print statements are added for debugging"


Finally the bug is fixed.

def foo(x):
print x
if x == 0:
return None
return 1/x
git commit -a -m "foo bug is fixed"


Now I would like to rebase the second commit on the
bug
branch with
master
, but I don't like to add the print statements (i.e. the first commit), so I use the interactive rebase as follows:

git rebase -i master

drop b2296f0 printing
pick 62beaa8 fixed


and only select the second commit (i.e. bugfix), However I get this conflict:

def foo(x):
<<<<<<< HEAD
=======
print x
if x == 0:
return None
>>>>>>> 62beaa8... fixed
return 1/x


Is there a way to have git get the correct version, without me manually deleting all debug print statements?

Answer

Your checkout and rebase targets should be the other way around. The rebase command can be articulated with wording to the effect of "unwind from my current position up to the commit specified".

Furthermore, as you'll already be on the feature/bugfix branch you can ignore the checkout and instead just do

git rebase -i master

and pick/squash/fixup the desired commits. When done you can then merge in:

git checkout master git merge bug

If instead you want the branch to be preserved in the git tree you can merge in with a specific merge commit (instead of doing a fast-forward):

git merge --no-ff bug