1. 程式人生 > >[Git]執行git stash pop時的衝突解決

[Git]執行git stash pop時的衝突解決

git stash pop時的衝突解決

今天遇到一個問題,本來在主分支上checkout了一條新分支出來工作,可是做一半的時候突然發現後續要做的東西依賴於另一條特性分支裡面的程式碼。特性分支其實是已經推送到遠端並且提交合並請求了,但是並沒有及時合入。所以跟老大溝通了一下,先把遠端的特性分支合入主分支,我本地再rebase一下最新的主分支。可是因為功能才完成一半,並不屬於一個完整的commit,所以我先把程式碼用git stash存了起來。本地rebase完執行git stash pop的時候就出現了衝突。因為以前從來沒有遇到過這種情況,所以一時不知道怎麼處理比較好。網上查了資料,發現上面的做法不是很好,所以自己嘗試著把問題處理了。

幹講命令的話估計不好理解,下面我通過一個例子來演示一下整個衝突出現和解決的過程。由於只是展示怎麼解決衝突,不涉及具體生產時的工作流程,所以就直接在本地進行了。本文為了儘可能地把問題講清楚,手動復現了衝突,所以冗餘的內容較多,可能對於Git老手來說顯得有點囉嗦,如果想直接看結論,請直接跳轉到第六步

第一步,我們先做好準備工作,初始化一個版本倉庫,新建一個檔案test並且提交,作為master分支上的根commit:

$ git init

$ touch test

$ git add test

$ git commit -m "init test"
[master (root-commit) c1f4cda] init test
 1
file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test $ git log commit c1f4cdac5e3ede10a995c42b9c1d5b490b0d09a2 (HEAD -> master) Author: ganziqim <[email protected]> Date: Mon Nov 13 14:14:01 2017 +0800 init test

第二步,切換到另一條分支上工作,新建另一個檔案anothertest並提交commit,代表在新分支上的某個工作階段的成果:

$ git checkout -b another
Switched to a new branch 'another'

$ touch anothertest

$ git add anothertest

$ git commit -m "init anothertest"
[another 66966d5] init anothertest
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 anothertest

$ git log
commit 66966d50a3128771bf70c06ab2b6bd05ef748337 (HEAD -> another)
Author: ganziqim <[email protected]>
Date:   Mon Nov 13 14:14:56 2017 +0800

    init anothertest

commit c1f4cdac5e3ede10a995c42b9c1d5b490b0d09a2 (master)
Author: ganziqim <[email protected]>
Date:   Mon Nov 13 14:14:01 2017 +0800

    init test

第三步,在another分支上修改test和anothertest檔案的內容,使用git stash進行暫存,代表工作一半的成果,無法作為一個完整的commit進行提交:

$ vim test

$ vim anothertest

$ git stash
Saved working directory and index state WIP on another: 66966d5 init anothertest

$ git stash list
[email protected]{0}: WIP on another: 66966d5 init anothertest

第四步,回到master分支,修改test檔案並提交commit,模擬master分支出現了another分支需要使用的程式碼。這個時候分支樹出現了分叉,master分支和another分支各自往前進行了一個commit:

$ git checkout master
Switched to branch 'master'

$ vim test

$ git add test

$ git commit -m "changes on branch master"
[master 9bc290f] changes on branch master
 1 file changed, 1 insertion(+)

$ git log
commit 9bc290f5b6cea5757e01af64ea8c2591c9debdf9 (HEAD -> master)
Author: ganziqim <[email protected]>
Date:   Mon Nov 13 14:18:11 2017 +0800

    changes on branch master

commit c1f4cdac5e3ede10a995c42b9c1d5b490b0d09a2
Author: ganziqim <[email protected]>
Date:   Mon Nov 13 14:14:01 2017 +0800

    init test

第五步,再回到another分支,使用git rebase進行變基,模擬another分支取得master分支上的最新程式碼,這時分支樹又變成了一條直線:

$ git checkout another
Switched to branch 'another'

$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: init anothertest

$ git log
commit 0687e2a2f0d243a725de6093d9f42de5ab02acfd (HEAD -> another)
Author: ganziqim <[email protected]>
Date:   Mon Nov 13 14:14:56 2017 +0800

    init anothertest

