LinuxCommandLibrary

git-sed

Apply sed across Git repository revisions

TLDR

Replace the specified text in the current repository

$ git sed '[find_text]' '[replace_text]'
copy

Replace the specified text and then commit the resulting changes with a standard commit message
$ git sed -c '[find_text]' '[replace_text]'
copy

Replace the specified text, using regular expressions
$ git sed -f g '[find_text]' '[replace_text]'
copy

Replace a specific text in all files under a given directory
$ git sed '[find_text]' '[replace_text]' -- [path/to/directory]
copy

SYNOPSIS

git sed

PARAMETERS


    The `sed` expression to apply. This follows the standard `sed` syntax. Enclose expression with quotes.

DESCRIPTION

The `git-sed` command is a powerful tool that enables the application of `sed` (Stream EDitor) to all files tracked by Git across different commits. Essentially, it iterates through your repository's history, applying a specified `sed` expression to each file at each revision. This allows for complex and automated find-and-replace operations across the entire codebase history, which can be invaluable for tasks like renaming functions, updating copyright notices, or correcting pervasive typos.

Unlike directly editing files and committing, `git-sed` rewrites history, creating new commits with the changes made by `sed`. This means that the original history is effectively replaced with the modified version.

Using `git-sed` should be approached with caution because changing git history has some significant impact, especially in collaborative environments, since changes will be needed to be pushed to all collaborating repositories.

The command is implemented using `git filter-branch`, making it effective but also potentially slow on large repositories with extensive history.

CAVEATS

History Rewriting: `git-sed` rewrites the Git history. This can cause problems if others have based work on the existing history, so coordination is essential.

Performance: Can be slow on large repositories. It utilizes `git filter-branch`, which can be resource-intensive.

EXAMPLE USAGE

Replacing 'old_name' with 'new_name' in all files:
git sed 's/old_name/new_name/g'

SAFETY CONSIDERATIONS

Always back up your repository or work on a branch before using `git-sed`, as it irrevocably alters history.

SEE ALSO

sed(1), git filter-branch(1), git replace(1)

Copied to clipboard