Git - 从 master 更新当前分支而不提交当前分支更改

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

Git - Update current branch from master without committing current branch changes

gitgithub

提问by user2568374

I have looked at numerous questions on this same topic and they all have different answers and it is confusing.

我查看了关于同一主题的许多问题,它们都有不同的答案,令人困惑。

Some say commit your changes first but I don't want to do this.

有人说先提交更改,但我不想这样做。

I am using Git Hub too, and do not understand how this works with the website commands such as create pull request , compare across forks, try changing your base, etc...

我也在使用 Git Hub,但不明白这如何与网站命令一起使用,例如 create pull request 、跨分支比较、尝试更改基础等...

I thought just dragging my current branch to the right square and dragging the repo master branch to the left square and then clicking the Merge Branches button would work but then there is a "sync" button on the upper right that needs to be clicked after that, I guess, and then you need to do a pull request on the website....etc...etc.

我以为只需将我当前的分支拖到右边的方块并将 repo master 分支拖到左边的方块然后单击 Merge Branches 按钮就可以了,但是右上角有一个“同步”按钮需要在此之后单击,我猜,然后你需要在网站上做一个拉取请求......等等......等等。

Sheesh, in CVS I just clicked update and it brought down all the changes in the Head to my current and that was that.

Sheesh,在 CVS 中,我只需单击更新,它将 Head 中的所有更改都降到我当前的状态,仅此而已。

There are three options I guess. Using git hub, using the website, and/or using the command line. How can this be simplified?

我猜有三种选择。使用 git hub、使用网站和/或使用命令行。如何简化?

回答by Makoto

It depends on what state your local repository is in relative to upstream.

这取决于您的本地存储库相对于上游的状态。

  • If there are conflicts, then you are better off stashing your work before pulling your branch in. That can be accomplished thus:

    git add . && git stash save
    git pull
    git stash pop
    

    If you don't want to deal with merging, then you can rebase your branch instead, which doesn't require that you save off your work (but will prompt you for conflicts):

    git pull --rebase
    

    You'll have to deal with conflicts using your merge tool of choice.

  • If there are no conflicts, then you can simply pull the branch in.

    git pull
    
  • 如果有冲突,那么你最好在拉入分支之前隐藏你的工作。这可以通过以下方式完成:

    git add . && git stash save
    git pull
    git stash pop
    

    如果你不想处理合并,那么你可以重新设置你的分支,这不需要你保存你的工作(但会提示你发生冲突):

    git pull --rebase
    

    您必须使用您选择的合并工具来处理冲突。

  • 如果没有冲突,那么您可以简单地将分支拉入。

    git pull
    

Github's role in all of this is simply to provide the remote repository in which you are pulling from/pushing to. There's no need to worry about it unless you don't have a remote set up; then I'd refer you to their wonderful documentationabout getting a remote repository set up.

Github 在所有这些中的作用只是提供您要从中拉取/推送到的远程存储库。除非您没有远程设置,否则无需担心;然后我会向您推荐他们关于设置远程存储库的精彩文档

回答by RJFalconer

Your situation:

你的情况:

  • You're on a branch that may or may not be available upstream, which is branched from an old commit of master
  • You have no unstaged changes
  • You have uncommitted changes
  • 您所在的分支可能在上游可用,也可能不可用,它是从 master 的旧提交分支而来的
  • 您没有未暂存的更改
  • 您有未提交的更改

"Some say commit your changes first but I don't want to do this"

“有人说先提交更改,但我不想这样做”

There's no real reason not to; it's still your private working branch until you pushit upstream.

没有真正的理由不这样做;它仍然是你的私人工作分支,直到你push成为上游。

So:

所以:

If you've not pushed yet:

如果你还没有推送:

  • git commit -m WorkInProgress(or git stash)
  • git rebase master
  • Resolve any conflicts
  • git stash popif you stashed
  • git commit -m WorkInProgress(或git stash
  • git rebase master
  • 解决任何冲突
  • git stash pop如果你藏起来

If you have pushed already:

如果您已经推送:

  • Ensure your current branch's commits are production quality
  • git stash
  • git pull(will not conflict, but will create merge commit)
  • git stash pop
  • 确保您当前分支的提交具有生产质量
  • git stash
  • git pull(不会冲突,但会创建合并提交)
  • git stash pop

回答by Ezzored

You can stash your changes in the current branch with git stash. For more information, please see this: https://git-scm.com/book/en/v1/Git-Tools-Stashing

您可以使用 git stash 将更改存储在当前分支中。有关更多信息,请参阅:https: //git-scm.com/book/en/v1/Git-Tools-Stashing

回答by Alim ?zdemir

You could do Patches which most IDEs and some tools provide, or if using Intellij IDEA there is a similar construct to stashing which is shelving, these changes are not persisted in git, but rather in IDEAs project files.

你可以做大多数 IDE 和一些工具提供的补丁,或者如果使用 Intellij IDEA 有一个类似于搁置的 stashing 的构造,这些更改不会保存在 git 中,而是保存在 IDEA 项目文件中。

But in the end, for your current situation git stashis the way to go.

但最终,对于您目前的情况,这git stash是要走的路。

In the longer run, it's probably better to just read a little bit more about git, since one of the differences between it and CVS is, you have to think two phases now. You don't interact directly with your remote github or whatever. Your local repo is like your former "Save" button and the remote repo sync's with your local. And even in CVS most systems complained if you forgot to save...

从长远来看,多读一点关于 git 的内容可能会更好,因为它与 CVS 之间的区别之一是,您现在必须考虑两个阶段。您不直接与远程 github 或其他任何交互。您的本地存储库就像您以前的“保存”按钮以及与本地的远程存储库同步。即使在 CVS 中,如果您忘记保存,大多数系统也会抱怨...