commit 9bc290f5b6cea5757e01af64ea8c2591c9debdf9 (master)
Author: ganziqim <[email protected]>
Date:   Mon Nov 13 14:18:11 2017 +0800

    changes on branch master

commit c1f4cdac5e3ede10a995c42b9c1d5b490b0d09a2
Author: ganziqim <[email protected]>
Date:   Mon Nov 13 14:14:01 2017 +0800

    init test

第六步,事情進展到目前來看,都非常順利,特性分支成功地取得了主分支上需要用到的程式碼。但是當我們執行git stash pop想取出之前工作一半的成果之後,卻出現了衝突,其原因是主分支上的最新程式碼和stash暫存的程式碼對同一個檔案都進行了修改。

$ git stash pop
Auto-merging test
CONFLICT (content): Merge conflict in test

$ git status
On branch another
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   anothertest

Unmerged paths:
  (use "git reset HEAD <file>..." to unstage)
  (use "git add <file>..." to mark resolution)

        both modified:   test

git status檢視狀態的時候,可以看到Git是有標出衝突的檔案的,是不是很熟悉呢?對,這個時候先按照解決普通的pull conflict的方式修改檔案,然後執行git add。如果完全接受主分支的修改,那麼再次檢視git status的時候這個檔案應該不會再出現在狀態裡了,但這裡我們模擬的是衝突兩邊的修改我們都想保留的情況。這時候代表你個人已經接受當前的衝突解決了,Git會把修改完的結果包括其它沒有出現衝突的檔案放入暫存區。

$ vim test

$ git add test

$ git status
On branch another
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   anothertest
        modified:   test

其實事情進行到上面那一步已經算作解決了,可是有時候你並不想把這些檔案中的某一個作為下個commit的內容提交到遠端,所以此時再執行一次git reset HEAD,就恢復git stash pop後該有的狀態了。

$ git reset HEAD
Unstaged changes after reset:
M       anothertest
M       test

$ git status
On branch another
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   anothertest
        modified:   test

no changes added to commit (use "git add" and/or "git commit -a")

需要注意的是,衝突解決之後,Git並不會刪除之前的stash記錄,可以使用git stash drop將沒用的記錄刪除掉。

$ git stash list
[email protected]{0}: WIP on another: 66966d5 init anothertest

$ git stash drop [email protected]{0}
Dropped [email protected]{0} (113018d1c2be77ffae51afc15944d8620619ef7d)

$ git stash list

總結

Git屬於越用越順手的東西,並且有很多實用的騷操作技巧,所以建議大家平時多多練習,沒事多搗鼓搗鼓,熟能生巧嘛!而且我是推薦直接使用命令列的,GUI工具雖然方便,但有時候處理問題的速度跟不上思維,而使用命令列更順手,尤其是專注工作,腦子快速運轉的時候。相信你會愛上那種感覺的!

相關推薦

[Git]執行git stash pop衝突解決

git stash pop時的衝突解決 今天遇到一個問題,本來在主分支上checkout了一條新分支出來工作,可是做一半的時候突然發現後續要做的東西依賴於另一條特性分支裡面的程式碼。特性分支其實是已經推送到遠端並且提交合並請求了,但是並沒有及時合入。所以跟老大

git pull拉取程式碼衝突解決辦法

在使用git pull命令拉取程式碼時,有時會遇到以下錯誤資訊: error: Your local changes to the following files would be overwritten by merge: ... Please commit your cha

在windows上執行python的指令碼錯誤解決

在windows的cmd中進入python模式後,執行一個已經寫完的python指令碼時,出現如下錯誤 >>> python test.py   File "<stdin>", line 1     python test.py      

git stash pop衝突解決

導致原因:本地修改檔案a.file,同時別人提交程式碼中也修改了檔案a.file;這時你想提交程式碼時就會遇到這個問題。 提交程式碼之前首先需要更新原生代碼到最新版本,此之前需先儲存本地修改; 命令如下: $ git stash $ git pull 然後將本地儲存的修改,

mac執行 git 出現xcrun: error: invalid active developer path解決方法

mac執行git命令時候出現: xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/Comman

git stash pop引發的unmerged 衝突問題

今天在進行一個git相關的操作時,遇到了一個合併衝突的問題,情景如下 1. 進行git stash save操作,將更改了的README.md存入stash 2. 更改README.md的內容,並進行正常的git commit README.md -m 'blabla' 3. 先進

