0

I have checked out in a fix branch and have edited two files (file1.ext, file2.ext) and made in two commits. By mistake I wrote a @todo in another file (file3.ext) and committed it in the first commit with file1.ext. But the todo had nothing to do with the fix. I have not done a push to remote yet!

Now my question is, how can I delete the file3.ext from the first commit?

$ git reflog

1bf987f6 (HEAD -> fix/1000-lorem) HEAD@{0}: commit: [2.0][UI-1000] Fix: lorem ipsum
81123e93 HEAD@{1}: commit: [2.0][UI-1000] Fix: lorem ipsum

My consideration

I would now delete the @todo from file3.ext again and commit. So the file would be at least without todo and therefore up to date. Are there any other, better ways to solve the problem?

MartinTTS
  • 111
  • 8

1 Answers1

1

There are many ways to do something like this. You could just make a third commit to fix the error. If you want to clean up history, this comes to mind:

  1. Branch from the commit prior to your two (the state of the fix branch before you worked on it).

  2. Check out the files from the first commit on the original fix branch: git checkout <commit hash> file1.ext; git checkout <commit hash> file 2.ext

  3. Commit them with the same message you used for the first commit.

  4. Do the same for the second commit, or simply cherry-pick the second commit.

  5. Delete the original fix branch and check it out anew from the remote.

  6. Merge in your second fix branch.

At this point you should have replaced the original fix branch with a new one being identical except for the mistake. Push it up.

isherwood
  • 58,414
  • 16
  • 114
  • 157
  • Thank you! It sounds good and it also sounds very tempting to try it out right away too. Before I do that, check out the current state as a backup first, right? – MartinTTS May 23 '23 at 16:37
  • A convenient backup is `git tag x`. Then you can always `git reset --hard x` to restore HEAD to where it current is. – ikegami May 23 '23 at 17:47
  • The new branch is a backup once you add the commits per steps 3 and 4. If you then see that work in your files you're all good. – isherwood May 23 '23 at 18:44