3

In one repo, I have a commit (about 3 behind the HEAD) that has a large number of file deletions that were accidentally staged and included in the commit. Depending on the file type, they appear like either of the following when executing git log --stat:

path/to/some/file                     | Bin 22522 -> 0 bytes
path/to/another/kind/of/file          | 1 -

I would like to reverse these deletions and not have any of them appear in my commit. (Note: I'm not trying to hide the deletions, so if there is a method that reverses them, that is fine, too. I just don't want my PR to merge in these file deletions.)

If there's something I should do to update the remote after reversing the file deletions, that would be helpful as well.

zahabba
  • 2,921
  • 5
  • 20
  • 33

1 Answers1

4

This has nothing to do with moving HEAD, but selectively cancelling (in Git linguo, "reverting") some files from a commit.

In your case, the @~3 commit includes deletions you don't want, but also other files (you might not want to revert to).

git revert --no-commit @~3 # Revert, don't commit it yet
git reset                  # Unstage everything

# add only the files you need (ie the one created, not the ones modified)
git add $(git ls-files -o --exclude-standard)

Here you want, after reverting the all commit @~3, to select only the untracked files (that is, the files created again by git revert, since those creation cancel what was deleted in @~3).
See "Git add only all new files, not modified files".

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • does anything change if those files that were deleted were previously untracked but added to the stage by `git add .`? I would love to go back and remove them from being staged, because they never should have been tracked. – zahabba Jan 30 '17 at 23:59
  • 1
    @zahabba That is different: to be sure they were never tracked (meaning they are not in any commit of the repo history) you would need a `git filter-branch` (https://help.github.com/articles/removing-sensitive-data-from-a-repository/), which would rewrite said history. `git revert` is less intrusive, creating one new commit, as opposed to `git filter-branch`, changing past commits. – VonC Jan 31 '17 at 05:06