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?
6 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
PS: This only works if no new files have been created and just the existing files are modified
-
13this should be the accepted answer as OP does not specify that work has been committed Jan 24, 2019 at 13:49
-
1This works great, but I'm not sure what git stash and git stash pop are or what they're for. I'm a little perplexed. May 20, 2021 at 2:38
-
5@EdisonPebojot git stash is for saving the changes done after the last commit and git stash pop is to bring back those changes to the workspace.– xpioneerAug 26, 2021 at 4:01
-
This worked perfectly for me, I had no commits. The goof I make most often with git is forgetting to start a new branch. Apr 15 at 11:08
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
.
-
3That will work if your new branch is supposed to come out of
master
. If you need it on a different line of development, you'll have to look into git-rebase– cheNov 10, 2011 at 20:34 -
2The
-b
ingit checkout
tells Git to create the branch before checking it out. It’s basically a shortcut togit branch name
andgit checkout name
; you usually use it when you want to start working on a new branch for something.– pokeNov 10, 2011 at 20:41 -
it looks like creating the new branch did not bring over the changes I had originaly made to master. not a big deal in my situation but it'd be good to know for the future....– RamyNov 10, 2011 at 20:42
-
What do you mean? If you create a branch, it should point to HEAD (if you don’t specify a different commit). So if you are on master (and have everything committed), then creating a new branch should get you to the same commit.– pokeNov 10, 2011 at 20:44
-
6If you didn’t commit anything yet, you can use
git stash
to save your work for somewhere else.– pokeNov 11, 2011 at 13:55
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
-
-
1@adrianmann - I mean if you do 2 separate bits of work, adding changes and committing each of them, so your history looks like Start -> A -> B, where A and B are the 2 commits. Feb 10, 2015 at 16:42
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.
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.
git add --all
git commit -m <commit-message>
git branch -m master <feature-branch-name>
git push --set-upstream origin <feature-branch-name>