Yes, this is possible, and not hard. The short version: git branch aBranch
to create the new branch, git stash
to save any uncommitted changes, and git reset --hard 1
to reset master
to the commit you would like, and optionally git stash pop
to apply those uncommitted changes to your current working copy, if they were intended for the new master
branch.
Here's a full explanation, with diagrams so you can follow along. In the following, I will notate the current branch (HEAD) with a *
.
0--1 *master
0--1--2 *master
0--1--2--3 *master
Create new branch aBranch
pointing at the current commit:
$ git branch aBranch
0--1--2--3 *master aBranch
If you have any changes in the working copy that haven't yet been committed, save them with a stash (this is important, as git reset --hard
will wipe away any uncommitted changes you have):
$ git stash
0--1--2--3 *master aBranch
\- 4 stash
Now reset master
back to the revision you want it at. I'm using 1
below as a placeholder; you may want to use the SHA-1, or something like origin/master
if you want to reset it to the revision that the upstream master
branch was at before you made your commits:
$ git reset --hard 1
0--1--2--3 aBranch
\ \- 4 stash
\- *master
If you had uncommitted changes in your working copy that you stashed away, and you want to apply them to master
, you can use git stash apply
or git stash pop
(apply
will leave the stash recorded as a commit; pop
will clean up the stash).
$ git stash pop
0--1--2--3 aBranch
\- *master (with stashed changes now back in your working copy)
Now you're on master
, and reset to commit 1, exactly as you wanted to be. New commits will go to the master
branch.
0--1--2--3 aBranch
\-4 *master
0--1--2--3 aBranch
\-4--5 *master
Note that if your working copy is clean (nothing reported by git status
), you don't need the git stash
command. If you wanted to apply your changes to aBranch
instead of master
, you would just use git stash pop
the next time you switched back to aBranch
and started working on it again.
One important point to keep in mind is that you should only edit branch history like this in your own private repositories, not public ones that other people could pull from. If you push this to a public repository that people are pulling from, then they'll have to rebase all of their changes, and it's best not to make them do that unless absolutely necessary.