3

Let A<-B<-C be a sequence of Git commits.

Commits B and C introduce 3 types of changes:

  1. Adding some files.
  2. Modifying some files.
  3. Deleting some files.

Unfortunately, the changes of type 2 and 3 in both B and C turned out to be incorrect. Therefore we need to derive a modified sequence A<-B'<-C', where B' and C' only include the file additions from the original A and B, discarding any Modifications or Deletions.

How can that be done?

If it helps, we don't absolutely need B' and C' as separate commits; we can do with just A<-D, where D contains every file added in either B or C.

Dun Peal
  • 16,679
  • 11
  • 33
  • 46

1 Answers1

2

You can try resetting to A, and add all new files:

# reset index, reset HEAD to A, preserve working tree.
git reset A

# Add only new files
git add $(git ls-files -o --exclude-standard)

# Make commit D (equals B and C new files)
git commit -m "only new files from B and C"

For the second step (adding only new files), see "Git add only all new files, not modified files".


The OP Dun Peal comments:

C removes some of the files introduced by B. Thus the C working copy does not include all the files introduced by B. I guess I could jut iterate over the commits one at a time, and perform this for each one.

Yes, an iterative approach is needed:

git reset --hard B
git reset A
git add $(git ls-files -o --exclude-standard)
git commit -m "only new files from B"

That produces commit B'.

Repeat for C, using B' as a base:

git reset --hard C
git reset B'
git add $(git ls-files -o --exclude-standard)
git commit -m "only new files from C"
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks, but there's a slight complication: `C` removes some of the files introduced by `B`. Thus the `C` working copy does not include all the files introduced by `B`. I guess I could jut iterate over the commits one at a time, and perform this for each one. – Dun Peal Dec 28 '13 at 21:27
  • 1
    @DunPeal I agree. I have edited the answer to give an idea of what said iterative approach would look like. – VonC Dec 28 '13 at 21:49