溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Git沖突中g(shù)it checkout高級(jí)用法是什么

發(fā)布時(shí)間:2021-12-06 16:49:41 來源:億速云 閱讀:341 作者:柒染 欄目:大數(shù)據(jù)

Git沖突中g(shù)it checkout高級(jí)用法是什么,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

 

背景

Git沖突的原因,一般是修改了同一個(gè)文件導(dǎo)致的,這個(gè)文件有可能是別人提交到遠(yuǎn)程倉庫里面,還有就是需要合并這個(gè)文件導(dǎo)致的。

 

解決方法

  1. 你確定你需要的是哪個(gè)倉庫的文件

git checkout --theirs conflicted_file.txt  # 保留遠(yuǎn)端的
git checkout --ours conflicted_file.txt # 保留本地的
 
  1. 然后執(zhí)行add和commit

git add -A
git commit -m "update conflict
   

舉個(gè)栗子

獲取遠(yuǎn)端服務(wù)器上的文件,提示沖突了需要合并

# git cherry-pick FETCH_HEAD
* branch            refs/changes/85/12385/3 -> FETCH_HEAD
error: 'cherry-pick' is not possible because you have unmerged files.
hint: Fix them up in the work tree,
hint: and then use 'git add/rm <file>' as
hint: appropriate to mark resolution and make a commit,
hint: or use 'git commit -a'.
fatal: cherry-pick failed
 

查看當(dāng)前倉庫的狀態(tài)

# git status
Not currently on any branch.
You are currently cherry-picking commit 53e5374.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
     both modified: file1.txt
 

用提示的命令執(zhí)行

# git cherry-pick --continue

U   file1.txt

error: 'commit' is not possible because you have unmerged files.
hint: Fix them up in the work tree,
hint: and then use 'git add/rm <file>' as
hint: appropriate to mark resolution and make a commit,
hint: or use 'git commit -a'.
fatal: Exiting because of an unresolved conflict.
 

提示file1.txt有更新,確定替換為遠(yuǎn)程倉庫的文件。這里用theirs

git checkout --theirs file1.txt
 

然后添加到本地倉庫

git add -A
 

最后繼續(xù)cherry-pick「復(fù)制」

# git cherry-pick --continue
[detached HEAD 8f26ce8] Summary : test git checkout --theirs
 Author: Rik
 2 files changed, 0 insertions(+), 0 deletions(-)
   

小結(jié)

git ckeckout 和 帶參數(shù)的--ours和 --theirs還是有區(qū)別的。

    

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

向AI問一下細(xì)節(jié)
推薦閱讀:
  1. git命令
  2. git沖突怎么辦

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

git
AI