Skip to content

Instantly share code, notes, and snippets.

@mbklein
Created March 29, 2011 18:25
Show Gist options
  • Save mbklein/892930 to your computer and use it in GitHub Desktop.
Save mbklein/892930 to your computer and use it in GitHub Desktop.
#!/bin/bash
framework=$1
project=$2
remote=$3
if [[ ! ("$#" -eq 3)]]
then
echo >&2 "Usage: $0 <framework-repo> <project-name> <project-repo>"
exit 1
fi
echo "Cloning framework: $1"
echo " into project $2"
echo " with new origin repo $3"
git clone $framework $project
cd $project
git submodule init && \
git submodule update && \
git remote rename origin source && \
git branch --track framework-source source/master && \
git remote add origin $remote && \
git push origin master && \
git checkout framework-source && \
git branch --force --set-upstream master origin/master && \
git checkout master
@mbklein
Copy link
Author

mbklein commented Mar 29, 2011

The above script will:

  1. Clone <framework-repo> into a <project-name> working directory
  2. Create a framework-source branch that tracks the master branch at <framework-repo>
  3. Point origin at <project-repo>
  4. Push to origin/master
  5. Set the master branch to track the master branch at <project-repo> master branch

From that point forward, you do all your work in branches, merge into master, push to origin, etc. just like always. Never commit or merge to framework-source. When you want to pull upstream changes into master:

  1. git checkout master
  2. git branch upstream-rebase
  3. git checkout framework-source
  4. git pull
  5. git checkout upstream-rebase
  6. git rebase framework-source
  7. [fix merge conflicts and commit until branch is clean]
  8. [check to make sure tests run and everything works as expected]
  9. git checkout master
  10. git merge upstream-rebase [which should be a fast-forward]
  11. [make sure master passes the same tests as in step 8]
  12. git branch -D upstream-rebase

It's not as painful as it looks, except for the conflict resolution, which is always a pain. Steps 1-6 could be pretty easily encapsulated in a shell script, but the rest is too potentially destructive to trust to an automated process.

It's been pointed out that this looks a lot like subtree merging, and it does. But the subtree strategy assumes you want to include the contents of an independent project in a subdirectory of your repo, and doesn't really help when you want to fork and pull from/to the root.

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