0

I have a few pushed commits, this is what my remote branch looks like:

Date: June 25, SHA: 123, files: a,b, message: fix code feedback
Date: June 24, SHA: 234, files: b, message: move pixel margins
Date: June 23, SHA: 345, files: a,b,c,d, message: Initial Commit

I have accidentally committed file "d" in the initial commit which I want to remove from the commit. How do I do this? I am the only one working on this branch.

Justin
  • 137
  • 2
  • 10
  • You may have to commit an empty orphan and then rebase onto that? – evolutionxbox Jun 25 '21 at 23:00
  • Does this answer your question? [How to modify a specified commit?](https://stackoverflow.com/questions/1186535/how-to-modify-a-specified-commit) – IMSoP Jun 25 '21 at 23:00
  • As you want to modify the root commit of your repo, you could do an interactive rebase and edit the desired commit. Since you need to modify the root, you'll need to add the `--root` switch to make the changes. – Jeff Mercado Jun 26 '21 at 03:20

1 Answers1

1

Two facts:

  • You cannot change a commit.

  • Commits are not about what files you added or edited. Every commit contains all your files.

So, if you committed d in your initial commit, then unless you committed a deletion of d in a subsequent commit, all your commits contain d.

In order to pretend that that didn't happen, you would have to rewrite your entire history — replacing all your commits. That's a somewhat fraught proposition, but there are tools that can do it.

https://github.com/newren/git-filter-repo

If there really are only three commits in your entire history, you might be better off just starting over.

matt
  • 515,959
  • 87
  • 875
  • 1,141