在中止 git 中的当前更改后切换分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7463392/
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
Switching a branch after aborting current changes in git
提问by highBandWidth
I cloned a git repo and then started playing around in its master branch. After a while, I want to ignore the changes I just made (without committing them), and switch to a different branch. However, it stops me from switching because there are uncommitted changes. How do I ignore them without stashing them either? This is what happens:
我克隆了一个 git repo,然后开始在它的 master 分支上玩。过了一会儿,我想忽略我刚刚所做的更改(不提交它们),并切换到不同的分支。但是,它阻止我切换,因为有未提交的更改。我如何在不隐藏它们的情况下忽略它们?这是发生的事情:
$ git checkout gh-pages
error: Your local changes to the following files would be overwritten by checkout:
somefile.txt
Please, commit your changes or stash them before you can switch branches.
Aborting
回答by sehe
Option 1
选项1
git checkout -f gh-pages
Option 2
选项 2
git reset --hard # beware: don't make that a habit
git checkout gh-pages
回答by Wtower
Just for the sake of completeness, and for those who landed here by searching: Although the OP asks specifically for a solution without stashing, it is worth mentioning that stash is indeed a very nice option:
只是为了完整起见,并为那些谁降落在这里通过搜索:虽然OP的解决方案明确要求没有藏起来,值得一提的是,藏匿确实是一个非常不错的选择:
The command saves your local modifications away and reverts the working directory to match the HEAD commit.
该命令保存您的本地修改并恢复工作目录以匹配 HEAD 提交。
So you can simply
所以你可以简单地
git stash
This is like resetting to HEAD. When being absolutely positively certain that indeed those uncommitted changes are worthless, simply
这就像重置为 HEAD。当绝对肯定那些未提交的更改确实毫无价值时,只需
git stash drop
You can even have multiple stashes etc. as mentioned in the documentation link above.
如上面的文档链接中所述,您甚至可以拥有多个 stash 等。
I would recommend this practice since it puts one in the habit to double-think before resetting without a significant cost.
我会推荐这种做法,因为它使人们养成在重置之前三思而后行的习惯,而无需付出大量代价。
回答by Kit Ho
You can ignore all uncommitted changes.
您可以忽略所有未提交的更改。
git reset --hard HEAD
git reset --hard HEAD
回答by Mark Longair
If you're reallysure that you want to throw away your uncommitted changes (i.e. those that are staged as well as those in your working tree) you can do:
如果您真的确定要丢弃未提交的更改(即那些已暂存的更改以及工作树中的更改),您可以执行以下操作:
git reset --hard
In general, stashing is often safer
一般来说,藏匿通常更安全
回答by maoyang
If you have unstaged file, try:
如果您有未暂存的文件,请尝试:
git checkout -- .
Or
或者
git checkout -- filename
回答by Yoweli Kachala
git add -A git stash
git add -A git stash
this will clear all the changes you made (Only those that are not commited)
这将清除您所做的所有更改(仅那些未提交的更改)
回答by medik
You can discard changes you made in a specific file:
您可以放弃在特定文件中所做的更改:
git checkout somefile.txt
And then jump smoothly to the branch:
然后顺利跳转到分支:
git checkout gh-pages