How to move the changes from one branch to another branch git?

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 have commit (say) 2 times after you realised you should have been in a branch then simply do

git branch work_branch
git reset --hard HEAD~2

replace the 2 with the number of commits back you want to go. You'll still be on master at this point, if you want to move to the branch to continue work, just git checkout work_branch

see git rev-parse --help if you want to understand the syntax for how to traverse back up your commit tree with references like HEAD~2

Tags:

Git