qwertymodo qwertymodo - 26 days ago 18
Git Question

Rewrite entire git history

Quite awhile back, I forked a repo on github and made quite a few changes to several unrelated features, and at the time I knew basically nothing about git, so my commits are basically just "whatever I happened to have worked on since whenever the last time I remembered to commit was". I would like to contribute some of my changes upstream, but that isn't possible with my repo in its current state. Now that I've actually been using git long enough to be comfortable with it, and more aware of good practices, is it possible to rewrite my entire commit history in order to break up the small number of large commits, each containing many unrelated changes, into smaller individual commits, only containing related changes? If nothing else, I can just delete the fork on Github, re-fork, overwrite everything with my current local copies and run a bunch of partial commits, but if there's a better way that would be nice.

Answer

Yes, it's possible to rewrite the entire history... but for your case you don't have to. You just need to rewrite the bits you worked on. This is, probably, just the changes to your master branch. For that you can do an interactive rebase for all the changes between master and origin/master which is what they have upstream.

To do this, first checkout master and then git rebase -i origin/master. This will throw you into an editor where you can choose what to do with each commit. You probably want to edit them (e). Once you save the file Git will checkout each change in turn and you can do what you like with it.

If you want to split up a commit, use git add -p and git add -i to add the change a piece at a time to the staging area, then git commit to commit just that one part of the change.

For more detail, read Rewriting History in Pro Git and Github's About Git Rebase.

Comments