git合併分支如何解決衝突

合併時有衝突  $ git merge --no-ff modifyGR error: Merging is not possible because you have unmerged files. hint: Fix them up in the work tree,

Git建立和合並分支、merge分支衝突解決、rebase分支衝突解決、臨時修改 (stash)

分支的概念: 在Git中每一次的提交,Git都會把它們串成一條時間線,這條時間線就是一個分支。 在Git裡,如果我們沒有對分支進行其他操作,預設會建立一個主分支master,我們的提交都在這個master分支上。 HEAD指標指向分支名,分支名指向提交。預設情況下,HE

GIT : 記錄IntelliJ IDEA 合併衝突的一個bug(衝突解決後代碼和本地倉庫一樣導致merge失敗)

IntelliJ IDEA版本 IntelliJ IDEA 2017.1.4 x64 1 問題描述 我們在用git開發是經常遇到衝突的情況,一般發生在協同開發時,一個檔案被兩個人同時改掉了,這是我們在pull程式碼時要解決衝突,並重新add然後commit最後push.

IDEA 使用 git ,pull產生衝突解決

從倉庫中拉專案到本地修改了程式碼後,提交。 commit是提交到本地,push 是推送到遠端倉庫中pull拉最新的程式碼拉新的分支,建立新的分支合併分支多個開發人員可能會對同一檔案同一地方的程式碼進行修

GIT : 記錄IntelliJ IDEA 合併衝突的一個bug(衝突解決後代碼和本地倉庫一樣導致merge失敗)

目錄 IntelliJ IDEA版本 IntelliJ IDEA 2017.1.4 x64 問題描述 我們在用git開發是經常遇到衝突的情況,一般發生在協同開發時,一個檔案被兩個人同時改掉了,這是我們在pull程式碼時要解決衝突,並重新

執行Git命令出現各種 SSL certificate problem 的解決辦法

比如我在windows下用git clone gitURL 就提示  SSL certificate problem: self signed certificate 這種問題,在windows下出現得頻率高些。我估計主要是git本身就是基於linux開發的,在windo

解決git bash中執行“ssh-add pathName”出現“Could not open a connection to your authentication agent.”

  最近在接觸laravel,開始用了git,安裝了windows for git,往github執行push操作時驗證不通過,原

git 命令 git stashgit stash pop

今天 merge 報錯 版本 pull 一個 一次 後來 更新 今天好心累 居然把我寫的東西都沒了 本地和遠程的版本差別太大 於是我想更新一下 更新的時候報錯了 於是我按照老師的來 之前忘記了他怎麽操作的 只記得有 git stash -> git merge -&

git merge git pull時候遇到沖突解決辦法git stash

drop 處理 con for -c content 進行 git pull 技術分享 在使用git pull代碼時,經常會碰到有沖突的情況,提示如下信息: error: Your local changes to ‘c/environ.c‘ would be overwr

執行git命令出現fatal: 'origin' does not appear to be a git repository錯誤

遠程 from pos pull reader could not span style fat 在執行git pull origin master時出現:   fatal: ‘origin‘ does not appear to be a git repository  

git分支與版本管理、版本回退、衝突解決記錄 - 海北天藍 - 部落格園

一.基礎使用 1.初始化本地倉庫 git init 2.關聯遠端倉庫 git remote add origin [email protected]:使用者名稱/倉庫名.git 3.新增遠端倉庫檔案到本地 git pull origin master   本地自動建

git pull 之後merging衝突解決

一、出現merging衝突的原因:git遠端上存在一個本地不存在的git 分支,就是本地遠端程式碼不同步 二、解決方式: 方法一: git pull 出現衝突後可以暫存本地修改git stash ,然後git pull 更新程式碼,git stash list 可檢視暫存記錄列表,釋放本地暫存 

git分支的合併和衝突解決

原文:   http://gitbook.liuhui998.com/3_3.htmlhttp://gitbook.liuhui998.com/5_3.html 一、如何分支的合併 在git中,可以使用git merge 和git rebase兩個命令來進行

mzy git學習,分支衝突,以及衝突解決(五)

衝突解決: 先嚐試製造衝突: 首先我:git checkout -b mzy 建立一個mzy的分支 然後在其中修改readme.txt檔案,隨便加上一點東西。 vim readme.txt   write... wq 然後: git add r