GIT 如何從另一分支合並特定的文件


是否遇到過這種情景:
您在一個分支上工作,發現該分支上的某些文件實現的功能已經在其他分支上實現了
但因為這兩個分支實現不同的功能,因此不能進行簡單的合並工作,但您又不想重復其他已經完成的工作
以下操作可以解決該問題:
 
處理流程這樣的:
  1. 先檢驗當前分支與要合並分支通用文件的差異(要合並的分支必須要全部commit)
  2. 拉出要“合並某分支文件有差異”的所有文件(會覆蓋當前分支的文件,在提交前請手動合並差異文件) git checkout 分支名稱 多個指定的文件名 
  3. 添加並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 文件"合並其他分支的文件會覆蓋當前分支的文件,所以在提交之前必須手動合並差異
QQ截圖20150805100649.jpg
參考:
 
 
 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM