我一直在做一个项目,但不幸的是,我忘记切换到我的分支,因此一直在master上工作
我怎么能复制的工作(3个文件),我已经在这里从master,到我的分支(称为,例如branch123)没有提交到master?
我一直在做一个项目,但不幸的是,我忘记切换到我的分支,因此一直在master上工作
我怎么能复制的工作(3个文件),我已经在这里从master,到我的分支(称为,例如branch123)没有提交到master?
听起来你只需要以下这些:
git stash
git checkout branch123
git stash apply
然后你应该回到你自己的分支上,而不需要触及主分支。
公认的答案是最彻底的,但有一个特殊的情况,你可以简化。如果您在工作目录中修改的文件在master和branch123中是相同的,您可以简单地这样做
git checkout branch123
不需要隐藏任何东西,因为签出的默认行为是不覆盖工作目录中修改过的文件,所以您不会丢失任何东西。(实际上这是Cascabel首先在评论中提到的)
正如其他人在评论中提到的,如果branch123还不存在,你可以这样做
git checkout -b branch123
根据我在这里的发现。
由于可以创建一个新的分支,但不可能签出一个现有的分支,同时有文件签出,我发现下面的技巧使用临时分支工作:
这种情况至少适用于VS 2015 Git插件,但很可能适用于任何Git工具。
checkout and make changes to files in master (ups!, wrong branch) create a new branch "temp" (or any unused name you choose) from master. Checked out files will now be checked out in temp and not in master. check in changes to temp (master is untouched) Everything is now checked in and it is possible to check out an existing branch. Check out the wanted branch (the branch I wanted to make the changes to begin with) 3.5 Git Rebase merge temp to the wanted branch. Now the changes are in the correct branch. delete the temp branch as it is not needed any more
编辑:我发现在执行合并之前,你必须执行临时分支的rebase (git rebase—onto)。否则,master中的更改将包含在合并中。以上额外的3.5步。更多关于rebase的信息请点击这里: https://git-scm.com/book/en/v2/Git-Branching-Rebasing