-2

I accidentally committed a change in a file that does not relates to my current branch and I need to remove this change from my commit history.

This is the file I want to remove fom my history:

enter image description here

How can I achieve this?

Michael Pacheco
  • 948
  • 1
  • 17
  • 25
  • Possible duplicate of [How to remove file from Git history?](https://stackoverflow.com/questions/43762338/how-to-remove-file-from-git-history) – jonrsharpe Aug 07 '19 at 15:04
  • Given that it's one commit behind the head on a branch you haven't pushed to the origin, did you consider just rewriting it (e.g. with an interactive rebase)? – jonrsharpe Aug 07 '19 at 15:04
  • @jonrsharpe Can you explain how to do this? I dont have much experience with git. – Michael Pacheco Aug 07 '19 at 17:09
  • I'd suggest doing some research on the terms I used. – jonrsharpe Aug 07 '19 at 17:21
  • Possible duplicate of [How to remove/delete a large file from commit history in Git repository?](https://stackoverflow.com/questions/2100907/how-to-remove-delete-a-large-file-from-commit-history-in-git-repository) – phd Aug 07 '19 at 18:59
  • https://stackoverflow.com/search?q=%5Bgit%5D+remove+file+from+history – phd Aug 07 '19 at 18:59

1 Answers1

-1

Do you simply want to remove the changes that the commit introduced? In that case use

git revert SHA

This will introduce a new commit without the changes from the particular commit you did not want.

If you have not pushed the commit so far you can also completely remove it from the history via:

 git rebase -i
NewEyes
  • 407
  • 4
  • 15
  • Is there a way to just remove the file `CustomSacReport.java` from commit `7a1e7e9c` without creating new commits or changing anything else? – Michael Pacheco Aug 07 '19 at 16:46
  • I did `git revert 7a1e7e9c` and `git rebase master` but the commit `7a1e7e9c` yet contains the undesired file – Michael Pacheco Aug 07 '19 at 17:07
  • @MichaelPacheco: commit `7a1e7e9c` (its real name is longer than that, but I don't have the full one and let's use the shorter one as it's easier to type) is, forever and ever, exactly the way you made it. What you'll do is make a *new and improved* commit, which *won't* be `7a1e7e9c`, that will be *like* `7a1e7e9c` but not have the new file. Then you'll copy the *last* commit too—I can't see its actual hash ID, let's call it `T` for *tip* instead—to a new commit whose ID isn't `T` but whose content is the same as `T` except that it doesn't have the file you don't want. – torek Aug 07 '19 at 17:19
  • You now switch your *branch name* around so that it identifies this new tip commit. Git now defines your branch as "the commits that lead up to the new tip". Commit `7a1e7e9c` and the old tip are forgotten; the two new commits are the last commits in the branch. This all works *if and only if* no one *else* has commit `7a1e7e9c` yet. – torek Aug 07 '19 at 17:20