简体   繁体   English

Git 推送被拒绝“非快进”

[英]Git push rejected "non-fast-forward"

I am fairly new to git , yet currently using it to manage our code in a team environment.我是git的新手,但目前正在使用它来管理我们在团队环境中的代码。 I had some rebasing issues, and I fixed them using:我遇到了一些变基问题,我使用以下方法修复了它们:

git checkout --ours filename.txt
git add filename.txt
git rebase --continue

Now I wish to push my changes, and so running the following command:现在我想推送我的更改,因此运行以下命令:

$ git push origin feature/my_feature_branch

Gave me the following error:给我以下错误:

To ssh://git@coderepo.com:7999/repo/myproject.git
 ! [rejected]        feature/my_feature_branch -> feature/my_feature_branch (non-fast-forward)
error: failed to push some refs to 'ssh://git@coderepo.com:7999/repo/myproject.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

What can I do to get rid of this error?我该怎么做才能摆脱这个错误?

Note: I am avoiding the use of --force option as much as possible.注意:我尽可能避免使用--force选项。

It looks, that someone pushed new commits between your last git fetch and git push .看起来,有人在你最后一次git fetchgit push之间推送了新的提交。 In this case you need to repeat your steps and rebase my_feature_branch one more time.在这种情况下,您需要重复您的步骤并再一次 rebase my_feature_branch

git fetch
git rebase feature/my_feature_branch
git push origin feature/my_feature_branch

After the git fetch I recommend to examine situation with gitk --all .git fetch之后,我建议使用gitk --all检查情况。

Probably you did not fetch the remote changes before the rebase or someone pushed new changes (while you were rebasing and trying to push).可能您在变基之前没有获取远程更改,或者有人推送了新的更改(当您变基并尝试推送时)。 Try these steps:尝试以下步骤:

#fetching remote 'feature/my_feature_branch' branch to the 'tmp' local branch 
git fetch origin feature/my_feature_branch:tmp

#rebasing on local 'tmp' branch
git rebase tmp

#pushing local changes to the remote
git push origin HEAD:feature/my_feature_branch

#removing temporary created 'tmp' branch
git branch -D tmp

try this command试试这个命令

$ git push -f -u origin <name of branch>

ie $ git push -f -u origin master$ git push -f -u origin master

I had this problem!我有这个问题! I tried: git fetch + git merge, but dont resolved!我试过:git fetch + git merge,但没有解决! I tried: git pull, and also dont resolved我试过:git pull,也没有解决

Then I tried this and resolved my problem (is similar of answer of Engineer):然后我尝试了这个并解决了我的问题(类似于工程师的回答):

git fetch origin master:tmp
git rebase tmp
git push origin HEAD:master
git branch -D tmp

I'm late to the party but I found some useful instructions in github help page and wanted to share them here.我迟到了,但我在github 帮助页面中找到了一些有用的说明,并想在这里分享。

Sometimes, Git can't make your change to a remote repository without losing commits.有时,Git 无法在不丢失提交的情况下对远程存储库进行更改。 When this happens, your push is refused.发生这种情况时,您的推送将被拒绝。

If another person has pushed to the same branch as you, Git won't be able to push your changes:如果其他人与您推送到同一分支,Git 将无法推送您的更改:

$ git push origin master
To https://github.com/USERNAME/REPOSITORY.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/USERNAME/REPOSITORY.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

You can fix this by fetching and merging the changes made on the remote branch with the changes that you have made locally:您可以通过获取远程分支上所做的更改并将其与您在本地所做的更改合并来解决此问题:

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

Or, you can simply use git pull to perform both commands at once:或者,您可以简单地使用git pull同时执行这两个命令:

$ git pull origin YOUR_BRANCH_NAME
# Grabs online updates and merges them with your local work

我有一个类似的问题,我解决了它: git pull origin

In my case for exact same error, I was not the only developer as well.就我而言,对于完全相同的错误,我也不是唯一的开发人员。

So I went to commit & push my changes at same time, seen at bottom of the Commit dialog popup:所以我同时提交并推送我的更改,在Commit对话框弹出窗口的底部可以看到:

选中选项:立即将更改推送到原点

...but I made the huge mistake of forgetting to hit the Fetch button to see if I have latest, which I did not. ...但是我犯了一个巨大的错误,就是忘记点击Fetch按钮来查看我是否有最新的,而我没有。

The commit successfully executed, however not the push, but instead gives the same mentioned error;提交成功执行,但不是推送,而是给出了相同的错误; ...even though other developers didn't alter same files as me, I cannot pull latest as same error is presented. ...即使其他开发人员没有更改与我相同的文件,我也无法提取最新的文件,因为出现了相同的错误。

The GUI Solution图形用户界面解决方案

