当我使用了一点源代码后,我做了我通常的事情提交,然后推送到远程存储库。但后来我注意到我忘记在源代码中组织导入。因此,我执行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新手,我认为这是完全的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次),但它节省了一天的工作。
事实上,我曾经用武力和.git存储库推过一次,结果被Linus BIG TIME骂了一顿。一般来说,这会给其他人带来很多问题。一个简单的答案是“不要这样做”。
我看到其他人给出了这样做的方法,所以我在这里不再重复。但这里有一个提示,在您使用--force(或+master)推出修改后的提交后,可以从这种情况中恢复过来。
使用gitreflog查找您修改的旧提交(称之为旧提交,我们将调用您通过修改新提交创建的新提交)。在新旧之间创建一个合并,记录新的树,比如git checkout new&&git merge-s our old。使用gitmergemaster将其合并到您的master用git push更新你的主人。头部:主将结果推出来。
然后,那些不幸地将他们的工作建立在你通过修改和强制推动而消除的承诺之上的人将看到由此产生的合并,他们将看到你喜欢新的而不是旧的。他们后来的合并将不会看到由于您的修改而导致的新旧冲突,因此他们不必遭受损失。