GIT 如何從另一分支合併特定的檔案

2021-09-08 11:13:29 字數 3241 閱讀 7502

是否遇到過這種情景:

您在乙個分支上工作,發現該分支上的某些檔案實現的功能已經在其他分支上實現了

但因為這兩個分支實現不同的功能,因此不能進行簡單的合併工作,但您又不想重複其他已經完成的工作

以下操作可以解決該問題:

處理流程這樣的:

先檢驗當前分支與要合併分支通用檔案的差異(要合併的分支必須要全部commit)

拉出要「合併某分支檔案有差異」的所有檔案(會覆蓋當前分支的檔案,在提交前請手動合併差異檔案) git checkout 分支名稱 多個指定的檔名 

新增並commit到當前分支 git commit -a -m '注釋 合併其他分支的某些檔案 和合併分支提交時的說明資訊'

f:\test>git init #初始化

initialized empty git repository in f:/test/.git/

f:\test>git add . #新增檔案

f:\test>git commit -m 'init' #提交

[master (root-commit) 0b9520a] 'init'

2 files changed, 4 insertions(+)

create mode 100644 dev.txt

create mode 100644 test.txt

f:\test>git checkout -b dev --新建並切換分支

switched to a new branch 'dev'

f:\test>git diff #更改檔案,比較檔案差異

warning: terminal is not fully functional

diff --git a/dev.txt b/dev.txt

index d62bb90..a21f2e9 100644

--- a/dev.txt

+++ b/dev.txt

@@ -1,2 +1,5 @@

init

-dev-edit

\ no newline at end of file

+dev-edit++

+dev-edit-2015-08-05

\ no newline at end of file

f:\test>git commit -a -m 'edit-dev' #提交

[dev 9f224fd] 'edit-dev'

1 file changed, 4 insertions(+), 1 deletion(-)

f:\test>git checkout master #切換到主分支

switched to branch 'master'

f:\test>git diff #更改檔案,並對比差異

warning: terminal is not fully functional

diff --git a/dev.txt b/dev.txt

index d62bb90..719fd72 100644

--- a/dev.txt

+++ b/dev.txt

@@ -1,2 +1,4 @@

init

-dev-edit

\ no newline at end of file

+dev-edit

++edt-master-2018-08-05

\ no newline at end of file

f:\test>git commit -a -m 'edit-master-dev' #提交並儲存已更改的檔案

[master dbeec1c] 'edit-master-dev'

1 file changed, 3 insertions(+), 1 deletion(-)

f:\test>git status

on branch master

nothing to commit, working directory clean

f:\test>git checkout dev dev.txt #在master中合併 並覆蓋dev分支中的dev.txt檔案

f:\test>git status #當前master分支狀態,因為dev.txt是直接從dev分支直接覆蓋而來,所以可能需要手動合併衝突

on branch master

changes to be committed:

(use "git reset head

<

file

>

..." to unstage)

modified: dev.txt

f:\test>git diff

warning: terminal is not fully functional

- (press return)

f:\test>

f:\test>git diff dev.txt #檢視dev.txt跟當前分支的差異,因為已經提交到暫存區,所以這裡沒有顯示出差異

warning: terminal is not fully functional

- (press return)

f:\test>git status

on branch master

changes to be committed:

(use "git reset head

<

file

>

..." to unstage)

modified: dev.txt

f:\test>git citool #使用gui檢視差異

使用"checkout 檔案"合併其他分支的檔案會覆蓋當前分支的檔案,所以在提交之前必須手動合併差異

參考:

高富帥們的git技巧-從另一分支獲取檔案內容而不用切換分支

how to "merge" specific files from another branch

get a file from a specific revision

git如何檢視某乙個檔案的詳細提交記錄

git檢視某個檔案的修改歷史

git學習教程(七) git差異比對

來自為知筆記(wiz)

git將某分支的某次提交合併到另一分支

git將某分支的某次提交合併到另一分支 假設已更改的是develop分支,另乙個分支是master 1 切換develop分支 下邊是切換分支的方法 1.檢視所有分支 git branch a 2.檢視當前使用分支 結果列表中前面標 號的表示當前使用分支 git branch 3.切換分支 git ...

git 六 分支的建立與合併

在版本回填退里,已經知道,每次提交,git都把它們串成一條時間線,這條時間線就是乙個分支。截止到目前,只有一條時間線,在git裡,這個分支叫主分支,即master分支。head嚴格來說不是指向提交,而是指向master,master才是指向提交的,所以,head指向的就是當前分支。首先,我們來建立d...

Git 六 分支管理策略 如何禁用快速合併

一 分支管理策略 通常,合併分支時,如果可以那就使用快速合併 fast forward 但是有一些分支在合併的時候儘管不能產生分支衝突但是也不能使用快速合併。這個時候會在合併之後做一次新的提交,但是該模式下,刪除分支,會丟掉部分資訊。1.例項 不能使用快速合併同時也不會產生分支衝突的情況 1.建立並...