Most of the time I prefer sticking with Sourcetree's GUI (Graphical User Interface) .大多数时候我更喜欢坚持使用 Sourcetree 的GUI(图形用户界面) This solution might not be ideal, however this is what got things going again for me without worrying that I may lose my changes or compromise more recent updates from other developers.这个解决方案可能并不理想,但这就是让我重新开始的原因,而不用担心我可能会丢失我的更改或损害其他开发人员的最新更新。

STEP 1步骤1

Right-click on the commit right before yours to undo your locally committed changes and select Reset current branch to this commit like so:右键单击您之前的提交以撤消本地提交的更改,然后选择Reset current branch to this commit ,如下所示:

Sourcetree 窗口,右键单击提交并选择:Reset current branch to this commit

STEP 2第2步

Once all the loading spinners disappear and Sourcetree is done loading the previous commit, at the top-left of window, click on Pull button...一旦所有加载微调器消失并且 Sourcetree 完成加载上一个提交,在窗口的左上角,单击Pull按钮...

突出显示“拉”按钮的 Sourcetree 窗口

...then a dialog popup will appear, and click the OK button at bottom-right: ...然后会弹出一个对话框,然后单击右下角的OK按钮:

突出显示“确定”按钮的 Sourcetree 窗口对话框

STEP 3第 3 步

After pulling latest, if you do not get any errors, skip to STEP 4 (next step below).拉完最新版后,如果没有出现任何错误,请跳到第 4步(下面的下一步)。 Otherwise if you discover any merge conflicts at this point, like I did with my Web.config file:否则,如果您此时发现任何合并冲突,就像我对Web.config文件所做的那样:

显示错误提示的 Sourcetree 窗口:更新被拒绝,因为您当前分支的尖端落后

...then click on the Stash button at the top, a dialog popup will appear and you will need to write a Descriptive-name-of-your-changes, then click the OK button: ...然后单击顶部的Stash按钮,将出现一个对话框弹出窗口,您需要编写一个 Descriptive-name-of-your-changes,然后单击OK按钮:

突出显示 Stash 按钮的 Sourcetree 窗口和弹出对话框,显示输入以命名您的存储,并突出显示 OK 按钮

...once Sourcetree is done stashing your altered file(s), repeat actions in STEP 2 (previous step above), and then your local files will have latest changes. ...一旦 Sourcetree 完成存储您更改的文件,重复步骤 2中的操作(上述上一步),然后您的本地文件将具有最新更改。 Now your changes can be reapplied by opening your STASHES seen at bottom of Sourcetree left column, use the arrow to expand your stashes, then right-click to choose Apply Stash 'Descriptive-name-of-your-changes' , and after select OK button in dialog popup that appears:现在可以通过打开STASHES底部的 STASHES 来重新应用您的更改,使用箭头展开您的 stash,然后右键单击选择Apply Stash 'Descriptive-name-of-your-changes' ,然后选择OK弹出对话框中的按钮:

扩展了 Stashes 部分的 Sourcetree 窗口,右键单击更改并突出显示 Apply Stash

Sourcetree 对话框弹出窗口要求您确认是否要应用存储您的本地副本

IF you have any Merge Conflict(s) right now, go to your preferred text-editor, like Visual Studio Code, and in the affected files select the Accept Incoming Change link, then save:如果您现在有任何合并冲突,请转到您首选的文本编辑器,如 Visual Studio Code,并在受影响的文件中选择Accept Incoming Change链接,然后保存:

在此处输入图像描述

Then back to Sourcetree, click on the Commit button at top:然后回到 Sourcetree,点击顶部的Commit按钮:

在此处输入图像描述

then right-click on the conflicted file(s), and under Resolve Conflicts select the Mark Resolved option:然后右键单击冲突的文件,并在Resolve Conflicts下选择Mark Resolved选项:

在此处输入图像描述

STEP 4第4步

Finally!!!最后!!! We are now able to commit our file(s), also checkmark the Push changes immediately to origin option before clicking the Commit button:我们现在可以提交我们的文件,还可以在单​​击Commit按钮之前选中Push changes immediately to origin选项:

在此处输入图像描述

PS while writing this, a commit was submitted by another developer right before I got to commit, so had to pretty much repeat steps. PS 在写这篇文章的时候,另一个开发者在我提交之前提交了一个提交,所以不得不重复很多步骤。

This problem comes when you have commit some changes in your GitHub repository using https://github.com/ which are not pulled into your local repository当您使用https://github.com/在您的 GitHub 存储库中提交了一些更改,这些更改未拉入您的本地存储库时,就会出现此问题

To solve this problem -为了解决这个问题——

  1. First pull your repository from GitHub and update commit or changes in your local repository首先从 GitHub 拉取你的存储库并更新本地存储库中的提交或更改
  2. Then push your commits to GitHub repository然后将您的提交推送到 GitHub 存储库
  3. You can follow below steps您可以按照以下步骤
