Skip to content

Instantly share code, notes, and snippets.

@Alesh17
Last active March 7, 2025 13:57
Show Gist options
  • Select an option

  • Save Alesh17/ad73a8c7600139fc0804a0853b2f16c5 to your computer and use it in GitHub Desktop.

Select an option

Save Alesh17/ad73a8c7600139fc0804a0853b2f16c5 to your computer and use it in GitHub Desktop.
Change a string in whole git history
  1. Setup git-filter-repo from https://github.com/newren/git-filter-repo
  2. Setup replace file with strings to replace and place it to your project root package: replaces.txt
password_1==>0001
password_2==>0002
password_3==>0003
  1. Replce your strings locally by do this command in your project path: git filter-repo --replace-text replaces.txt --force
  2. After execution the script the IDE sometimes deletes the Git Remotes, so set it again (Git -> Manage Remotes -> Add)
  3. Create a script clear.sh and give it execution rights (in terminal): chmod +x clear.sh
#!/bin/bash

# 1. Get all branches
branches=$(git branch)

# 2. Get all tags
tags=$(git tag)

# 3. Force push all branches
for branch in $branches; do
  # Remove "origin/" at the start of branch name
  branch_name=$(echo $branch | sed 's/^origin\///')
  # Push branch
  git push origin refs/heads/$branch_name --force
done

# 4. Force push all tags
for tag in $tags; do
  # Push tag
  git push origin tag $tag --force
done

CAREFUL! IN THIS STEP YOU WILL FORCE PUSH ALL GIT HISTORY TO REMOTE REPOSITORY. BE SURE YOU KNOW WHAT IT IS AND HAVE ALERTED YOUR COLLEAGUES WHO ALSO WORK IN THIS REPOSITORY!

  1. Execute script in your project path: sh clear.sh
  2. Tell your colleagues to clone "new" project
  3. Done!

You can check that all strings was removed from git history by command: git grep "string" $(git rev-list --all)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment