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

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - 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

Solution 2 - Git

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.

Solution 3 - Git

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

Solution 4 - Git

I resolved this problem by the following approach

Step 1: Create a new branch from the infected master branch and named it something like that master_infected ;

Step 2: now hard reset the infected master branch for removing the polluted commits by

 git reset --hard HEAD~2  
    

(going back two commits before HEAD because for my case I have that two polluted commits. so for your case it may be different )

Now my master_infected branch is containing whose code which I want to preserve (As I said my polluted code) and the master branch is now in a save mode.

Solution 5 - Git

Create a new branch from where you should have, and then cherry pick the changes on the incorrect branch into the new branch.

You can then delete the bad branch, assuming you haven't pushed it elsewhere and other changes have been made against it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRamyView Question on Stackoverflow
Solution 1 - GitxpioneerView Answer on Stackoverflow
Solution 2 - GitpokeView Answer on Stackoverflow
Solution 3 - GitMark FisherView Answer on Stackoverflow
Solution 4 - GitGk Mohammad EmonView Answer on Stackoverflow
Solution 5 - GitWillDView Answer on Stackoverflow