Git: modify code in previous commit to remove sensitive info

Viewed 63

I have a private repo where I developed a bunch of scripts for my personal use. I then got sloppy and pushed info without writing a gitignore and left my personal access tokens hardcoded directly into the script. Now, quite a few people are asking for the scripts and so I've decided to publish the git repo. However, I understandably need to remove the access tokens from the git history. I'm the only person that uses the repo as of right now.

I do, however need the commits to stay separate for a host of other reasons (so I can't make the change and squash all commits). I understand that I can remove the files involved, but the access tokens are in the main script file itself. Thus, I'd like to remove/edit that line in a previous commit and have it carry over to newer commits.

How would I do this?

1 Answers

If you haven't done anything else, you can make use of git commit --amend

First modify whatever things you want to change. Add those files git add . or the specific file.

Then git commit --amend which would cause to change your previous commit status, therefore git log would be different. You can even write another commit message right there.

If you want to start over, then you should reset to a previous one.

git reset --hard HEAD~1 would do the trick. Careful, it would erase all your changes from your last commit.

In any case, if you already pushed to your remote repo, you can overwrite it using git push -f

Related