当我使用了一点源代码后,我做了我通常的事情提交,然后推送到远程存储库。但后来我注意到我忘记在源代码中组织导入。因此,我执行modify命令以替换先前的commit:

> git commit --amend

不幸的是,无法将提交推回到存储库。它是这样被拒绝的:

> git push origin
To //my.remote.repo.com/stuff.git/
 ! [rejected]        master -> master (non-fast forward)
error: failed to push some refs to '//my.remote.repo.com/stuff.git/'

我该怎么办?(我可以访问远程存储库。)


当前回答

快速咆哮:没有人在这里发布简单的答案,这表明了Git CLI显示出的绝望的用户敌意。

无论如何,如果你没有试图强行推动,那么“显而易见”的方法就是先拉动。这将拉动您修改的更改(因此不再具有),以便您再次拥有它。

一旦解决了任何冲突,就可以再次推送。

So:

git pull

如果在pull中出现错误,可能是本地存储库配置有问题(我在.git/config分支部分中有一个错误的ref)。

之后

git push

也许你会得到一个额外的承诺,主题讲述一个“琐碎的合并”。

其他回答

如果您使用的是Visual Studio代码,可以尝试此扩展以使其更容易。

https://marketplace.visualstudio.com/items?itemName=cimdalli.git-提交修改推力

正如您从其名称中可以理解的,它连续执行命令

gitcommit—修改git push—力

我也有同样的问题。

意外修改了已推送的最后一个提交在本地做了很多更改,提交了大约五次尝试推送,出现错误,恐慌,合并远程,得到很多不是我的文件,推送,失败等。

作为一个Git新手,我认为这是完全的FUBAR。

解决方案:@bara建议+创建一个本地备份分支

# Rewind to commit just before the pushed-and-amended one.
# Replace <hash> with the needed hash.
# --soft means: leave all the changes there, so nothing is lost.
git reset --soft <hash>

# Create new branch, just for a backup, still having all changes in it.
# The branch was feature/1234, new one - feature/1234-gone-bad
git checkout -b feature/1234-gone-bad

# Commit all the changes (all the mess) not to lose it & not to carry around
git commit -a -m "feature/1234 backup"

# Switch back to the original branch
git checkout feature/1234

# Pull the from remote (named 'origin'), thus 'repairing' our main problem
git pull origin/feature/1234

# Now you have a clean-and-non-diverged branch and a backup of the local changes.
# Check the needed files from the backup branch
git checkout feature/1234-gone-bad -- the/path/to/file.php

也许这不是一个快速而干净的解决方案,我失去了我的历史(1次提交而不是5次),但它节省了一天的工作。

我通过放弃本地修改的提交并在顶部添加新的更改来解决这个问题:

# Rewind to commit before conflicting
git reset --soft HEAD~1

# Pull the remote version
git pull

# Add the new commit on top
git add ...
git commit
git push

您可以通过以下操作以简单、安全的方式进行操作:

使用gitcommit修改您的上一次提交--修改和需要添加的任何选项git pull将本地回购与远程回购同步。拉动后,您将在本地和远程之间发生冲突。您只需接受当前更改并再次提交即可解决这些问题。数字推送

现在,本地和远程回购已更新,无需更改回购历史记录。

下面是一个非常简单和干净的方法,在您已经进行了git添加“您的文件”和git提交后推送您的更改--修改:

git push origin master -f

or:

git push origin master --force