0

I'm trying to use git rebase -i and it's just confusing... What I want to do is keep the exact contents of my current commit, I just want to show the history as if it was the direct descendant of a previous commit. Say I have these commits where I started working on something in the morning and did a commit before going to lunch and finished it in the afternoon so I wanted to complete it, but I don't want to see the commit where I broke for lunch (this is very simplified, I might commit and branch off to try something else out quick but always mean to introduce the changes back to my branch if it works out):

dbd2032 Add default config file
   |
033fca5 ... break for lunch
   |
05c4e48 Use config file

So I do a git rebase -i HEAD~2, which goes back to the grandparent 05c4e48 and gives me this to edit:

pick 033fca5 ... break for lunch
pick dbd2032 Add default config file

Which I change unintuitively to this:

pick 033fca5 ... break for lunch
squash dbd2032 Add default config file

This works, git opens up my editor with the old '... break for lunch' message which I then have to change to 'Add default config file'. After it's done I end up with a new log and git diff dbd2032 shows no differences:

35a5eec Add default config file
   |
05c4e48 Use config file

This is counter-intuitive to me because I am not actually picking 033fca5, I'm specifically removing that commit from history. If I delete the line though I don't get the changes that were introduced in that commit. Git must be doing file diffs along the way to find the changes... Specifically I'm picking the exact tree represented by commit dbd2032.

Is there any kind of shortcut for what I'm doing? I have the exact contents and commit message I want, I just want to show it as the child of the grandparent (or other descendant).

Jason Goemaat
  • 28,692
  • 15
  • 86
  • 113
  • I tried to give you an answer, but now I'm not completely sure that that is what you want... – Chris Maes Jan 06 '16 at 10:58
  • do you want: the same result as now, but not having to edit a file when using the squash option? – Chris Maes Jan 06 '16 at 10:58
  • Exactly, just keep the tree and commit message exactly like it is, just set the parent to a different commit. I'm just lazy, `git rebase -i` actually makes me edit two files, the rebase one and the commit message at the end. In the most common scenario I don't have to make any changes to the message and I only want to keep the exact contents of my latest commit, in essence squashing everything back to the first commit in the list, but changing the message to the most recent message. – Jason Goemaat Jan 06 '16 at 11:04
  • I added a new answer. git commit --amend is the way to go! – Chris Maes Jan 06 '16 at 11:07
  • did my answer help you, or is it still not what you are looking for? – Chris Maes Jan 06 '16 at 13:09
  • Not what I was looking for, thanks though. I might have another commit or two in there in my scenario. – Jason Goemaat Jan 07 '16 at 05:43

1 Answers1

3

There are two possible scenarios: you finish your work after lunch and you want to amend your changes into the last commit, and rewording the commit, then just use:

git commit --amend

suppose your message is ok, and you just want to add some changes, then you can use the --no-edit option:

git commit --amend --no-edit

this way there will never be 2 commits to be merged or squashed together afterwards.

Chris Maes
  • 35,025
  • 12
  • 111
  • 136