3

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?

  • you might rebase ; backup file level ; purge repo ; git init again / restore & at last git commit a new initial commit & push .... BUT doing so you loose all historic *and I am not sure I have not forgotten a step* – francois P Sep 05 '20 at 19:54
  • You cannot *change* the existing commits, but you can delete them after making new and improved variants that are similar but do not have the sensitive data. See [here](https://stackoverflow.com/q/872565/1256452) and [here](https://stackoverflow.com/q/57122756/1256452). – torek Sep 05 '20 at 20:04
  • @torek so I'm essentially recreating the entire git history manually without the sensitive data? Is there no way to programmatically do this within git (without removing entire files)? –  Sep 05 '20 at 20:10
  • *so I'm essentially recreating the entire git history* - yes; ... *manually* - no: that's where all the programs listed in the other two answers come in. – torek Sep 05 '20 at 20:11
  • @torek - Doesn't filter branch, as described in the other links, remove the entire file? –  Sep 05 '20 at 20:12
  • 2
    The `git filter-branch` program simply runs some arbitrary set of filters (as given on the command line) on each to-be-filtered commit, then makes a new commit based on the result of that filtering. If you use `--tree-filter`, which is the slowest one, you can make arbitrary changes to source files stored as files. If you use `--index-filter`, which is among the fast ones, you must make all your changes within Git's index. It's usually faster and easier to use The BFG (java, limited set of ops, but much easier to drive) or the new `git filter-repo` (python). – torek Sep 05 '20 at 20:14

1 Answers1

1

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

kachus22
  • 439
  • 1
  • 4
  • 10