Removing data from git history
How to edit the commit history without breaking everything (hopefully)
git rebase
The version control tool git is useful in many situations. This is partly because every seemingly insignificant intermediate state of a project is recorded as a commit
in the git history and can be retrieved at any time using the git checkout
command. However, it becomes tedious when files or individual lines have crept into the project that need to be removed completely later. For example, when a project is to be released, and some commits contain data that is not intended for the public. Simply deleting them in the HEAD
of the current branch and committing the change is not sufficient because the unwanted data still exists in all previous commits created after their addition.
In such cases, the commit history needs to be manipulated. While this is easily possible, it also carries the risk of permanently damaging the project.
git rebase -i <hash>~1
Initiates the rebase. Replace <hash>
with the hash of the commit where the issue occurred, for example, where the confidential data was added. The ~1
refers to the commit before the mentioned one.
edit
instead of pick
pick
to edit
for the problematic commit, allowing adjustments to be made to this commit's changes.git add .
git commit --amend
git rebase --continue
continues the rebase, for example, to the next commit where edit
was chosen. Any conflicts arising from the made changes can be resolved like other merge conflicts and accepted using steps 3, 4, and 5. If any issues arise at any point, the entire rebase can be aborted with git rebase --abort
.Feel free to leave your opinion or questions in the comment section below.