0

I did git rebase origin/master on my project to get on track with upstream.

I deleted some files during the conflict resolution (not in the last commit) as I thought I won't need them in my branch. But I do need them.

I could checkout the upstream elsewhere copy those files to my repo and create a commit saying "sorry" but that's just not nice. I'd much rather "undo" the deletion.

I tried running git rebase -i <parent of the deleting commit> according to this answer and mark the deleting commit with e for edit. But I don't see any files staged. All I can do is amend the commit and that only allows me to edit the commit message, from what I know.

So how do I undo the deletion in my past commit?


For simplicity and completeness I'll add the set of commands I used:

git rebase -i <parent_of_the_wrong_commit>
# mark e next to the deleting commit and copy its hash
git reset HEAD^
git checkout -- the/accidentally/deleted/directory
git add .
git commit -c <the_copied_hash>
git rebase --continue
Community
  • 1
  • 1
kub1x
  • 3,272
  • 37
  • 38

2 Answers2

2

All I can do is ammend the commit and that only allows me to edit the commit message, from what I know.

Not true. When your editing a commit like that you can do anything you normally would.

To fix your issue there are two best methods I know

1. Abort the whole thing and try again

Use git reflog to find the commit(s) you knew about before the rebase. then use git reset --hard <commit> to fix master and try again.

2. Fix the rebase as is

Like you described before you can fix the rebase with the interactive flag.

When you are dropped at the command line for the broken commit take note of the commit ID (copy to clipboard).

Then git reset HEAD^ which will undo the broken commit and place those changes into your working directory.

Now stage only the changes you wanted (git add --patch).

Make a commit with those staged changes with either:

  • git commit - new commit with a new message
  • git commit -c <commit from before> - new commit with the same message as before (from clipboard)

Then clean up the bad changes that are still in your working directory with git checkout -- .

With that your ready to finish the rebase with git rebase --continue.

Good Luck

It is also worth noting the possibility that the changes in your commits were not very atomic. If the deletions were separate from other changes this would have been a simple mater of dropping the offending commit during an interactive rebase.

Sukima
  • 9,965
  • 3
  • 46
  • 60
1

Interactive rebasing with the edit stanza as you did was exactly right, you are just confused about commit amending. If you add changes to the index (git add ...) and do a git commit --amend -C HEAD, the current state of the index replaces the state of the commit as it was before, meaning you can add additional changes or undoing of changes to the latest commit. (-C HEAD takes the commit message of the HEAD commit, so the one amended, without the need or ability to edit it)

Vampire
  • 35,631
  • 4
  • 76
  • 102