Josh Karabin
Josh Karabin

Reputation: 130

Inserting an ignored commit between two commits

I have a clone of a public git repository. Simplified, the commits (A-D) look like this:

   +->D
   |
A->B->C

Commits A and B are already in the public repo. I added commit C to the clone that includes significant coding standard changes as well as significant feature changes. I later decided that I wanted to add a commit D to the clone that corrects only the coding standard issues in commit B.

I'd like to reorder commit C to come after commit D, so that things look like this:

A->B->D->C

Because I've already made the coding standard changes in both commits, I'd like to tell git to accept commit C as-is with no merging. I haven't been able to figure out how to do this. Can anyone suggest the right approach?

I had thought that git-rebase with a merge strategy our "ours" would do the trick, but am not having luck with that.

Upvotes: 3

Views: 81

Answers (3)

user743382
user743382

Reputation:

git checkout C
git reset --soft D
git commit

should be sufficient. git reset --soft merely resets what git thinks the "current" commit is, without actually making any changes whatsoever. So your index and worktree will still be exactly how they were in C, and you can commit that without needing to make any further changes.

Note that nothing in these commands will cause any sort of merge or automatic conflict resolution to take place. This means that if there is any part of D that isn't already part of C, this will cause those changes of D to be effectively reverted. You've stated that that's not the case, that D merely contains bits of C, so that shouldn't be a problem.

Upvotes: 3

bitoiu
bitoiu

Reputation: 7484

I think this is as simple as:

git checkout D
git cherry-pick <commitIDofC>

Resolve any missing conflicts. The commit contents won't be exactly the same because they just can't be, the history at B and the history after B->D can't be the same so if you touch the same areas of the code they can't be the same.

Please try this out and let me know.

Cheers.

Upvotes: 1

Jeow Li Huan
Jeow Li Huan

Reputation: 3796

I use SourceTree as the GUI for GIT Assuming commit C is the master branch

  1. Checkout commit D as "temp"
  2. Select all the files in commit C, right click and "reset to commit"
  3. Uncommitted changes should now show your feature changes
  4. Commit your changes as E
  5. Delete "master" branch
  6. Checkout "temp" branch as "master"

You get the A->B->D->E, where E is C without the changes in D.

Upvotes: 1

Related Questions