中止 git 中的当前更改后切换分支
我克隆了一个 git 存储库,然后开始在它的主分支上玩。一段时间后,我想忽略我刚刚所做的更改(不提交它们),并切换到不同的分支。但是,它阻止我切换,因为存在未提交的更改。我如何忽略它们而不隐藏它们?发生的情况是这样的:
$ 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
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 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
选项 1
选项 2
Option 1
Option 2
只是为了完整起见,以及对于那些通过搜索到达这里的人:尽管OP特别要求没有 stash,值得一提的是,stash 确实是一个非常不错的选择:
所以你可以简单的
这就像重置到HEAD一样。当绝对肯定那些未提交的更改确实毫无价值时,
您甚至可以拥有多个存储等,如上面的文档链接中所述。
我会推荐这种做法,因为它会让人们养成在重置之前反复思考的习惯,而无需付出巨大的代价。
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:
So you can simply
This is like resetting to HEAD. When being absolutely positively certain that indeed those uncommitted changes are worthless, simply
You can even have multiple stashes etc. as mentioned in the documentation link above.
I would recommend this practice since it puts one in the habit to double-think before resetting without a significant cost.
您可以忽略所有未提交的更改。
git重置--hard HEAD
You can ignore all uncommitted changes.
git reset --hard HEAD
如果您确实确定要丢弃未提交的更改(即暂存的更改以及工作树中的更改),您可以执行以下操作:
一般来说,隐藏通常更安全
If you're really sure 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:
In general, stashing is often safer
如果您有未暂存的文件,请尝试:
或者
If you have unstaged file, try:
Or
git添加-A
git stash
这将清除您所做的所有更改(仅清除那些未提交的更改)
git add -A
git stash
this will clear all the changes you made (Only those that are not commited)
您可以放弃在特定文件中所做的更改:
然后平滑地跳转到分支:
You can discard changes you made in a specific file:
And then jump smoothly to the branch:
对我来说也一样。 git reset --hard + git checkout 之后我仍然遇到同样的错误。
我已经通过在硬重置后立即在第二个命令上添加 --force 来修复它。
The same for me. After git reset --hard + git checkout I was still getting the same error.
I have fix it by adding --force on second command, right after hard reset.
忽略但不删除更改
OP 要求忽略更改 - 但不要求删除它们。听起来OP希望改变仍然存在。因此,这个答案适用于那些处于类似情况且不想丢失更改的人。
考虑到已经遇到的限制,再加上您想要保留这些更改,一种选择是将它们放在另一个分支中,
无论您在哪里有未提交或暂存的更改,您始终可以使用以下命令创建一个新分支:
然后根据需要使用 git add 和 git commit 。现在您可以随意切换到其他分支,知道这些更改已保存。
就像OP一样,我不喜欢使用stash和pop(和cherry pick),但是通过将它们放入一个新分支来隔离这些更改,然后我可以合并和重新设置基准,这对我来说效果很好。使用分支来解决问题对我来说很容易
To ignore but NOT remove the changes
OP asks to ignore changes - but does not ask to remove them. Sounds like OP want changes to still be around. So this answer is for those folks in a similar situation who don't want to lose their changes.
Given the limitations encountered already, plus given you want to keep those changes, one option is to put them in another branch
Regardless of where you have either uncommitted or staged changes, you can always make a new branch with
and then
git add
andgit commit
as necessary. and now you can switch to other branches at will, knowing these changes have been saved.Like the OP I don't like to use stash and pop (and cherry pick) but isolating these changes by putting them into a new branch I can then merge and rebase to and from works well for me. Using branches for the solution is easy for me