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
1) Create a new branch, which moves all your changes to new_branch.
git checkout -b new_branch
2) Then go back to old branch.
git checkout master
3) Do git rebase
git rebase -i <short-hash-of-B-commit>
4) Then the opened editor contains last 3 commit information.
...
pick <C's hash> C
pick <D's hash> D
pick <E's hash> E
...
5) Change pick
to drop
in all those 3 commits. Then save and close the editor.
...
drop <C's hash> C
drop <D's hash> D
drop <E's hash> E
...
6) Now last 3 commits are removed from current branch (master
). Now push the branch forcefully, with +
sign before branch name.
git push origin +master
If you just need to move all your unpushed commits to a new branch, then you just need to,
create a new branch from the current one :git branch new-branch-name
push your new branch: git push origin new-branch-name
revert your old(current) branch to the last pushed/stable state: git reset --hard origin/old-branch-name
Some people also have other upstreams
rather than origin
,
they should use appropriate upstream
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
How can I go from this
A - B - C - D - E
|
master
to this?
A - B - C - D - E
| |
master newbranch
With two commands
giving
A - B - C - D - E
|
newbranch
and
giving
A - B - C - D - E
| |
master newbranch
This doesn't "move" them in the technical sense but it has the same effect:
A--B--C (branch-foo)
\ ^-- I wanted them here!
\
D--E--F--G (branch-bar)
^--^--^-- Opps wrong branch!
While on branch-bar:
$ git reset --hard D # remember the SHAs for E, F, G (or E and G for a range)
A--B--C (branch-foo)
\
\
D-(E--F--G) detached
^-- (branch-bar)
Switch to branch-foo
$ git cherry-pick E..G
A--B--C--E'--F'--G' (branch-foo)
\ E--F--G detached (This can be ignored)
\ /
D--H--I (branch-bar)
Now you won't need to worry about the detached branch because it is basically
like they are in the trash can waiting for the day it gets garbage collected.
Eventually some time in the far future it will look like:
A--B--C--E'--F'--G'--L--M--N--... (branch-foo)
\
\
D--H--I--J--K--.... (branch-bar)
Had just this situation:
Branch one: A B C D E F J L M
\ (Merge)
Branch two: G I K N
I performed:
git branch newbranch
git reset --hard HEAD~8
git checkout newbranch
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
git branch newbranch
git reset --hard #########
git checkout newbranch