我开始研究一个新特性,在编码了一段时间后,我决定这个特性应该属于自己的分支。

如何将现有未提交的更改移动到新分支并重置当前分支?

我想重置当前分支,同时保留新功能的现有工作。


当前回答

我使用@Robin回答并列出我所做的一切,

git status                               <-- review/list uncommitted changes
git stash                                <-- stash uncommitted changes
git stash branch <new-branch> stash@{1}  <-- create a branch from stash
git add .                                <-- add local changes
git status                               <-- review the status; ready to commit
git commit -m "local changes ..."        <-- commit the changes
git branch --list                        <-- see list of branches incl the one created above
git status                               <-- nothing to commit, working tree (new-branch) is clean
git checkout <old-branch>                <-- switch back

! 如果回购有多个存储,请查看要应用于新分支机构的存储:

git stash list  
  stash@{0}: WIP on ...  
  stash@{1}: WIP on ...

并通过以下方式检查各个藏匿点,

git stash show stash@{1}

或者立即检查所有储藏物:

git stash list -p

其他回答

如果你提交了它,你也可以选择一个提交ID。当我开始在master中工作时,我经常这样做,然后想在我推到origin/之前创建一个本地分支。

git cherry-pick <commitID>

正如这里所描述的,你可以用樱桃树做很多事情,但这可能是你的一个用例。

我使用@Robin回答并列出我所做的一切,

git status                               <-- review/list uncommitted changes
git stash                                <-- stash uncommitted changes
git stash branch <new-branch> stash@{1}  <-- create a branch from stash
git add .                                <-- add local changes
git status                               <-- review the status; ready to commit
git commit -m "local changes ..."        <-- commit the changes
git branch --list                        <-- see list of branches incl the one created above
git status                               <-- nothing to commit, working tree (new-branch) is clean
git checkout <old-branch>                <-- switch back

! 如果回购有多个存储,请查看要应用于新分支机构的存储:

git stash list  
  stash@{0}: WIP on ...  
  stash@{1}: WIP on ...

并通过以下方式检查各个藏匿点,

git stash show stash@{1}

或者立即检查所有储藏物:

git stash list -p

或者:

将当前更改保存到临时存储:$git存储基于此存储创建新分支,然后切换到新分支:$git stash branch<new branch>stash@{0}

提示:使用tab键可以减少键入存储名称。

选项1(现有分支)

git stash (from main/any-branch)
git checkout your-existing-branch
git stash apply 

选项2(创建新分支)

git switch -c your-new-branch

这是告诉您使用gitstash-k的唯一答案,您需要。。。

如果您已经花了一个小时使用gitadd-p

然后决定在执行实际提交之前测试添加到索引中的内容。在这种情况下,不要使用纯数字存储!

而是:

git stash -k

这将保留索引并删除仍在工作目录中且尚未添加到索引中的其余部分。正是你想要的。

现在您可以尝试编译/测试和提交。即。

make
git commit -m 'Yay!'

然后使用

git stash pop

如果您发现它不编译,那么进行更改并添加索引和提交可能会混淆git stash pop。当涉及到合并时,它不是那么好。在这种情况下,你可能无论如何都应该提交;因此:

make
git commit -m 'Grrrr'

然后创建一个新分支,

git switch -c tmpbranch

在那里做你的工作(更改代码、进行测试和更多提交)

/* blood sweat and tears */

一旦一切正常,将其提交给新分支

commit -a -m 'Finally!'

返回到旧分支,然后使用与推到存储位置时相同的工作目录执行git存储弹出。

git checkout youknowwhatbranchyouwereonright
git stash pop

也要提交,否则无法合并tmpbranch。然后合并您创建的临时分支。

git commit -a -m 'Still working on this.'
git merge tmpbranch
/* fix collisions and commit */

现在,您可以重新设置基础,将“仍在处理这个”放在顶部,并将其余部分压缩/修复为单个注释。例如

git rebase -i

可能会给你:

pick 540623a Grrr
pick a8589d3 Still working on this.
pick d3b602c Finally

然后将其更改为:

reword 540623a Grrr
fixup d3b602c Finally
pick a8589d3 Still working on this.

最后撤销最后一次提交(“仍在处理”)

git reset HEAD~1