如何將修改後的提交推送到遠程Git存儲庫?

當我使用個人源代碼工做時,我作了我慣常的事情提交,而後我推送到遠程存儲庫。 但後來我注意到我忘了在源代碼中組織個人導入。 因此我作了修改命令來替換之前的提交: 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


#1樓

我經過放棄個人本地修改提交併在頂部添加新更改來解決它: 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

#2樓

快速咆哮:這裏沒有人發佈簡單答案的事實證實了Git CLI表現出的絕望的用戶敵意。 rem

不管如何,假設你尚未試圖強行推進,那麼「明顯」的作法是先拉。 這會拉動您修改的更改(所以再也不具備),以便您再次使用它。 get

解決任何衝突後,您能夠再次推送。 hash

因此: it

git pull

若是你在pull中出錯,多是你的本地存儲庫配置有問題(我在.git / config分支部分有一個錯誤的引用)。 io

以後 ast

git push

也許你會獲得額外的提交,主題講述「瑣碎的合併」。


#3樓

我有一樣的問題。

  • 意外修改了已推送的最後一次提交
  • 在本地作了不少改變,承諾了五次
  • 試圖推,獲得一個錯誤,恐慌,合併遠程,獲得了不少不是個人文件,推,失敗等。

做爲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次),但它節省了一天的工做。


#4樓

我不得不經過從遠程倉庫中提取並解決出現,提交而後推送的合併衝突來解決這個問題。 但我以爲有更好的方法。


#5樓

簡短的回答:不要將修改後的提交推送到公共回購。

答案很長:一些Git命令,好比git commit --amendgit rebase ,實際上重寫了歷史圖。 只要你沒有公佈你的更改,這很好,可是一旦你作了,你真的不該該在歷史記錄中徘徊,由於若是有人已經獲得你的改變,那麼當他們試圖再次拉動時,它可能會失敗。 您應該只使用更改進行新的提交,而不是修改提交。

可是,若是你真的想要推送修改後的提交,你能夠這樣作:

$ git push origin +master:master

前導+符號將強制推送發生,即便它不會致使「快進」提交。 (當您推送的更改是公共存儲庫中已有更改的直接後代時,會發生快進提交。)

相關文章
相關標籤/搜索