1

I have a hard time removing a single line containing some sensitive information from an earlier commit.

Lets say my git log looks like the following

b200bbe (HEAD -> master) A
67a8df7 B
47a6947 C
a55540f D
68b51d5 E

And now I see in one of my testfiles I've created a debug line printing out some sensitive information at the time of commit E

If I just remove the line and commit the change people would be able to see the line if browsing the file at the time between commit E and the new commit.

There are multiple articles explaining how to remove a complete file from the git tree but I can't see to find a way to only remove one line. filter-branch --tree-filter seems to be the closest however I still end up with changes that I need to commit and a git diff would expose the line that got removed from the command.

None of the commits have been pushed yet and no branches have been made since commit E. Is it possible to remove a single line in a file from the complete git tree?

Klam Huggeren
  • 107
  • 2
  • 13
  • Possible duplicate of [How to remove line from commit and put back her to working directory?](https://stackoverflow.com/questions/29155823/how-to-remove-line-from-commit-and-put-back-her-to-working-directory) – phd May 12 '18 at 23:53
  • If you're removing sensitive information from a git history _you must make sure to make it unreachable_. Then when the garbage collector runs it will be removed. – evolutionxbox May 14 '18 at 08:54

1 Answers1

4

You can try with git rebase -i.

Following the example you used in your question and assuming A is in the remote repo but B, C, D and E are not, after using git rebase you will get an screen like this:

pick 67a8df7 B
pick 47a6947 C
pick a55540f D
pick 68b51d5 E

Then you will need to edit commit E line like this:

pick 67a8df7 B
pick 47a6947 C
pick a55540f D
edit 68b51d5 E

Finally, remove the line that you want, commit your changes and use git rebase --continue to go on.

More info at git rebase documentation.

marian0
  • 664
  • 6
  • 15