git switch 分支而不丢弃本地更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22082307/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:49:08  来源:igfitidea点击:

git switch branch without discarding local changes

gitgit-branch

提问by megawac

Alright, lets say one day we make happen to make a bunch of modifications and when we go to commit them we notice we were working on the wrong branch.

好吧,假设有一天我们碰巧做了一堆修改,当我们提交它们时,我们注意到我们在错误的分支上工作。

How can we force git to switch branches without discarding local changes.

我们如何在不丢弃本地更改的情况下强制 git 切换分支。

I'm probably going to go about this in a naive way while I wait a reply, but I would like to know if theres a correct procedure as I'd be lying if I said this hasn't happened to me before...

我可能会在等待回复时以一种天真的方式解决这个问题,但我想知道是否有正确的程序,因为如果我说这之前没有发生在我身上,我会撒谎......

  • Backup changed repo
  • git reset --hard
  • git checkout right-branch
  • Restore changes
  • git commit -m "changes"
  • 备份更改的 repo
  • git reset --hard
  • git checkout right-branch
  • 恢复更改
  • git commit -m "changes"

回答by torek

There are a bunch of different ways depending on how far along you are and which branch(es) you want them on.

有很多不同的方法,具体取决于您走了多远以及您希望它们位于哪个分支。

Let's take a classic mistake:

让我们举一个经典的错误:

$ git checkout master
... pause for coffee, etc ...
... return, edit a bunch of stuff, then: oops, wanted to be on develop

So now you want these changes, which you have not yet committed to master, to be on develop.

所以,现在你想这些变化,你还没有承诺master,要论develop

  1. If you don't havea developyet, the method is trivial:

    $ git checkout -b develop
    

    This creates a new developbranch starting from wherever you are now. Now you can commit and the new stuff is all on develop.

  2. You do havea develop. See if Git will let you switch without doing anything:

    $ git checkout develop
    

    This will either succeed, or complain. If it succeeds, great! Just commit. If not (error: Your local changes to the following files would be overwritten ...), you stillhave lots of options.

    The easiest is probably git stash(as all the other answer-ers that beat me to clicking postsaid). Run git stash saveor git stash push,1or just plain git stashwhich is short for save/ push:

    $ git stash
    

    This commits your code (yes, it really does make some commits) using a weird non-branch-y method. The commits it makes are not "on" any branch but are now safely stored in the repository, so you can now switch branches, then "apply" the stash:

    $ git checkout develop
    Switched to branch 'develop'
    $ git stash apply
    

    If all goes well, and you like the results, you should then git stash dropthe stash. This deletes the reference to the weird non-branch-y commits. (They're still in the repository, and can sometimes be retrieved in an emergency, but for most purposes, you should consider them gone at that point.)

  1. 如果你没有一个develop呢,方法很简单:

    $ git checkout -b develop
    

    这会develop从您现在所在的任何地方创建一个新分支。现在你可以提交,新的东西都在develop

  2. 确实有一个develop. 看看 Git 是否会让你不做任何事情就切换:

    $ git checkout develop
    

    这要么成功,要么抱怨。如果成功了,太好了!只要承诺。如果不是 ( error: Your local changes to the following files would be overwritten ...),您仍然有很多选择。

    最简单的可能是git stash(正如所有其他击败我点击的回答者post所说)。运行git stash saveor git stash push, 1或者只是简单git stashsave/ 的缩写push

    $ git stash
    

    这将使用一种奇怪的非分支方法提交您的代码(是的,它确实进行了一些提交)。它所做的提交不是“在”任何分支上,而是现在安全地存储在存储库中,因此您现在可以切换分支,然后“应用”存储:

    $ git checkout develop
    Switched to branch 'develop'
    $ git stash apply
    

    如果一切顺利,并且您喜欢结果,那么您应该git stash drop存储。这将删除对奇怪的非分支 y 提交的引用。(它们仍在存储库中,有时可以在紧急情况下检索,但在大多数情况下,您应该认为它们此时已消失。)

The applystep does a merge of the stashed changes, using Git's powerful underlying merge machinery, the same kind of thing it uses when you do branch merges. This means you can get "merge conflicts" if the branch you were working on by mistake, is sufficiently different from the branch you meant to be working on. So it's a good idea to inspect the results carefullybefore you assume that the stash applied cleanly, even if Git itself did not detect any merge conflicts.

apply步骤使用 Git 强大的底层合并机制合并隐藏的更改,这与执行分支合并时使用的内容相同。这意味着如果您错误地处理的分支与您打算处理的分支有很大不同,您可能会遇到“合并冲突”。因此,即使 Git 本身没有检测到任何合并冲突,在您假设 stash 应用干净之前仔细检查结果也是一个好主意

Many people use git stash pop, which is short-hand for git stash apply && git stash drop. That's fine as far as it goes, but it means that if the application results in a mess, and you decide you don't want to proceed down this path, you can't get the stash back easily. That's why I recommend separate apply, inspect results, droponly if/when satisfied. (This does of course introduce another point where you can take another coffee break and forget what you were doing, come back, and do the wrongthing, so it's not a perfect cure.)

许多人使用git stash pop,这是 的简写git stash apply && git stash drop。就目前而言这很好,但这意味着如果应用程序导致混乱,并且您决定不想沿着这条路继续前进,则无法轻松取回存储。这就是为什么我建议单独apply检查结果,drop仅当/当满意时。(这当然会引入另一点,你可以再喝一次咖啡,忘记你在做什么,回来做错事,所以这不是一个完美的治疗方法。)



1The savein git stash saveis the old verb for creating a new stash. Git version 2.13 introduced the new verb to make things more consistent with popand to add more options to the creation command. Git version 2.16 formally deprecated the old verb (though it still works in Git 2.23, which is the latest release at the time I am editing this).

1savegit stash save是老动词用于创建新的存储。Git 2.13 版引入了新动词,使之与pop创建命令更加一致并向创建命令添加更多选项。Git 2.16 版正式弃用了旧动词(尽管它在 Git 2.23 中仍然有效,这是我编辑时的最新版本)。

回答by Ray

Use git stash

使用 git 存储

git stash

It pushes changes to a stack. When you want to pull them back use

它将更改推送到堆栈。当你想把它们拉回来时使用

 git stash apply

You can even pull individual items out. To completely blow away the stash:

您甚至可以拉出单个项目。要完全吹走藏匿处:

 git stash clear

回答by brokenfoot

  • git stashto save your uncommited changes
  • git stash listto list your saved uncommited stashes
  • git stash apply stash@{x}where x can be 0,1,2..no of stashes that you have made
  • git stash保存您未提交的更改
  • git stash list列出您保存的未提交存储
  • git stash apply stash@{x}其中 x 可以是 0,1,2..no 你所做的隐藏

回答by X3074861X

You can either :

你可以:

  • Use git stashto shelve your changes or,

  • Create another branch and commit your changes there, and then merge that branch into your working directory

  • 使用git stash搁置更改,或者,

  • 创建另一个分支并在那里提交您的更改,然后将该分支合并到您的工作目录中