Skip to content

Instantly share code, notes, and snippets.

@ogier
Created April 29, 2012 11:55
Show Gist options
  • Save ogier/2549844 to your computer and use it in GitHub Desktop.
Save ogier/2549844 to your computer and use it in GitHub Desktop.
How to safely rebase all your Django branches.

Here is a quick primer on how to update your branches to track the new django/django git repository. The commands that follow assume that you have your own fork of django/django-old, and that you have this cloned locally, as the origin remote.

Following these steps shouldn't break anything. So long as all your changes are committed and pushed to your fork of django-old already, nothing will affect them. If you are concerned, you can run them in a fresh clone of your fork.

Rebasing

First, add all of the new commits from the new Django repository.

$ git remote add django-new https://github.com/django/django
$ git fetch django-new
warning: no common commits
remote: Counting objects: 128958, done.
remote: Compressing objects: 100% (29764/29764), done.
remote: Total 128958 (delta 92493), reused 128830 (delta 92366)
Receiving objects: 100% (128958/128958), 43.85 MiB | 3.58 MiB/s, done.
Resolving deltas: 100% (92493/92493), done.
From https://github.com/django/django
 * [new branch]      master     -> django-new/master

Now, for each feature branch you want to rebase, first checkout the branch.

$ git checkout -b feature-nnn origin/feature-nnn      # if you don't already have a local branch
$ git checkout feature-nnn                            # if you do

Then perform a rebase. If you've never done one before this can be a little daunting, because merge conflicts may arise. You may need to fix them and run git rebase --continue to move on.

The important thing is that you want to want to rebase onto the new repository's version of history, but you only want to record the changes that you made on top of the old master branch (which I am assuming is called origin/master). The correct incantation to do this is as follows:

$ git rebase --onto django-new/master origin/master

If you did this correctly the changes you made on the feature-nnn branch will be moved onto the new Django history. You can check this by running git log --pretty=oneline django-new/master.., which should output a few lines, one for each commit in your feature branch. If you see many lines, or this command returns a fatal error, something went wrong.

Repeat the above for every branch you want to migrate.

Once you are sure everything worked

Moving forwards, you probably want to push everything to github in a new fork. I recommend renaming your old fork from Github's admin panel to django-old, then fork the new django/django. You can fix up your local repository to track the new fork by running the following commands:

First, set up a remote to track the old fork (or if you feel brave, and you're sure you rebased all your feature branches, just git remote rm origin).

$ git remote set-url origin [email protected]:<your_name>/django-old    # or whatever you named your old fork
$ git remote rename origin django-old

Then set up the new origin, and point master at it.

$ git remote add origin [email protected]:<your_name>/django
$ git fetch origin       # shouldn't pull in too many commits
$ git branch -D master
$ git checkout -b master origin/master

Finally, you can push all your feature branches to the new repository.

$ git push origin feature-nnn

Anyways, hope this helps. Happy hacking.

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