Skip to content

Instantly share code, notes, and snippets.

@crunchie84
Created January 21, 2021 14:11
Show Gist options
  • Save crunchie84/a070f66a3af57d918e4c345d0ec5ea9b to your computer and use it in GitHub Desktop.
Save crunchie84/a070f66a3af57d918e4c345d0ec5ea9b to your computer and use it in GitHub Desktop.
Git Surgery blogpost - rewriting histories
# part one, put on your scrubs
mkdir git-surgery
cd git-surgery
git clone [email protected]:crunchie84/blogpost-git-surgery-source.git source
cd source
# extract our application from the source repository
git subtree split -P Configuration/application-1 -b rewritten-history-application-1
# Prepare our new repository
cd ..
mkdir mavenized-solution
cd mavenized-solution
git init -b master
git pull ../source rewritten-history-application-1
# correct file paths
# we are in the /git-surgery/mavenized-solution folder
git filter-branch --force --prune-empty --tree-filter '
dir="src/main/configuration/application-1"
if [ ! -e "${dir}" ]
then
mkdir -p "${dir}"
git ls-tree --name-only $GIT_COMMIT | xargs -I files mv files "${dir}"
fi'
# clean up our mavenized-poc repository
cd .. # back to the git-surgery root folder
git clone [email protected]:crunchie84/blogpost-git-surgery-poc-target.git mavenized-poc
cd mavenized-poc
git filter-repo --path src/main/configuration/application-1 --invert-paths
#merge the cleaned-up mavenized-setup onto our source repository
# working dir = /git-surgery/mavenized-poc
git checkout -b mavenizing-application
cd ../mavenized-solution
git pull ../mavenized-poc mavenizing-application --allow-unrelated-histories
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment