I'd like to move the last several commits I've committed to master to a new branch and take master back to before those commits were made. Unfortunately, my Git-fu is not strong enough yet, any help?
I.e. How can I go from this
master A - B - C - D - E
to this?
newbranch C - D - E
/
master A - B
In General...
The method exposed by sykora is the best option in this case. But sometimes is not the easiest and it's not a general method. For a general method use git cherry-pick:
To achieve what OP wants, its a 2-step process:
Step 1 - Note which commits from master you want on a
newbranch
Execute
Note the hashes of (say 3) commits you want on
newbranch
. Here I shall use:C commit:
9aa1233
D commit:
453ac3d
E commit:
612ecb3
Step 2 - Put them on the
newbranch
OR (on Git 1.7.2+, use ranges)
git cherry-pick applies those three commits to newbranch.
Yet another way to do this, using just 2 commands. Also keeps your current working tree intact.
Old version - before I learned about
git branch -f
Being able to
push
to.
is a nice trick to know.You can do this is just 3 simple step that i used.
1) make new branch where you want to commit you recent update.
git branch <branch name>
2) Find Recent Commit Id for commit on new branch.
git log
3) Copy that commit id note that Most Recent commit list take place on top. so you can find your commit. you also find this via message.
git cherry-pick d34bcef232f6c...
you can also provide some rang of commit id.
git cherry-pick d34bcef...86d2aec
Now your job done. If you picked correct id and correct branch then you will success. So before do this be careful. else another problem can occur.
Now you can push your code
git push
Had just this situation:
I performed:
I expected that commit I would be the HEAD, but commit L is it now...
To be sure to land on the right spot in the history its easier to work with the hash of the commit
Moving to a new branch
WARNING: This method works because you are creating a new branch with the first command:
git branch newbranch
. If you want to move commits to an existing branch you need to merge your changes into the existing branch before executinggit reset --hard HEAD~3
(see Moving to an existing branch below). If you don't merge your changes first, they will be lost.Unless there are other circumstances involved, this can be easily done by branching and rolling back.
But do make sure how many commits to go back. Alternatively, you can instead of
HEAD~3
, simply provide the hash of the commit (or the reference like origin/master) you want to "revert back to" on the master (/current) branch, e.g:*1 You will only be "losing" commits from the master branch, but don't worry, you'll have those commits in newbranch!
WARNING: With Git version 2.0 and later, if you later
git rebase
the new branch upon the original (master
) branch, you may need an explicit--no-fork-point
option during the rebase to avoid losing the carried-over commits. Havingbranch.autosetuprebase always
set makes this more likely. See John Mellor's answer for details.Moving to an existing branch
If you want to move your commits to an existing branch, it will look like this:
This doesn't "move" them in the technical sense but it has the same effect: