Git 推送失败,“非快进更新被拒绝”

发布于 2024-11-27 02:00:08 字数 188 浏览 0 评论 0原文

我已经通过 Git Online 编辑了我的 GIT 存储库。在尝试推送本地代码更改后,出现错误:

Git push failed, To prevent from losing history, non-fast forward updates were rejected.

如何解决此问题?

I've edited my GIT repositories via Git Online. After I tried to push my local code changes, I got an error:

Git push failed, To prevent from losing history, non-fast forward updates were rejected.

How can I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

岁月流歌 2024-12-04 02:00:08

首先拉取更改:

git pull origin branch_name

Pull changes first:

git pull origin branch_name
少年亿悲伤 2024-12-04 02:00:08

如果您确定要推送,请在命令行中添加 --force 。例如,使用 git push origin --force (我推荐使用命令行,因为您会通过命令行找到其他用户的更多支持。而且,这对于 SmartGit 来说可能是不可能的。)请参阅此站点更多信息:http://help.github.com/remotes/

Add --force to your command line if you are sure you want to push. E.g. use git push origin --force (I recommend the command line as you will find much more support from other users with the command line. Also this may not be possible with SmartGit.) See this site for more information: http://help.github.com/remotes/

清风挽心 2024-12-04 02:00:08

在推送之前,使用 rebase 选项执行 git pull。这将获取您在线(在您的源中)所做的更改并在本地应用它们,然后在其之上添加您的本地更改。

git pull --rebase

现在,您可以推送到远程

git push 

有关更多信息,请查看 Git rebase 解释第 3.6 章 Git 分支 - 变基

Before pushing, do a git pull with rebase option. This will get the changes that you made online (in your origin) and apply them locally, then add your local changes on top of it.

git pull --rebase

Now, you can push to remote

git push 

For more information take a look at Git rebase explained and Chapter 3.6 Git Branching - Rebasing.

淡墨 2024-12-04 02:00:08

我遇到了同样的错误,只需在命令中添加 "--force" 即可

git push origin master --force

I encountered the same error, just add "--force" to the command, it works

git push origin master --force
记忆里有你的影子 2024-12-04 02:00:08

解决此问题的最安全方法是使用--rebase

例如,

git pull <remote> <branch> --rebase

这可能会导致本地分支发生冲突,您需要手动修复它们。

解决所有冲突后,您可以使用 --force-with-lease 推送更改,

例如

git push <remote> <branch> --force-with-lease

使用此标志,Git 检查分支的远程版本是否与您 rebase 的版本相同,即,如果有人在您变基时推送了新的提交,则该推送将被拒绝,并且您将被迫再次对您的分支进行变基。

如果您正在处理一个每小时有数百次提交的大型项目,这会很烦人,但这仍然是解决此问题的最佳方法。

避免使用--force除非你确切地知道自己在做什么。

使用 --force 具有破坏性,因为它会无条件地用本地的任何内容覆盖远程存储库。

但使用 --force-with-lease 可以确保您不会覆盖其他人的工作。

请在 Atlassian 的开发者博客中查看这篇文章的更多信息。

The safest way to solve this is using --rebase

E.g.

git pull <remote> <branch> --rebase

This may cause conflicts in your local branch, and you will need to fix them manually.

Once you resolve all the conflicts, you can push your change with --force-with-lease

E.g.

git push <remote> <branch> --force-with-lease

Using this flag, Git checks if the remote version of the branch is the same as the one you rebase, i.e. if someone pushed a new commit while you were rebasing, the push is rejected, and you will be forced to rebase your branch again.

It is annoying if you are working on a large project with hundreds of commits every hour, but it is still the best way to solve this problem.

AVOID USING --force unless you know exactly what you are doing.

Using --force is destructive because it unconditionally overwrites the remote repository with whatever you have locally.

But with --force-with-lease, ensure you don't overwrite other's work.

See more info on this post in Atlassian's developer blog.

一袭水袖舞倾城 2024-12-04 02:00:08

我也遇到过同样的问题。
原因是,我的本地分支机构不知何故失去了对远程分支机构的跟踪。

后,我就可以推动了。

git branch branch_name --set-upstream-to=origin/branch_name
git pull

解决了合并冲突

I've had the same problem.
The reason was, that my local branch had somehow lost the tracking to the remote counterpart.

After

git branch branch_name --set-upstream-to=origin/branch_name
git pull

and resolving the merging conflicts, I was able to push.

娇女薄笑 2024-12-04 02:00:08

使用 --rebase 选项对我有用。

  • git pull <远程> <分支> --rebase

然后推送到存储库。

  • git push <远程>

例如

git pull origin master --rebase

git Push origin master

Using the --rebase option worked for me.

  • git pull <remote> <branch> --rebase

Then push to the repo.

  • git push <remote> <branch>

E.g.

git pull origin master --rebase

git push origin master

轻许诺言 2024-12-04 02:00:08

(一)Netbeans 7.1的解决方案:尝试拉取。这很可能也会失败。现在查看日志(它们通常显示在 IDE 中)。有一行或多行说:

“由于此文件而拉取失败:”

搜索该文件,将其删除(之前进行备份)。通常它是一个 .gitignore 文件,因此您不会删除代码。重做推送。现在一切都应该正常了。

(One) Solution for Netbeans 7.1: Try a pull. This will probably also fail. Now have a look into the logs (they are usually shown now in the IDE). There's one/more line saying:

"Pull failed due to this file:"

Search that file, delete it (make a backup before). Usually it's a .gitignore file, so you will not delete code. Redo the push. Everything should work fine now.

离去的眼神 2024-12-04 02:00:08

我也遇到过同样的问题。
我解决了

git checkout <name branch>
git pull origin <name branch>
git push origin <name branch>

I've hade the same problem.
I resolved with

git checkout <name branch>
git pull origin <name branch>
git push origin <name branch>
狠疯拽 2024-12-04 02:00:08

这对我有用。它可以在 git 文档此处中找到,

如果您在您想要的分支上,您可以执行以下操作:

git fetch origin
# Fetches updates made to an online repository
git merge origin YOUR_BRANCH_NAME
# Merges updates made online with your local work

This is what worked for me. It can be found in git documentation here

If you are on your desired branch you can do this:

git fetch origin
# Fetches updates made to an online repository
git merge origin YOUR_BRANCH_NAME
# Merges updates made online with your local work
ぽ尐不点ル 2024-12-04 02:00:08

遇到了同样的问题,要解决它,请运行以下 git 命令。

  • git pull {url} --rebase
  • git push --set-upstream {url} master

您必须先在 github 上创建存储库。

Encountered the same problem, to solve it, run the following git commands.

  • git pull {url} --rebase
  • git push --set-upstream {url} master

You must have created the repository on github first.

纵情客 2024-12-04 02:00:08

有时,当从 git 中拉取时,HEAD 会分离。您可以通过输入命令来检查:

git branch 

<块引用>

  • (HEAD 与 8790704 分离)

    大师

    开发

最好移动到您的分支并从各自的分支获取新的拉取

git checkout develop

git pull origin develop

git push origin develop

Sometimes, while taking a pull from your git, the HEAD gets detached. You can check this by entering the command:

git branch 
  • (HEAD detached from 8790704)

    master

    develop

It's better to move to your branch and take a fresh pull from your respective branch.

git checkout develop

git pull origin develop

git push origin develop
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文