当我使用个人源代码工做时,我作了我惯常的事情提交,而后我推送到远程存储库。 但后来我注意到我忘了在源代码中组织个人导入。 因此我作了修改命令来替换之前的提交: php
> git commit --amend
不幸的是,提交不能被推回到存储库。 这被拒绝了: git
> 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/'
我该怎么办? (我能够访问远程存储库。) spa
我经过放弃个人本地修改提交并在顶部添加新更改来解决它: code
# 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
快速咆哮:这里没有人发布简单答案的事实证实了Git CLI表现出的绝望的用户敌意。 rem
不管如何,假设你尚未试图强行推进,那么“明显”的作法是先拉。 这会拉动您修改的更改(所以再也不具备),以便您再次使用它。 get
解决任何冲突后,您能够再次推送。 hash
因此: it
git pull
若是你在pull中出错,多是你的本地存储库配置有问题(我在.git / config分支部分有一个错误的引用)。 io
以后 ast
git push
也许你会获得额外的提交,主题讲述“琐碎的合并”。
我有一样的问题。
做为Git-newbie,我认为这是完整的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命令,好比git commit --amend
和git rebase
,实际上重写了历史图。 只要你没有公布你的更改,这很好,可是一旦你作了,你真的不该该在历史记录中徘徊,由于若是有人已经获得你的改变,那么当他们试图再次拉动时,它可能会失败。 您应该只使用更改进行新的提交,而不是修改提交。
可是,若是你真的想要推送修改后的提交,你能够这样作:
$ git push origin +master:master
前导+
符号将强制推送发生,即便它不会致使“快进”提交。 (当您推送的更改是公共存储库中已有更改的直接后代时,会发生快进提交。)