git 在本地分支上,不想提交更改,但需要切换到另一个分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18073106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 08:52:31  来源:igfitidea点击:

On local branch, don't want to commit changes, but need to switch to another branch

gitgit-commitgit-checkoutgit-stash

提问by Suleman Ahmad

I am working on branch Aand the feature/task is not done. Then I need to switch to another branch Bfor a quick fix. When I try to switch on another branch Git forces me to save my local changes otherwise I 'll lose my all local changes.

我在分支A上工作,但功能/任务没有完成。然后我需要切换到另一个分支B进行快速修复。当我尝试打开另一个分支时,Git 会强制我保存本地更改,否则我将丢失所有本地更改。

I need to commit my incomplete code. Is there any way that I can switch between multiple branches without committing and losing any code? Or is there a better way to handle the situation?

我需要提交我不完整的代码。有什么方法可以在多个分支之间切换而不提交和丢失任何代码?或者有没有更好的方法来处理这种情况?

回答by mipadi

You can use git stash, which will save your changes without creating a commit.1

您可以使用git stash,这将保存您的更改而不创建提交。1

First, stash your changes:

首先,隐藏您的更改:

$ git stash

Then switch to your other branch:

然后切换到你的另一个分支:

$ git checkout branch-B

When you're reading, go back to your original branch and unstash your changes:

阅读时,返回到原始分支并取消隐藏更改:

$ git checkout branch-A
$ git stash pop

See the documentation linked above for more details and specifics on additional use cases.

有关其他用例的更多详细信息和细节,请参阅上面链接的文档。



1Technically it does create a commit, but git stashuses some magic so you don't actually seethe commit, and Git's tools know how to deal properly with these pseudo-commits.

1从技术上讲,它确实创建了一个提交,但git stash使用了一些魔法,因此您实际上看不到提交,而且 Git 的工具知道如何正确处理这些伪提交。

回答by mipadi

One option, as mipadi demonstrates, is to simply use git stash.

正如mipadi 所展示的,一种选择是简单地使用git stash.

Another option is to simply just commit your current work in progress, switch branches, and then when you're ready to switch back, do a mixed reset back to your previous commit:

另一种选择是简单地提交您当前正在进行的工作,切换分支,然后当您准备好切换回来时,将混合重置回您之前的提交:

# While working on "feature" branch,
# you suddenly need to go work on a hotfix:
$ git commit --all --message "Backup my feature work"
$ git checkout -b hotfix master

# You did your hotfix, and are ready to go back to feature
$ git checkout feature
$ git reset head^

git reset head^will do a mixed reset back to the commit before you did a backup commit, and all of the changes you made in your backup commit will be restored to your working copy. From the official Linux Kernel documentation for git reset(emphasis mine):

git reset head^将在您进行备份提交之前混合重置回提交,并且您在备份提交中所做的所有更改都将恢复到您的工作副本。来自官方 Linux 内核文档git reset(重点是我的):

Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action.

重置索引而不是工作树(即,更改的文件被保留但未标记为提交)并报告尚未更新的内容。这是默认操作。

回答by Erlisar Vasquez

I know this question is old, I just want to share one technique that could help you handle this situation. You can actually commit it to the branch and undo it later on.

我知道这个问题很老,我只想分享一种可以帮助您处理这种情况的技巧。您实际上可以将其提交到分支并稍后撤消。

1. Commit to the branch. (Commit only. Do not push.)

1. 提交到分支。(仅提交。不要推动。)

$ git commit --all --message "Commit Message here."

2. If you want to continue working on the branch, just checkout and undo your last commit without losing your latest changes.

2. 如果您想继续在该分支上工作,只需检出并撤消您的最后一次提交,而不会丢失您的最新更改。

$ git reset --soft HEAD~1

Note the --soft flag: this makes sure that the changes in undone revisions are preserved. After running the command, you'll find the changes as uncommitted local modifications in your working copy.

请注意 --soft 标志:这确保保留未完成修订中的更改。运行该命令后,您会在工作副本中发现更改为未提交的本地修改。

You can read more here.

您可以在此处阅读更多内容。