git 如何在 GitHub 上合并远程更改?

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

How to merge remote changes at GitHub?

gitgithub

提问by John

I'm getting following error, whn trying first Github push:

我在尝试第一次 Github 推送时遇到以下错误:

[rejected] master -> master (non-fast forward)
error: failed to push some refs to '[email protected]:me/me.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.  See the 'non-fast forward'
section of 'git push --help' for details.

how can I fix this and merge remote changes?

如何解决此问题并合并远程更改?

采纳答案by Thilo

See the 'non-fast forward' section of 'git push --help' for details.

有关详细信息,请参阅“ git push --help”的“非快进”部分。

You can perform "git pull", resolve potential conflicts, and "git push" the result. A "git pull" will create a merge commit C between commits A and B.

Alternatively, you can rebase your change between X and B on top of A, with "git pull --rebase", and push the result back. The rebase will create a new commit D that builds the change between X and B on top of A.

您可以执行“git pull”,解决潜在的冲突,并“git push”结果。“git pull”将在提交 A 和 B 之间创建一个合并提交 C。

或者,您可以使用“git pull --rebase”将 X 和 B 之间的更改重新设置在 A 之上,并将结果推回。rebase 将创建一个新的提交 D,它在 A 之上构建 X 和 B 之间的更改。

回答by SammyK

You can also force a push by adding the + symbol before your branch name.

您还可以通过在分支名称前添加 + 符号来强制推送。

git push origin +some_branch

回答by Jorge Israel Pe?a

You probably have changes on github that you never merged. Try git pullto fetch and merge the changes, then you should be able to push. Sorry if I misunderstood your question.

您可能在 github 上有从未合并的更改。尝试git pull获取并合并更改,然后您应该能够推送。对不起,如果我误解了你的问题。

回答by David Calhoun

If you "git pull" and it says "Already up-to-date.", and still get this error, it might be because one of your other branches isn't up to date. Try switching to another branch and making sure that one is also up-to-date before trying to "git push" again:

如果您“git pull”并显示“已经是最新的。”,并且仍然出现此错误,则可能是因为您的其他分支之一不是最新的。尝试切换到另一个分支并确保一个分支也是最新的,然后再尝试“git push”:

Switch to branch "foo" and update it:

切换到分支“foo”并更新它:

$ git checkout foo
$ git pull

You can see the branches you've got by issuing command:

您可以通过发出命令来查看您拥有的分支:

$ git branch

回答by JuLy

You can force it to push, but please do this ONLY when you're quite sure what you are doing.

您可以强制它推动,但请仅在您非常确定自己在做什么时才这样做。

The command is:

命令是:

git push -f 

回答by Zds

This problem can also occur when you have conflicting tags. If your local version and remote version use same tag name for different commits, you can end up here.

当您有冲突的标签时,也会出现此问题。如果你的本地版本和远程版本对不同的提交使用相同的标签名称,你可以在这里结束。

You can solve it my deleting the local tag:

您可以通过删除本地标签来解决它:

$ git tag --delete foo_tag

回答by IgorGanapolsky

When I got this error, I backed up my entire project folder. Then I did something like

当我收到此错误时,我备份了整个项目文件夹。然后我做了类似的事情

$ git config branch.master.remote origin
$ git config branch.master.merge refs/heads/master

...depending on your branch name (if it's not master).

...取决于您的分支名称(如果它不是主分支)。

Then I did git pull --rebase. After that, I replaced the pulled files with my backed-up project's files. Now I am ready to commit my changes again and push.

然后我做到了git pull --rebase。之后,我用我备份的项目文件替换了拉取的文件。现在我准备再次提交我的更改并推送。

回答by Preeti Duhan

1)Forcing a pull to overwrite local changes

1)强制拉动覆盖本地更改

If you don't care about the changes done locallyand want to obtain the code from the repository, you can force a pull. This will overwrite all the local changes done on your computer a duplicate copy of the version in the repository will appear.

如果您不关心在本地完成的更改并希望从存储库中获取代码,则可以强制拉取。这将覆盖在您的计算机上所做的所有本地更改,将出现存储库中版本的重复副本。

Execute the following commands in your IDE:

在 IDE 中执行以下命令:

git reset -- hard

git pull

This will instantly destroy all your local changes so make sure that you know what you are doing and don't need your local changes.

这将立即破坏您所有的本地更改,因此请确保您知道自己在做什么并且不需要您的本地更改。

2)Keeping both changes (local and from the repo)

2)保留两个更改(本地和来自回购)

If you want to keep both changes (changes done locally and changes present in the repository), you can add and commit your changes. When you pull, there will obviously be a merge conflict. Here you can use the tools in your IDE (such as Difftool and mergetool) to compare the two pieces of code and determine which changes to keep and which to remove. This is the middle way; no changes will be lost until you manually remove them.

如果您想保留这两个更改(在本地完成的更改和存储库中存在的更改),您可以添加并提交您的更改。拉的时候,显然会出现合并冲突。在这里,您可以使用 IDE 中的工具(例如 Difftool 和 mergetool)来比较两段代码并确定要保留哪些更改以及要删除哪些更改。这是中道;除非您手动删除它们,否则不会丢失任何更改。

git add $the_file_under_error

git commit

git pull