Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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?

like image 487
Ramy Avatar asked Nov 10 '11 20:11

Ramy


People also ask

Can I move my changes to another branch?

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.


2 Answers

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 
like image 62
xpioneer Avatar answered Sep 24 '22 14:09

xpioneer


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.

like image 39
poke Avatar answered Sep 24 '22 14:09

poke