git Git告诉我拉,然后提交,然后拉?

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

Git telling me to pull, then commit, then pull?

gitgithub

提问by egidra

I am trying to push new changes, but I have a conflicted file. After trying to push, I get the following error:

我正在尝试推送新的更改,但我有一个冲突的文件。尝试推送后,出现以下错误:

Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

Ok, so we need to use git pull. I try to use git pulland then I get this error:

好的,所以我们需要使用 git pull。我尝试使用git pull,然后出现此错误:

error: Your local changes to the following files would be overwritten by merge:
    db/profile_edit.php
Please, commit your changes or stash them before you can merge.

But, when I try to commit, I go back to the first error. What should I do? The changes on the remote repo are newer than the ones on my local machine. So, how do I open it up with a diff tool and make the changes and then tell git that I have made changes so it will let me push changes?

但是,当我尝试提交时,我又回到了第一个错误。我该怎么办?远程 repo 上的更改比我本地机器上的更改新。那么,如何使用 diff 工具打开它并进行更改,然后告诉 git 我已经进行了更改,以便让我推送更改?

回答by Gabriel L. Oliveira

Try to do

试着做

$ git pull --rebase

To pull remote changes before yours, and then commit. And see if it works.

在你之前拉远程更改,然后提交。看看它是否有效。

If this does not work, try this instead:

如果这不起作用,请尝试以下操作:

$ git stash
$ git pull --rebase
$ git stash pop

To save your changes on the stash, apply remote commits inside your work-repository, and then apply your changes (saved into stash) inside your work-repository again.

要将更改保存在 上stash,请在工作存储库中应用远程提交,然后再次将更改(保存到stash)应用到工作存储库中。

回答by vhallac

First, I think if you were to git addthen git commit, your local repo would be clean enough to do a git pull.

首先,我认为如果您当时git add这样做git commit,您的本地存储库将足够干净,可以执行git pull.

However, if there may be changes you don't want to commit until you see what happened on the upstream, you can use git stash. It will temporarily clean up your working directory and save your changes, so you can pull (I would recommend git pull --rebaseto avoid merge points - but it is a personal taste issue). Once you have upstream changes pulled, you can get your local modifications back using git stash pop. After you clean up conflicts and get rid of unnecessary changes, you can add, commit, then finally push.

但是,如果您在看到上游发生的事情之前不想提交更改,则可以使用git stash. 它将临时清理您的工作目录并保存您的更改,以便您可以拉取(我建议git pull --rebase避免合并点 - 但这是个人品味问题)。拉取上游更改后,您可以使用git stash pop. 在清理冲突并摆脱不必要的更改后,您可以添加、提交,然后最后推送。

回答by Tekkub

You're getting yourself confused with the commands.

你让自己对命令感到困惑。

git commitsaves changes into git's database git pullbrings remote commits into your repo

git commit将更改保存到 git 的数据库 git pull中将远程提交带入您的存储库

You're trying to pullwith uncommitted changes, so git is asking you to committhose first (or git stashthem away) so that it can merge your local changes with the remote ones.

您正在尝试拉取未提交的更改,因此 git 要求您首先提交(或将git stash它们提交),以便它可以将您的本地更改与远程更改合并。