Reputation:
Let say that I start to work on clear master
branch - no changes to commit
.
Do some local changes but realize that this modifications should be in a separate branch
instead of master
.
Is there a way how to move this changes to separate new branch
and restate master
branch into status no changes to commit
?
EDIT
Following the accepted answer for git branching - how to make current master a branch and then revert master back to previous version? ...When following the steps, my master will still have modified files.See the last comment 7.
Do I am missing something ?
$ git branch # 1. starting on master
# On branch master
nothing to commit, working directory clean
# On branch master # 2.modifying a file
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: test.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
$ git stash # 3. stashing changes
Saved working directory and index state WIP on master: 393bfad initial commit
HEAD is now at 393bfad initial commit
$ git status
# On branch master
nothing to commit, working directory clean
$ git checkout -b experiment # 4. creating new branch experiment
Switched to a new branch 'experiment'
$ git stash pop # 5. pop staged changes in exper.
# On branch experiment
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: test.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (16b6871d43f367edd03f59558eca4163cd1a2b2f)
$ git checkout master #6. going back to master
M test.txt
Switched to branch 'master'
git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: test.txt #7. in master test.txt is still modified !!!
Upvotes: 6
Views: 21449
Reputation: 2200
I haven't tested this (and I'm not a git guru that should be blindly trusted), but after reading about stashing (http://git-scm.com/book/en/Git-Tools-Stashing) it seems like the following commands may be best.
git stash
git stash branch testchanges
Upvotes: 0
Reputation: 26555
If you did not commit anything, it is no problem to go with git stash
. If you already did some commits (and you really should) git stash
won't help. In this case the easiest way is:
git branch -m feature-xy
.git pull --rebase
to integrate potential upstream changes.git push -u feature-xy:feature-xy
(Note the -u
to update you upstream branch.)git checkout master
(As there is no local master anymore, git will create a new one from upstream.)Upvotes: -1
Reputation: 212594
After you git stash pop
, you need to commit to the new branch. Just commit before checking out master
.
$ git stash pop
$ git commit -m "Committing changes to branch experiment"
$ git checkout master
Consider the following sequence. From the beginning (ie, you are in branch master with local unstaged changes in the working directory), you could just do:
$ git checkout -b experiment
$ git add test.txt
$ git commit -m 'Commit message'
$ git checkout master
The advantage of the stash/pop is that it does the 'add' for you and you don't need to specify the files that are changed. But you still need to commit on the new branch.
Upvotes: 6