iT邦幫忙

2017 iT 邦幫忙鐵人賽
DAY 27
1
自我挑戰組

Git學習筆記系列 第 28

Pull後被覆蓋?

現在有個 remote repository 為 testRep

有兩個 RD 分別 從上面的 testRep clone 下來到自己的工作區內

RD name local repository
momo testRep1
kuro testRep2

testRep1及testRep2的分支線圖如下

Imgur

兩個RD同時開始開發不同的功能

這時候kuro做完了他的功能func001,將此功能commit並且Push到remote上了

kuro 異動了原本的 test1.txt

新增了新增了 test2.txt

因為沒人比kuro先commit及Push,因此很順利的就完成了整個作業流程

kuro的testRep2分支線圖如下

這時候remote repository的分支也是長這樣

Imgur

kuro做完了func001並且Push後

通知momo說他完成了

此時momo的func002正寫到一半

momo寫一半的資料包含

momo 異動了原本的 test1.txt

新增了新增了 test3.txt

聽了kuro寫完func001後就想說把remote的資料Pull下來合併

這時候momo 先將寫一半的資料利用git add暫存到staged area

此時的testRep1分支線圖如下

Imgur

接著

momo下了pull把remote的資料pull回來

​$ git pull
Updating 0229011..f454e57
error: Your local changes to the following files would be overwritten by merge:
	test.txt
Please, commit your changes or stash them before you can merge.
Aborting

這時候會發現,git提示說你有local change還沒commit或stash起來,可能會被pull作業給覆蓋掉

就沒繼續執行pull作業了

有兩種做法就是commit或stash

commit 提交

把現有的異動加到staged area 並且 commit

這個解法可以讓momo pull 下來remote的資料

不過不建議這麼做 因為會多一個做一半的commit

會對之後追蹤版本多增加一點困擾

commit一多 眼睛都花了

stash 封存

比較建議封存這個做法

我們可以先把改一半的異動封存到stash 1後

​$ git add .     

​$ git stash save 'stash 1'
Saved working directory and index state On master: stash 1
HEAD is now at 0229011 merge form iss1 commit       

查詢目前有哪些stash

$ git stash list 
stash@{0}: On master: stash 1

再將remote的資料pull下來

​$ git pull
Updating 0229011..f454e57
Fast-forward
 test.txt  | 5 ++++-
 test2.txt | 1 +
 2 files changed, 5 insertions(+), 1 deletion(-)
 create mode 100644 test2.txt

之後再將stash的資料apply或pop回來

​$ git stash pop stash@{0}
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt

momo繼續開發

開發完成就可以commit後push了

會操作這段主要是因為有同事會發生code在還沒commit前,pull之後發現自己寫的進度不見了

但是自己驗證卻發現不論是下指令或是利用source tree這套工具

在本地端的異動還沒commit或stash的情況下,是無法完成pull作業的

所以到底哪種情況會導致開發進度被覆蓋呢?


上一篇
git gc
下一篇
subtree
系列文
Git學習筆記31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
twdreamer
iT邦新手 5 級 ‧ 2018-11-02 23:45:30

謝謝! 解決了小新手合併分支的問題

我要留言

立即登入留言