How can I move my work and changes from the master
branch to a newly created branch and leave the master branch intact after the move?
To move these changes to an exiting branch, just checkout the other branch with the regular checkout command. For example, run this command where “existingbranch” is the name of an existing branch.
If the changes are not commited.
you can stash the changes in the master branch .
git stash
then checkout the branch
git checkout -b newbranchname
and pop the changes here
git stash pop
If the changes are commited :
then create a branch :
git checkout -b newbranch
checkout back to master branch:
git checkout master
reset to previous commit :
git reset --hard head^1
You can create a new branch pointing to the current commit using git branch branchname
(or git checkout -b branchname
if you want to check it out directly). This will basically duplicate your master branch, so you can continue working on there.
If you have successfully copied the branch, you can reset master
to its original point by using git reset --hard commit
where commit
is the hash of the commit that should be the last one on master.
So for example you have a situation like this:
---- 1 ---- 2 ---- 3 ---- 4 ---- 5 ---- 6 ^ ^ original master master commit
So you have checked out master
on commit 6
, and you want to create a new branch ticket
pointing to that 6
while resetting master
to 3
:
git branch ticket git reset --hard 3 git checkout ticket
And then you’re on ticket
pointing to commit 6
, while master
points to 3
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With