一:git命令在提交代碼前,沒有pull拉最新的代碼,因此再次提交出現了沖突。
error: You have not concluded your merge (MERGE_HEAD exists).
hint: Please, commit your changes before merging.
fatal: Exiting because of unfinished merge.
解決方法如下兩種:
1.保留你本地的修改
git merge --abort
git reset --merge
合並后記得一定要提交這個本地的合並(add-->commit-->push-->pull)
然后在獲取線上倉庫
git pull
2.down下線上代碼版本,拋棄本地的修改
不建議這樣做,但是如果你本地修改不大,或者自己有一份備份留存,可以直接用線上最新版本覆蓋到本地
git fetch --all
git reset --hard origin/master
git fetch
二:從git遠程倉庫中pull最新的代碼,出現如下錯誤:Please commit your changes or stash them before you merge.
解決方法如下:(git stash 可用來暫存當前正在進行的工作, 比如想pull 最新代碼, 又不想加新commit, 或者另外一種情況,為了fix 一個緊急的bug, 先stash, 使返回到自己上一個commit, 改完bug之后再stash pop, 繼續原來的工作。)
1: git stash //暫存代碼
2: git pull 分支名//從遠程倉庫拉取最新代碼
3: git stash pop //合並代碼到本地倉庫 此時代碼是將暫存的代碼和遠程倉庫的代碼合並,如下圖:
4:這時候需要手動修改合並所需的代碼即可。
5:git stash clear//需要清空git棧執行該命令
git stash: 備份當前的工作區的內容,從最近的一次提交中讀取相關內容,讓工作區保證和上次提交的內容一致。同時,將當前的工作區內容保存到Git棧中。
git stash pop: 從Git棧中讀取最近一次保存的內容,恢復工作區的相關內容。由於可能存在多個Stash的內容,所以用棧來管理,pop會從最近的一個stash中讀取內容並恢復。
git stash list: 顯示Git棧內的所有備份,可以利用這個列表來決定從那個地方恢復。
git stash clear: 清空Git棧。此時使用gitg等圖形化工具會發現,原來stash的哪些節點都消失了
三:git push 報錯,如下:


再次執行push命令:如下圖所示:
四:git push 還會報下面的錯(如圖所示):這多是多人開發有了沖突。
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first 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.
解決命令如下:
git push -u 代碼所在的分支 -f //強制提交,此時遠程上的修改已經被覆蓋。這種方法一般不建議使用,除非你把遠程上修改的代碼復制到本地。
五:本地回退歷史版本,當提交代碼發生沖突或者想回退到某一個版本,操作如下:
1:git log (獲取提交的歷史日志)
2: 執行命令:git reset --hard 版本號(就是git log 中的 commit后面的哈希值(上圖中的黃色部分 commit 后面的值))
3:想要修改遠程上的代碼還需要執行如下命令:
git push -u 代碼所在的分支 -f //強制提交,此時遠程上的修改已經被覆蓋。這種方法一般不建議使用,除非你把遠程上修改的代碼復制到本地。