git pull <git repo HTTPS LINK>
git commit -m "updates and bugs fixed"
git push

Write lock on shared local repository在共享本地存储库上写锁

I had this problem and none of above advises helped me.我遇到了这个问题,以上建议都没有帮助我。 I was able to fetch everything correctly.我能够正确获取所有内容。 But push always failed.但是推送总是失败。 It was a local repository located on windows directory with several clients working with it through VMWare shared folder driver.它是一个位于 windows 目录的本地存储库,有多个客户端通过 VMWare 共享文件夹驱动程序使用它。 It appeared that one of the systems locked Git repository for writing.似乎其中一个系统锁定了 Git 存储库以进行编写。 After stopping relevant VMWare system, which caused the lock everything repaired immediately.停止相关的VMWare系统后,导致锁的一切立即修复。 It was almost impossible to figure out, which system causes the error, so I had to stop them one by one until succeeded.几乎不可能弄清楚是哪个系统导致了错误,所以我不得不一一阻止它们,直到成功。

Well I used the advice here and it screwed me as it merged my local code directly to master.好吧,我在这里使用了建议,当它直接将我的本地代码合并到 master 时,它把我搞砸了。 .... so take it all with a grain of salt. .... 所以把这一切都带上一粒盐。 My coworker said the following helped resolve the issue, needed to repoint my branch.我的同事说以下有助于解决问题,需要重新指向我的分支。

 git branch --set-upstream-to=origin/feature/my-current-branch feature/my-current-branch

In Eclipse do the following:在 Eclipse 中执行以下操作:

GIT Repositories > Remotes > Origin > Right click and say fetch GIT Repositories > Remotes > Origin > 右键单击​​并说 fetch

GIT Repositories > Remote Tracking > Select your branch and say merge GIT Repositories > Remote Tracking > 选择你的分支并说合并

Go to project, right click on your file and say Fetch from upstream.转到项目,右键单击您的文件并说从上游获取。

  1. move the code to a new branch - git branch -b tmp_branchyouwantmergedin将代码移动到新分支 - git branch -b tmp_branchyouwantmergedin
  2. change to the branch you want to merge to - git checkout mycoolbranch更改到要合并到的分支 - git checkout mycoolbranch
  3. reset the branch you want to merge to - git branch reset --hard HEAD重置要合并到的分支 - git branch reset --hard HEAD
  4. merge the tmp branch into the desired branch - git branch merge tmp_branchyouwantmergedin将 tmp 分支合并到所需的分支 - git branch merge tmp_branchyouwantmergedin
  5. push to origin推到原点

Here is another solution to resolve this issue这是解决此问题的另一种解决方案

>git pull
>git commit -m "any meaning full message"
>git push

after you get the non fast forward error , just do below :得到非快进错误后,只需执行以下操作:

1> git pull --rebase origin <name-of-the-remote-branch>

This will fetch the remote changes in to your local branch .这会将远程更改获取到您的本地分支。 On top of that it will apply your local commits .最重要的是,它将应用您的本地提交。

2> git push origin <name-of-the-remote-branch>

This will apply your local changes in the local copy of the remote branch to the actual remote branch repo in git这会将远程分支的本地副本中的本地更改应用到 git 中的实际远程分支 repo

git checkout master git结账大师

git fetch [the temporal branch of the company before pushing to master] git fetch [推送到master之前公司的时间分支]

git pull --rebase [the temporal branch of the company before pushing to master] master git pull --rebase [推送到master之前公司的临时分支] master

git checkout -b [new-branch] git checkout -b [新分支]

Then add your files and do the following:然后添加您的文件并执行以下操作:

git add .混帐添加。

git commit -m"added article" git commit -m"添加的文章"

git push -u origin [new-branch] git push -u origin [新分支]

This worked for me这对我有用

Sometimes, Git can't make your change to a remote repository without losing commits.有时,Git 无法在不丢失提交的情况下对远程存储库进行更改。 When this happens, your push is refused.发生这种情况时,您的推送将被拒绝。 If another person has pushed to the same branch as you, Git won't be able to push your changes如果其他人与您推送到同一分支,Git 将无法推送您的更改

you can fix this by:您可以通过以下方式解决此问题:

git pull origin YOUR_BRANCH_NAME

Only this worked for me:只有这对我有用:

You can fix this by fetching and merging the changes made on the remote branch with the changes that you have made locally:您可以通过获取并合并在远程分支上所做的更改与您在本地所做的更改来解决此问题:

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

Follow this for more insights关注以获得更多见解

  1. Undo the local commit.撤消本地提交。 This will just undo the commit and preserves the changes in working copy这只会撤消提交并保留工作副本中的更改
git reset --soft HEAD~1
  1. Pull the latest changes拉取最新更改
git pull
  1. Now you can commit your changes on top of latest code现在您可以在最新代码之上提交您的更改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM