github不小心同步覆蓋了本地文件

昨天不小心github的commit還沒push就同步了,導致本地文件被覆蓋,一度以爲沒救了。 後來得微博 @空非無和 @柳煙堆雪 指點,用git reflog 恢復了文件。

事情是這樣的。。。
我在兩個電腦上修改一個項目,A修改文件a,B修改文件b。然後我幹了下面這些事,按時間順序。。。
1. A上 git commit
2. A上 git push
3. B上 git commit
4. B上 git pull
然後。。。B上修改的b就被覆蓋了。。。

而且git log已經找不到第3步的commit了。


解決方案:
執行git reflog

502dd0f HEAD@{0}: pull --progress --rebase --prune origin master  
147b3b5 HEAD@{1}: commit: commit-mark
502dd0f HEAD@{2}: rebase finished: returning to refs/heads/master  

然後

git reset --hard 502dd0f
git cherry-pick 147b3b5

就可恢復原來B上的本地文件, 然後push到服務器端:

git push origin master

但出現:

 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/GitUsername/GitProgramName.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

可見,線下線上文件出現了衝突。
解決方法:

  1. git pull
  2. 如果有衝突,解決衝突
  3. git push

但是git pull出現問題:

You are not currently on a branch. Please specify which
branch you want to merge with. See git-pull(1) for details.

    git pull <remote> <branch>

解決方法:切換到master,再執行git push

git checkout -b temp
git checkout master
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章