切换分支并忽略任何更改而不提交
我当时正在 git 分支上工作,并准备好提交我的更改,因此我使用有用的提交消息进行了提交。然后我心不在焉地对不值得保留的代码进行了微小的更改。我现在想更改分支,但是 git 给了我,
错误:您对“X”进行了本地更改;无法切换分支。
我可以在不提交的情况下更改分支吗?如果是这样,我该如何设置?如果不是,我该如何摆脱这个问题?我想忽略微小的更改而不提交,只更改分支。
I was working on a git branch and was ready to commit my changes, so I made a commit with a useful commit message. I then absentmindedly made minor changes to the code that are not worth keeping. I now want to change branches, but git gives me,
error: You have local changes to "X"; cannot switch branches.
Can I change branches without committing? If so, how can I set this up? If not, how do I get out of this problem? I want to ignore the minor changes without committing and just change branches.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
您需要一个干净的状态来更改分支。只有在不影响“脏文件”的情况下才允许分支签出(如 Charles Bailey 在评论)。
否则,您应该:
reset --hard HEAD
(如果您不介意丢失那些次要的更改)或checkout -f
(切换分支时,即使索引或工作树与 HEAD 不同,也要继续进行。这用于丢弃本地更改。)或者,最近:
git 切换
:git switch -f <分支名称>
(
-f
是--force
的缩写,它是--discard-changes
的别名)这与 git switch -m不同,后者会触发当前分支、工作树内容和新分支之间的三向合并:您不会丢失工作就这样进行中。
You need a clean state to change branches. The branch checkout will only be allowed if it does not affect the 'dirty files' (as Charles Bailey remarks in the comments).
Otherwise, you should either:
reset --hard HEAD
(if you do not mind losing those minor changes) orcheckout -f
(When switching branches, proceed even if the index or the working tree differs from HEAD. This is used to throw away local changes. )Or, more recently:
git switch
:git switch -f <branch-name>
(
-f
is short for--force
, which is an alias for--discard-changes
)This differs from
git switch -m <branch-name>
, which triggers a three-way merge between the current branch, your working tree contents, and the new branch: you won't lose your work in progress that way.如果您想放弃更改,
如果您想保留更改,
If you want to discard the changes,
If you want to keep the changes,
嗯,应该是
well, it should be
跟随,
Follow,
请注意,如果您合并了远程分支或有本地提交,并且想要返回到远程 HEAD,则必须执行以下操作:
HEAD
单独只会引用本地提交/合并 - 我有几次忘记了,当我完全打算取消所有更改/提交并返回到远程分支时,重置并最终得到“您的存储库是 X 提交..”。Note that if you've merged remote branches or have local commits and want to go back to the remote HEAD you must do:
HEAD
alone will only refer to the local commit/merge -- several times I have forgotten that when resetting and end up with "your repository is X commits ahead.." when I fully intended to nuke ALL changes/commits and return to the remote branch.这些答案都没有帮助我,因为即使在重置和隐藏之后我仍然有未跟踪的文件。我必须做:
None of these answers helped me because I still had untracked files even after reset and stash. I had to do:
git checkout -f your_branch_name
如果您在恢复更改时遇到问题:
如果您想删除未跟踪的目录和文件:
git checkout -f your_branch_name
if you have troubles reverting changes:
if you want to remove untracked directories and files:
如果你对文件进行了更改,Git 在切换分支时也需要更改,它不会让你这样做。要放弃工作更改,请使用:
然后,您将能够切换分支。
If you have made changes to files that Git also needs to change when switching branches, it won't let you. To discard working changes, use:
Then, you will be able to switch branches.
请按照以下步骤操作:
Follow these steps:
简单答案:
强制签出分支
强制签出分支是告诉 git 删除您在当前分支中所做的所有更改,并签出所需的更改。
或者如果您正在检查提交
答案是否,这就是 Git 的哲学,即跟踪所有更改,并且每个节点(即提交)都有及时了解您所做的最新更改,当然,除非您进行了新的提交,
您
决定保留更改吗?
然后使用 来存储它们
,然后将您的更改取消存储到所需的分支中 。 ,使用
它会应用您的更改,但也将它们保留在存储队列中。如果您不想将它们保留在存储堆栈中,则使用
这相当于
apply
然后弹出它们。 >下降
Easy Answer:
is to force checkout a branch
Force checking out a branch is telling git to drop all changes you've made in the current branch, and checkout out the desired one.
or in case you're checking out a commit
The answer to that is No, that's literally the philosophy of Git that you keep track of all changes, and that each node (i.e. commit) has to be up-to-date with the latest changes you've made, unless you've made a new commit of course.
You decided to keep changes?
Then stash them using
and then to unstash your changes in the desired branch, use
which will apply you changes but keep them in the stash queue too. If you don't want to keep them in the stash stack, then pop them using
That's the equivalent of
apply
and thendrop
切换到丢失更改的新分支:
切换到丢失更改的现有分支:
switching to a new branch losing changes:
switching to an existing branch losing changes:
如果您想保留更改并在单行命令中更改分支
If you want to keep the changes and change the branch in a single line command
为了你的心理平静
(为了更容易地访问您在分支切换时未提交的更改)
切换之前:
使用
而不是默认值:
如果您的 git stash 列表 是一个较长的列表,那么这是值得的
并且必须切换回以前从某个地方开始的想法。
For your mental calmness
(to have much easier access to changes you have left uncomitted doing a branch switch)
Before switching:
use
instead of the default:
This pays off if your
git stash list
is a longer listand must switch back to some previous idea started somewhere there.
将未提交的更改移至新分支
我为此创建了一个
.gitconfig
别名:要更改为
new-branch-name
,请使用:以及任何未提交的文件和索引更改将被保留。
Move uncommited changes to a new branch
I created a
.gitconfig
alias for this:To change to
new-branch-name
, use:And any non-commited file and index changes will be kept.
当 git stash 不起作用时切换到其他分支而不提交更改。您可以使用以下命令:
git checkout -f 分支名称
To switch to other branch without committing the changes when git stash doesn't work. You can use the below command:
git checkout -f branch-name
关闭终端,删除项目所在的文件夹,然后再次克隆您的项目,瞧。
Close terminal, delete the folder where your project is, then clone again your project and voilá.
git stash 命令用于保存尚未提交到临时区域的更改,以便您可以切换到不同的分支,进行更改,然后返回到原始更改。
// 做你的工作:改变、提交或者你想要的
git stash command is used to save changes that have not been committed to a temporary area so that you can switch to a different branch, make changes, and then come back to your original changes.
// Do your work: change, commit or what your are want