Removing data from git history

How to edit the commit history without breaking everything (hopefully)

#Git#Other

How to 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.

Instructions

  1. 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.

  2. edit instead of pick
    Depending on the configuration, a text editor now opens, listing all commits from the problematic commit to the current one. Change pick to edit for the problematic commit, allowing adjustments to be made to this commit's changes.
  3. git add .
    Now, all desired changes can be made and added using, for example, this command:
  4. git commit --amend
  5. 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.

Comments

Feel free to leave your opinion or questions in the comment section below.