4

Problem

Say that I have done some work on master:

c1 <- c2
       ^
     master

At this point, I branch off, and do some more work:

                test                  
                 v
          c3 <- c4
c1 <- c2 /
       ^   
     master

Then do a git merge --no-ff (--no-ff to keep the branch history):

                test                  
                 v
          c3 <- c4
c1 <- c2 /        \ [c5]
                      ^   
                    master

([c5] is a merge commit)

I then delete the test branch, thinking that I have finished with it. However, I find that, after all, there is a problem with the work I've done on test. I need to amend c3, but without harming the branch history.


Things that I've tried


My attempt

I git checkout c3; then change what I need to change, and then git commit --amend. Then I checkout the master branch, and git branch temp c3. I then git rebase --preserve-merges temp: this gives me a merge conflict, which I fix, and then git add <file> and git rebase --continue.

However, when I look at the branch history with git log --oneline --graph, I see something like the following:

*  [c5]
|\
| * c4
|/
* c3'
* c2
* c1

It should look more like this:

*  [c5]
|\
| * c4
| * c3'
|/
* c2
* c1

where c3' is the amended commit.

How can I get this behaviour out of git?

Tim's suggestion

git checkout c3
git checkout -b test
git commit --amend
git checkout master
git reset --hard c2
git merge --no-ff test

Unfortunately, this doesn't seem to work, as we lose c4. (Please correct me, Tim, if I'm misinterpreting your answer.) The resulting branch history from git log --oneline --graph is as follows:

*  [c5]
|\  
| * c3'
|/  
* c2
* c1

where [c5'] is a new merge commit. (I don't mind that it's new, by the way.)

6
  • Did you try just branching off c3 and merging to master?
    – jthill
    Feb 17, 2016 at 23:42
  • Is this just your local master? Or have you pushed anything >= c3?
    – Massey101
    Feb 18, 2016 at 0:39
  • git checkout c3 will enter a "detached header" status, make modification on this status is not recommended. If you want to modify commit history, you should use git rebase -i
    – gzh
    Feb 18, 2016 at 0:55
  • Just my local master, @Massey101; nothing has (yet) been pushed to a remote.
    – digitalis_
    Feb 18, 2016 at 9:50
  • @Tim (and all other viewers): I have made a bash script for making a test repo identical to the hypothetical one in the question. You can find it here, on GitHub: gist.github.com/danieloosthuizen/f9009b65ce9d833b268c
    – digitalis_
    Feb 18, 2016 at 13:23

3 Answers 3

1

You could use an interactive rebase:

git checkout master
git rebase -i c2

This should open an editor with one line per commit. Change the pick in c3's line to edit. Save and close. Now git rewinds you back to c3 and will stop there for you to make an edit. Change what you want to change and amend with

git commit --amend
git rebase --continue
4
  • When the editor opened, it only showed me the line pick c4 <message>; after I aborted the rebase, it seemed to have flattened the branch history.
    – digitalis_
    Feb 18, 2016 at 13:01
  • @digitalis_ sorry, I made a mistake. Updated my answer (git rebase -i c3). For your flattened branch history: that's weird, an aborted rebase should not do anything. I hope you can restore it somehow? Feb 18, 2016 at 13:12
  • 1
    Don't worry @Tim, I was using a test repository – no data was lost. I seem to have found an answer to my question using an interactive rebase, as you suggested. Thanks for the help!
    – digitalis_
    Feb 18, 2016 at 13:52
  • with regards to the weird flattening: I think I must have :wqed and then git rebase --aborted, which may have been the cause of the flattening.
    – digitalis_
    Feb 18, 2016 at 13:55
1

I appear to have found an answer! Thank you, Tim, for the inspiration to try an --interactive rebase.

Basically, the answer lies in doing a git rebase -p -i <root>; I'm calling the commit right before where the branch history forks the <root>. In the hypothetical case of the question, the <root> is c2.

The -p is shorthand for --preserve-merges, which will stop git rebase from removing merge commits.

An editor will pop up with something like the following:

pick c3 <message>
pick c4 <message>
pick [c5] <message>

Change the pick before c3 to edit so that you have:

edit c3 <message>
pick c4 <message>
pick [c5] <message>

Save and quit. Make the changes that you want to make, add them, and git commit --amend. After this, you need to git rebase --continue to continue the rebase.

You may get merge conflicts during this time. Just fix the conflicts, git add the changed files to mark the conflict as fixed, then git rebase --continue. Repeat this until git tells you that the rebase has been successful.

2
  • oh ya, I forgot the -p. Glad you figured it out! Feb 18, 2016 at 14:53
  • 1
    Thanks @Tim; your answers (and patience) helped a lot, though!
    – digitalis_
    Feb 18, 2016 at 16:01
0

As shown in your question, when you make c4 commit in test branch and merge it to master branch, fast-forward mode will only move master reference to c4, why do you think you will lost the branch history and use --no-ff option purposely?

About what you have tried, It seems that you made modification to timeline of master branch timeline, so the commit graph is a obvious consequence.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.