1

I committed a piece of code that I wasn't suppose to commit. (thankfully not on the main branch)

The file had a change to use a temp workaround while a dependent system was down, so I could keep developing on the task at hand. Once the system was fixed, I removed the workaround.

Problem is I committed this change on that file, and by the time I realized I did that it was like 5 commits later.

Is there anyway I can remove the listing of that change from the commits, so it doesn't appear under a pull request.

needoriginalname
  • 703
  • 3
  • 9
  • 27
  • Possible duplicate of [git remove old commits](https://stackoverflow.com/questions/50234180/git-remove-old-commits) – phd Sep 28 '18 at 17:43
  • https://stackoverflow.com/search?q=%5Bgit%5D+remove+old+commit – phd Sep 28 '18 at 17:43

2 Answers2

2

Sure.... let's suppose you added the file on my-branch~5. Then this is what you can do:

git checkout my-branch~5
git rm --cached the-file.txt # remove for git only, keep file on working tree
git commit --amend --no-edit # create new revision without the file
git cherry-pick my-branch~5..my-branch # replay old branch history
git branch -f my-branch # set my-branch where I am right now
git checkout my-branch # check it out so I can continue working
msanford
  • 11,803
  • 11
  • 66
  • 93
eftshift0
  • 26,375
  • 3
  • 36
  • 60
0

Is the issue the commit history or the commit's code?

I don't know if you can erase a commit history. I actually would not suggest if you could. But you can:

  • Reverse your commit.
  • Or simply commit the file again without your changes.

Any change will be automatically considered in your PR.

Cleriston
  • 750
  • 5
  • 11
  • its the commit's history. – needoriginalname Sep 28 '18 at 16:51
  • But why? Just refactor your code and move on. Trust me, it will make your live much easier. – Cleriston Sep 28 '18 at 16:52
  • @Cleriston why do you want to keep a revision that is _wrong_ on a private branch if you can clean it up before publishing it? It's fine if you like it that way, but there's no need for it and git provides the tool to correct it. – eftshift0 Sep 28 '18 at 17:00
  • Maybe if you: Get the commit before the one you want to remove. Create a branch from that. Cherry pick the commits after the one you want to remove. Switch the PR source to be the new branch you created. Delete the old branch with the wrong commit. – Cleriston Sep 28 '18 at 17:20
  • reason being is that this is a codebase in production, and the code I am working on is purely test cases. But if there was any change detected where working production code would occur, it would have for scrutiny when reviewd. And the piece of code I didn't want to commit was the working part of the code. – needoriginalname Sep 28 '18 at 22:55