Skip to content

Instantly share code, notes, and snippets.

@samdeane

samdeane/fixgit.sh

Created Aug 19, 2012
Embed
What would you like to do?
Script to move a git repo containing absolute gitdir and worktree submodule paths
#!/usr/bin/env bash
# Fix up a git repo containing submodules that have absolute paths which have changed.
#
# Note that this doesn't do the *proper* fix, which is to turn the absolute paths into
# relative paths. This is possible, but would require a lot more logic because the relative
# paths would be different in each case.
#
# What it does instead is a pragmatic workaround, which is to search & replace the old
# path with a new (and presumably correct) one. This will get you up and running again.
#
# This script operates on the current directory and all directories below it, so be
# careful where you run it from.
#
# Set oldpath and newpath to the old and new paths respectively.
#
# Sam Deane, August 2012.
# WARNING:
# This script may hose your repo if I've got something wrong.
# It's worked fine for me, but use with caution, and backup first.
# And don't blame me if it all goes pear-shaped!
if ! [[ -e .git ]]; then
echo "Run this script from the root of your git repo."
exit 1
fi
# *** REPLACE THESE WITH YOUR OLD AND NEW PATHS ***
oldpath=/Volumes/data/Work
newpath=/Volumes/titan/Users/sam/Work
cmd="sed -i.gitbak s|$oldpath|$newpath|g"
find . -name .git -type f -exec $cmd {} \;
find . -name config -type f -exec $cmd {} \;
find . -name .git.gitbak -type f -exec rm {} \;
find . -name config.gitbak -type f -exec rm {} \;
# TODO: find a way to prevent sed from making a backup file, so we don't have to delete them
@djalmaaraujo

This comment has been minimized.

Copy link

@djalmaaraujo djalmaaraujo commented Aug 20, 2012

This if on .git folder doesn't work on mac os mountain lion.
Thanks for your script!

@joehoyle

This comment has been minimized.

Copy link

@joehoyle joehoyle commented Sep 1, 2012

I believe it should be if ! [[ -d .git ]]; then (it should be checking for a .git directory, not file

@samdeane

This comment has been minimized.

Copy link
Owner Author

@samdeane samdeane commented Sep 3, 2012

Possibly should be -e actually, the ,git can be a file if it's a submodule, or a directory if it's the root module.

@goonzoid

This comment has been minimized.

Copy link

@goonzoid goonzoid commented Sep 11, 2012

re: the TODO at the bottom, you can do this on OS X using sed -i "" instead of sed -i.gitbak. Note the space between the -i and the ""

@samdeane

This comment has been minimized.

Copy link
Owner Author

@samdeane samdeane commented Nov 6, 2012

Thanks for the comments folks - have updated the script accordingly.

@samdeane

This comment has been minimized.

Copy link
Owner Author

@samdeane samdeane commented Nov 6, 2012

Actually, I spoke too soon. Can't get the sed -i "" to work as a command embedded in the find. Keep ending up with files called .git"" or whatever...

@mcpark3141

This comment has been minimized.

Copy link

@mcpark3141 mcpark3141 commented Aug 16, 2016

Very useful script

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