简体   繁体   English

只有在没有冲突的情况下,我如何才能让 git-checkout 合并本地修改?

[英]How can I have git-checkout merge local modifications only if there's no conflict?

When I use git checkout -m , it merges local modifications, which makes life easier when a file has changed between two commits but in a completely separate place from where my local modifications are.当我使用git checkout -m时,它会合并本地修改,当文件在两次提交之间发生更改但与我的本地修改所在的位置完全不同时,这会使生活更轻松。 However, this becomes a messy merge conflict that has to be resolved when there is indeed a conflict, and I can't simply go back to the previous branch.但是,这变成了一个混乱的合并冲突,当确实存在冲突时必须解决,我不能简单地 go 回到上一个分支。 I also don't want to have to use git stash push and git stash pop all the time, as that is cumbersome.我也不想一直使用git stash pushgit stash pop ,因为这很麻烦。 Any ideas what to do?有什么想法该怎么做?

Use the stash:使用存储:

git stash save
git checkout where-I-want-to-go
git stash pop

If it fails when running git stash pop (which is where the conflict might arise) you simply run back to where you were before, as if nothing had happened:如果在运行 git stash pop 时失败(这是可能发生冲突的地方),您只需运行回到之前的位置,就好像什么都没发生一样:

git checkout -f - # with single -.  yep, this takes you back, just like that
git stash pop # just as if you didn't move anywhere

The alternative is to use git worktree in order to manage multiple worktrees (one per branch)另一种方法是使用git worktree来管理多个工作树(每个分支一个)

That way, you don't have to checkout/switch branches, you go to a different folder, and you can import changes you want from one folder to the other if you need to.这样,您不必签出/切换分支,将 go 到另一个文件夹,如果需要,您可以将所需的更改从一个文件夹导入到另一个文件夹。

You might consider what I do overly cumbersome as well, but:您可能会认为我所做的事情也过于繁琐,但是:

  1. Attempt git switch right-branch .尝试git switch right-branch If this works, great.如果这行得通,那就太好了。 If not, proceed to...如果没有,请继续...
  2. Run git commit -m temp-commit-do-not-use .运行git commit -m temp-commit-do-not-use
  3. Run git switch right-branch .运行git switch right-branch
  4. Run git cherry-pick -n wrong-branch .运行git cherry-pick -n wrong-branch Resolve conflicts if any.如果有冲突,请解决。 Commit (replacing the "temp-commit-do-not-use" message of course).提交(当然要替换“temp-commit-do-not-use”消息)。
  5. Run git switch wrong-branch and git reset --hard HEAD^ to remove the temporary do-not-use-this-commit commit.运行git switch wrong-branchgit reset --hard HEAD^以删除临时 do-not-use-this-commit 提交。

Consider also using git worktree (as VonC suggests ), since you can then open one window on right-branch and one on wrong-branch and swap back and forth just by moving your mouse.还可以考虑使用git worktree (如VonC 建议的那样),因为您可以在right-branch上打开一个 window,在wrong-branch上打开一个,然后只需移动鼠标即可来回交换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM