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

发布于 2024-08-02 05:02:38 字数 234 浏览 12 评论 0原文

我当时正在 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 技术交流群。

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

发布评论

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

评论(17

秋叶绚丽 2024-08-09 05:02:38

您需要一个干净的状态来更改分支。只有在不影响“脏文件”的情况下才允许分支签出(如 Charles Bailey 在评论)。

否则,您应该:

或者,最近:

即使索引或工作树与 HEAD 不同也继续进行。
索引和工作树都会恢复以匹配切换目标。

这与 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:

  • stash your current change or
  • reset --hard HEAD (if you do not mind losing those minor changes) or
  • 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. )

Or, more recently:

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.

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.

星星的轨迹 2024-08-09 05:02:38

如果您想放弃更改,

git checkout -- <file>
git checkout branch

如果您想保留更改,

git stash save
git checkout branch
git stash pop

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
毁梦 2024-08-09 05:02:38

嗯,应该是

git stash save
git checkout branch
// do something
git checkout oldbranch
git stash pop

well, it should be

git stash save
git checkout branch
// do something
git checkout oldbranch
git stash pop
給妳壹絲溫柔 2024-08-09 05:02:38

跟随,

$: git checkout -f

$: git checkout next_branch

Follow,

$: git checkout -f

$: git checkout next_branch
漆黑的白昼 2024-08-09 05:02:38

请注意,如果您合并了远程分支或有本地提交,并且想要返回到远程 HEAD,则必须执行以下操作:

git reset --hard origin/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:

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.

南风几经秋 2024-08-09 05:02:38

这些答案都没有帮助我,因为即使在重置和隐藏之后我仍然有未跟踪的文件。我必须做:

git reset --hard HEAD
git clean -d -f

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
青朷 2024-08-09 05:02:38

git checkout -f your_branch_name

git checkout -f your_branch_name

如果您在恢复更改时遇到问题:

git checkout .

如果您想删除未跟踪的目录和文件:

git clean -fd

git checkout -f your_branch_name

git checkout -f your_branch_name

if you have troubles reverting changes:

git checkout .

if you want to remove untracked directories and files:

git clean -fd
橪书 2024-08-09 05:02:38

如果你对文件进行了更改,Git 在切换分支时也需要更改,它不会让你这样做。要放弃工作更改,请使用:

git reset --hard HEAD

然后,您将能够切换分支。

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 reset --hard HEAD

Then, you will be able to switch branches.

纵情客 2024-08-09 05:02:38

请按照以下步骤操作:

  1. 即使您在分支之间切换,Git stash save 也会保存所有更改。
git stash 保存
  1. Git checkout 任何其他分支,现在既然您保存了更改,您就可以在任何分支中移动。上面的命令将确保您的更改被保存。
git checkout 分支
  1. 现在,当您返回分支时,请使用此命令取回所有更改。
git stash pop

Follow these steps:

  1. Git stash save will save all your changes even if you switch between branches.
git stash save
  1. Git checkout any other branch, now since you saved your changes you can move around any branch. The above command will make sure that your changes are saved.
git checkout branch
  1. Now when you come back to the branch use this command to get all your changes back.
git stash pop
旧伤还要旧人安 2024-08-09 05:02:38

简单答案:

强制签出分支

git checkout -f <branch_name>

强制签出分支是告诉 git 删除您在当前分支中所做的所有更改,并签出所需的更改。

或者如果您正在检查提交

git checkout -f <commit-hash>

“认为我可以在不提交的情况下更改分支。如果是这样,如何
我可以设置这个吗?如果没有,我该如何摆脱这个问题?”

答案是,这就是 Git 的哲学,即跟踪所有更改,并且每个节点(即提交)都有及时了解您所做的最新更改,当然,除非您进行了新的提交,

决定保留更改吗?

然后使用 来存储它们

git stash

,然后将您的更改取消存储到所需的分支中 。 ,使用

git stash apply

它会应用您的更改,但也将它们保留在存储队列中。如果您不想将它们保留在存储堆栈中,则使用

git stash pop

这相当于 apply 然后 弹出它们。 >下降

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.

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?"

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

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

内心荒芜 2024-08-09 05:02:38

切换到丢失更改的新分支:

git checkout -b YOUR_NEW_BRANCH_NAME --force

切换到丢失更改的现有分支:

git checkout YOUR_BRANCH --force

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
谁把谁当真 2024-08-09 05:02:38

如果您想保留更改并在单行命令中更改分支

git stash && git checkout <branch_name> && git stash pop

If you want to keep the changes and change the branch in a single line command

git stash && git checkout <branch_name> && git stash pop
清秋悲枫 2024-08-09 05:02:38

为了你的心理平静
(为了更容易地访问您在分支切换时未提交的更改)

切换之前:

git checkout <next_branch>

使用

git stash save "brief description of changes"

而不是默认值:

git stash    
// or
git stash save

如果您的 git stash 列表 是一个较长的列表,那么这是值得的
并且必须切换回以前从某个地方开始的想法。

For your mental calmness
(to have much easier access to changes you have left uncomitted doing a branch switch)

Before switching:

git checkout <next_branch>

use

git stash save "brief description of changes"

instead of the default:

git stash    
// or
git stash save

This pays off if your git stash list is a longer list
and must switch back to some previous idea started somewhere there.

昵称有卵用 2024-08-09 05:02:38

将未提交的更改移至新分支

我为此创建了一个 .gitconfig 别名:

[alias]
spcosp = !"git stash push && git checkout \"$@\" && git stash pop --index #"

要更改为 new-branch-name,请使用:

git spcosp new-branch-name

以及任何未提交的文件和索引更改将被保留。

Move uncommited changes to a new branch

I created a .gitconfig alias for this:

[alias]
spcosp = !"git stash push && git checkout \"$@\" && git stash pop --index #"

To change to new-branch-name, use:

git spcosp new-branch-name

And any non-commited file and index changes will be kept.

风轻花落早 2024-08-09 05:02:38

当 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

双马尾 2024-08-09 05:02:38

关闭终端,删除项目所在的文件夹,然后再次克隆您的项目,瞧。

Close terminal, delete the folder where your project is, then clone again your project and voilá.

杀お生予夺 2024-08-09 05:02:38

git stash 命令用于保存尚未提交到临时区域的更改,以便您可以切换到不同的分支,进行更改,然后返回到原始更改。

git stash save "Your stash message"
git checkout <Target branch>

// 做你的工作:改变、提交或者你想要的

git checkout <Current branch>
git stash list
git stash apply stash@{n}

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.

git stash save "Your stash message"
git checkout <Target branch>

// Do your work: change, commit or what your are want

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