中止 git 中的当前更改后切换分支

发布于 2024-12-04 23:46:18 字数 353 浏览 2 评论 0原文

我克隆了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

离鸿 2024-12-11 23:46:19

选项 1

git checkout -f gh-pages

选项 2

git reset --hard     # beware: don't make that a habit
git checkout gh-pages

Option 1

git checkout -f gh-pages

Option 2

git reset --hard     # beware: don't make that a habit
git checkout gh-pages
画中仙 2024-12-11 23:46:19

只是为了完整起见,以及对于那些通过搜索到达这里的人:尽管OP特别要求没有 stash,值得一提的是,stash 确实是一个非常不错的选择:

该命令保存您的本地修改并恢复工作目录以匹配 HEAD 提交。

所以你可以简单的

git stash

这就像重置到HEAD一样。当绝对肯定那些未提交的更改确实毫无价值时,

git stash drop

您甚至可以拥有多个存储等,如上面的文档链接中所述。

我会推荐这种做法,因为它会让人们养成在重置之前反复思考的习惯,而无需付出巨大的代价。

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:

The command saves your local modifications away and reverts the working directory to match the HEAD commit.

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

git stash drop

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.

烟织青萝梦 2024-12-11 23:46:19

您可以忽略所有未提交的更改。

git重置--hard HEAD

You can ignore all uncommitted changes.

git reset --hard HEAD

魂ガ小子 2024-12-11 23:46:19

如果您确实确定要丢弃未提交的更改(即暂存的更改以及工作树中的更改),您可以执行以下操作:

git reset --hard

一般来说,隐藏通常更安全

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:

git reset --hard

In general, stashing is often safer

你的背包 2024-12-11 23:46:19

如果您有未暂存的文件,请尝试:

git checkout -- .

或者

git checkout -- filename

If you have unstaged file, try:

git checkout -- .

Or

git checkout -- filename
◇流星雨 2024-12-11 23:46:19

git添加-A
git stash

这将清除您所做的所有更改(仅清除那些未提交的更改)

git add -A
git stash

this will clear all the changes you made (Only those that are not commited)

窗影残 2024-12-11 23:46:19

您可以放弃在特定文件中所做的更改:

git checkout somefile.txt

然后平滑地跳转到分支:

git checkout gh-pages

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
柠檬心 2024-12-11 23:46:19

块引用
选项 2 对我不起作用,它只是重复错误。

对我来说也一样。 git reset --hard + git checkout 之后我仍然遇到同样的错误。

我已经通过在硬重置后立即在第二个命令上添加 --force 来修复它。

git reset --hard
git checkout -f <branch name>

Blockquote
Option 2 did not work for me, it just repeats the error.

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.

git reset --hard
git checkout -f <branch name>
仄言 2024-12-11 23:46:19

忽略但不删除更改

OP 要求忽略更改 - 但不要求删除它们。听起来OP希望改变仍然存在。因此,这个答案适用于那些处于类似情况且不想丢失更改的人。

考虑到已经遇到的限制,再加上您想要保留这些更改,一种选择是将它们放在另一个分支中,

无论您在哪里有未提交或暂存的更改,您始终可以使用以下命令创建一个新分支:

git checkout -b new_branch

然后根据需要使用 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

git checkout -b new_branch

and then git add and git 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文