git:切换分支并忽略任何更改而不提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1304626/
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
git: Switch branch and ignore any changes without committing
提问by Daniel Farrell
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,
我在 git 分支上工作并准备提交我的更改,所以我用有用的提交消息进行了提交。然后我心不在焉地对代码做了一些不值得保留的小改动。我现在想更改分支,但是 git 给了我,
error: You have local changes to "X"; cannot switch branches.
错误:您对“X”进行了本地更改;不能切换分支。
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.
我可以在不提交的情况下更改分支吗?如果是这样,我该如何设置?如果没有,我该如何摆脱这个问题?我想在不提交的情况下忽略细微的更改,而只是更改分支。
回答by VonC
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 Baileyremarks in the comments).
您需要一个干净的状态来更改分支。只有在不影响“脏文件”的情况下才允许分支结帐(正如Charles Bailey在评论中所说)。
Otherwise, you should either:
否则,您应该:
- stashyour current change or
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. )
- 隐藏您当前的更改或
reset --hard HEAD
(如果您不介意丢失这些细微的更改)或checkout -f
(切换分支时,即使索引或工作树与HEAD不同,也继续进行。这是用来丢弃本地更改的。)
Or, more recently:
或者,最近:
- With Git 2.23(August 2019) and the new command
git switch
:git switch -f <branch-name>
(-f
is short for--force
, which is an alias for--discard-changes
)
- 在GIT 2.23(2019年8月)和新的命令
git switch
:git switch -f <branch-name>
(-f
是为短--force
,这是一个别名--discard-changes
)
Proceed even if the index or the working tree differs from HEAD.
Both the index and working tree are restored to match the switching target.
即使索引或工作树与 HEAD 不同,也要继续。
恢复索引和工作树以匹配切换目标。
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 is done: you won't loose your work in progress that way.
这与 不同git switch -m <branch-name>
,后者触发当前分支、您的工作树内容和新分支完成之间的三向合并:您不会以这种方式丢失正在进行的工作。
回答by Jamie Macey
If you want to discard the changes,
如果您想放弃更改,
git checkout -- <file>
git checkout branch
If you want to keep the changes,
如果您想保留更改,
git stash save
git checkout branch
git stash pop
回答by romerun
well, it should be
嗯,应该是
git stash save
git checkout branch
// do something
git checkout oldbranch
git stash pop
回答by simplyharsh
Follow,
跟随,
$: git checkout -f
$: git checkout next_branch
回答by ccliffe
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,则必须执行以下操作:
git reset --hard origin/HEAD
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.
HEAD
单独只指本地提交/合并 - 有几次我忘记了当我完全打算取消所有更改/提交并返回到远程分支时,重置并最终得到“您的存储库是 X 提交..” .
回答by Greg Hewgill
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:
如果您更改了 Git 在切换分支时也需要更改的文件,它不会让您这样做。要放弃工作更改,请使用:
git reset --hard HEAD
Then, you will be able to switch branches.
然后,您将能够切换分支。
回答by vishnu viswanath
None of these answers helped me because I still had untracked files even after reset and stash. I had to do:
这些答案都没有帮助我,因为即使在重置和存储之后我仍然有未跟踪的文件。我必须做:
git reset --hard HEAD
git clean -d -f
回答by Kareem Jeiroudi
Easy Answer:
简单回答:
is to force checkout a branch
是强制结帐分支
git checkout -f <branch_name>
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.
强制签出分支是告诉 git 删除您在当前分支中所做的所有更改,并签出所需的更改。
or in case you're checking out a commit
或者如果您正在检查提交
git checkout -f <commit-hash>
"thought that I could change branches without committing. If so, how can I set this up? If not, how do I get out of this problem?"
“以为不用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.
答案是否定的,这实际上是 Git 的哲学,即您跟踪所有更改,并且每个节点(即提交)必须与您所做的最新更改保持同步,除非您已经当然做了一个新的提交。
You decided to keep changes?
你决定保持变化?
Then stash them using
然后使用
git stash
and then to unstash your changes in the desired branch, use
然后要在所需的分支中取消隐藏您的更改,请使用
git stash apply
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
这将应用您的更改,但也将它们保留在存储队列中。如果您不想将它们保留在存储堆栈中,请使用
git stash pop
That's the equivalent of apply
and then drop
这相当于apply
然后drop
回答by Jorge Avila
switching to a new branch losing changes:
切换到丢失更改的新分支:
git checkout -b YOUR_NEW_BRANCH_NAME --force
switching to an existing branch losing changes:
切换到现有分支丢失更改:
git checkout YOUR_BRANCH --force
回答by miguelacio
Close terminal, delete the folder where your project is, then clone again your project and voilá.
关闭终端,删除项目所在的文件夹,然后再次克隆您的项目,瞧。