Move existing, uncommitted work to a new branch in Git
I started some work on a new feature and after coding for a bit, I decided this feature should be on its own branch.
How do I move the existing uncommitted changes to a new branch and reset my current one?
I want to reset my current branch while preserving existing work on the new feature.
Use the following:
git checkout -b <new-branch>
This will leave your current branch as is, create and checkout a new branch and keep all your changes. You can then make a commit with:
git add <files>
and commit to your new branch with:
git commit -m "<Brief description of this commit>"
The changes in the working directory and changes staged in index do not belong to any branch yet. This changes where those changes would end in.
You don't reset your original branch, it stays as it is. The last commit on <old-branch>
will still be the same. Therefore you checkout -b
and then commit.
Alternatively:
Save current changes to a temp stash:
$ git stash
Create a new branch based on this stash, and switch to the new branch:
$ git stash branch <new-branch> stash@{0}
Tip: use tab key to reduce typing the stash name.
If you have been making commits on your main branch while you coded, but you now want to move those commits to a different branch:
Copy your current history onto a new branch, bringing along any uncommitted changes too:
git checkout -b <new-feature-branch>
Now force the original "messy" branch to roll back: (without switching to it)
git branch -f <previous-branch> <earlier-commit-id>
For example:
git branch -f master origin/master
or if you had made 4 commits:
git branch -f master HEAD~4
Warning: It appears that git branch -f master origin/master
will reset the tracking information for that branch. So if you have configured your master
branch to push to somewhere other than origin/master
then that configuration will be lost.
An alternative is to use this reset technique. But those instructions will discard any uncommitted changes you have. If you want to keep those, stash them first and unstash them at the end.
链接地址: http://www.djcxy.com/p/556.html上一篇: 我怎样才能调和分离的HEAD与主/来源?
下一篇: 将现有的未提交的工作移至Git中的新分支