Git modify a previous commit that is already pushed with wrong changes

Viewed 97

I have the following situation with my git repository

Commit N: My latest commit (3 file changes)

Commit N-1: Some good commit (2 file changes)

Commit N-2: Some problematic commit (2 files changes, in one of them your password says hi to the world) .

.

.

.

Commit 1

So, all those commits are already pushed to the remote repository.

What I want to achieve:

Have everthing stay the same, but modify the remote commit n-2 in a way that i would modify one of the files and remove the area where the password is not revealed. Also, after the update, the password should not be visible in the commit history. But i dont wanna loose any of my current changes.

1 Answers

The modern tool to use in that case would be git filter-repo, which will replace the old git filter-branch or BFG

Example:

To replace the text 'password' with 'p455w0rd':

git filter-repo --replace-text <(echo "password==>p455w0rd") main~2..main

But you will need a git push --force in order to replace your pushed commits by the rewritten commits.
So your changes are not lost, but not " everything stays the same" exactly, considering any change of content in a commit means technically a new commit (new SHA).

Related