Notes git - Remove the secret

Source : https://docs.gitlab.com/ee/user/application_security/secret_detection/secret_push_protection/index.html

Remove the secret Remove a blocked secret to allow the commit to be pushed to GitLab. The method of removing the secret depends on how recently it was committed. The instructions below use the Git CLI client, but you can achieve the same result by using another Git client.

If the blocked secret was added with the most recent commit on your branch

Remove the secrets from the files.

Stage the changes with git add < file-name >.

Modify the most recent commit to include the changed files with git commit --amend.

Push your changes with git push.

If the blocked secret appears earlier in your Git history

Optional. Watch a short demo of removing secrets from your commits

Identify the commit SHA from the push error message. If there are multiple, find the earliest using git log.

Create a copy branch to work from with git switch --create copy-branch so you can reset to the original branch if the rebase encounters issues.

Use git rebase -i <commit-sha>~1 to start an interactive rebase.

Mark the offending commits for editing by changing the pick command to edit in the editor.

Remove the secrets from the files.

Stage the changes with git add < file-name >.

Commit the changed files with git commit --amend.

Continue the rebase with git rebase --continue until all secrets are removed.

Push your changes from the copy branch to your original remote branch with git push --force --set-upstream origin copy-branch:<original-branch>.

When you are satisfied with the changes, consider the following optional cleanup steps.

Optional. Delete the original branch with git branch --delete --force <original-branch>.

Optional. Replace the original branch by renaming the copy branch with git branch --move copy-branch <original-branch>.