Skip to content

Instantly share code, notes, and snippets.

@quickshiftin
Last active August 29, 2015 13:56
Show Gist options
  • Save quickshiftin/8922723 to your computer and use it in GitHub Desktop.
Save quickshiftin/8922723 to your computer and use it in GitHub Desktop.
Simple git find-&-replace utility (for use w/ GNU sed)
#--------------------------------------------------------
# Simple search and replace utility for git repositories.
# @note Use w/ GNU sed.
# (c) Nathan Nobbe 2014
# quickshiftin@gmail.com
# http://quickshiftin.com
# $1 - Search
# $2 - Replace
# $3 - Subdirectory (optional, defaults to cwd)
#--------------------------------------------------------
function git_find_replace
{
local grepexp="$1"
local replace="$2"
local dir='.'
if [ ! -z "$3" ]; then
dir="$3"
fi
# Build sed command
local sedcmd="s/$grepexp/$replace/g"
# First find the files; then pipe through xargs sed for replacement
echo git grep --name-only "$grepexp" "$dir" '| xargs sed -i -r' "$sedcmd"
git grep --name-only "$grepexp" "$dir" | xargs sed -r "$sedcmd" -i
}
@quickshiftin
Copy link
Author

To use this w/ BSD sed, simply change the -i component to -i ''. You must explicitly supply an empty string to the -i flag under BSD sed.

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