问题描述
如何合并git中的两个分支,从分支中保留必要的文件?
合并两个分支时,在一个分支中删除,而不是在另一个分支中,该文件最终被删除。
例如: 如何重现: 创建一个包含一个文件的git仓库。 创建分支 删除master中的文件 在branchA中进行任何未更改的文件它必须保持不变,以避免冲突) 从这里,我发现合并这两个分支的任何方式都会删除test.txt文件。假设我们依靠文件为 失败示例: 合并1 合并2 Rebase 1 这是一个有趣的问题。因为您在创建 在错误的合并之后,您可以撤消,然后重新-merge,但是加回文件: How can you merge two branches in git, retaining necessary files from a branch? When merging two branches, if a file was deleted in one branch and not in another, the file is ultimately deleted. For example: How to Reproduce: Create a git repo with one file. Create a branch Delete the file in master Make ANY changes in branchA that don't touch the deleted file (it has to be unchanged to avoid Conflict) From here, any way I've found to merge these two branches, the test.txt file is deleted. Assuming we were relying on the file for Failing examples: Merge 1 Merge 2 Rebase 1 This is an interesting issue. Because you deleted the file after After the bad merge you can undo, and then re-merge, but add back the file: 这篇关于git merge:删除我想保留的文件!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
hr>
git init
echotest> test.txt
git add。
git commit -m初始提交
git branch branchA
git rm test.txt
git commit -m已删除文件从主
git checkout branchA
touch something.txt
git add 。
git commit -m一些分支变更
branchA
,这是个大问题。
git checkout branchA
git merge master
ls test.txt
git checkout master
git merge branchA
ls test.txt
git checkout branchA
git rebase master
ls test.txt
BranchA
后删除了该文件,然后将 master
合并到 BranchA
,我不确定Git如何能够意识到存在冲突。
git checkout HEAD @ {1}。
git merge --no-commit master
git checkout master test.txt
git add test.txt
git commit
git init
echo "test" > test.txt
git add .
git commit -m "initial commit"
git branch branchA
git rm test.txt
git commit -m "removed file from master"
git checkout branchA
touch something.txt
git add .
git commit -m "some branch changes"
branchA
, this is a big problem.git checkout branchA
git merge master
ls test.txt
git checkout master
git merge branchA
ls test.txt
git checkout branchA
git rebase master
ls test.txt
BranchA
was created, and then are merging master
into BranchA
, I'm not sure how Git would be able to realize there is a conflict.git checkout HEAD@{1} .
git merge --no-commit master
git checkout master test.txt
git add test.txt
git commit