Recently I had a coworker persist sensitive information in some files managed by version control in a branch that had not made its way into master yet. He was out for the day so I was tasked with modifying the Git history so that the sensitive information would not be visible when doing a $ git log.
What I had to do was the following:
- Open up Git BASH
- Change the active directory to the repo
- Run the following command:
Code:
$ git log --oneline
- Find the commit hash of the commit where the sensitive information was added to the Git history.
- Copy the commit hash of the commit just prior.
- Rebase the branch by calling:
Code:
$ git rebase -i {hash}
- From the interactive mode (using VIM):
- Press i to enter edit mode
- Replace Pick on the commit I needed to change with Edit
- Press esc to exit edit mode
- Press Shift + Z + Z to save the changes
- With the branch is in rebase mode, I edited the file with the sensitive information and removed it
- Staged the changes by running the following command:
- Amended the commit with the same commit message by running the following command:
Code:
$ git commit --amend -C HEAD
- Finished the rebase by running the following command:
Code:
$ git rebase --continue
I was nervous about doing this on my actual branch, so for a test run I cloned a backup:
Code:
$ git clone {project name} ./{backup name}