git:切换分支并忽略任何更改而不提交

本文翻译自:git: Switch branch and ignore any changes without committing

I was working on a git branch and was ready to commit my changes, so I made a commit with a useful commit message. 我正在使用git分支并准备提交我的更改,因此我使用有用的提交消息进行了提交。 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给了我,

error: You have local changes to "X"; 错误:您对“X”进行了本地更改; cannot switch branches. 不能切换分支。

I 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? 如果没有,我该如何摆脱这个问题? I want to ignore the minor changes without committing and just change branches. 我想忽略这些微小的变化而不提交,只是改变分支。


#1楼

参考:https://stackoom.com/question/5tom/git-切换分支并忽略任何更改而不提交


#2楼

If you have made changes to files that Git also needs to change when switching branches, it won't let you. 如果您对切换分支时Git也需要更改的文件进行了更改,则不会让您这样做。 To discard working changes, use: 要放弃工作更改,请使用:

git reset --hard HEAD

Then, you will be able to switch branches. 然后,您将能够切换分支。


#3楼

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). 只有在不影响“脏文件”的情况下才允许分支结账(正如Charles Bailey在评论中所说的那样)。

Otherwise, you should either: 否则,您应该:

  • stash your current change or 存储你当前的变化或
  • reset --hard HEAD (if you do not mind losing those minor changes) or reset --hard HEAD (如果你不介意失去那些微小的变化)或
  • checkout -f (When switching branches, proceed even if the index or the working tree differs from HEAD. This is used to throw away local changes. ) checkout -f (当切换分支时,即使索引或工作树与HEAD不同,也要继续。这用于丢弃本地更改。)

#4楼

Follow, 跟随,

$: git checkout -f

$: git checkout next_branch

#5楼

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

#6楼

well, it should be 好吧,它应该是

git stash save
git checkout branch
// do something
git checkout oldbranch
git stash pop
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值