删除我想保留的文件

删除我想保留的文件

本文介绍了git merge:删除我想保留的文件!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何合并git中的两个分支,从分支中保留必要的文件?



合并两个分支时,在一个分支中删除,而不是在另一个分支中,该文件最终被删除。



例如:


  • 当您创建新分支时,文件在主文件夹中存在

  • 由于我们不需要该文件,所以从主文件中删除文件

  • 在分支中进行更改以添加依赖于现有文件的功能

  • 您在master 中修复了错误, em>(不能丢弃)

  • 您有一天会合并,并且该文件不存在!

  • hr>

    如何重现:


    1. 创建一个包含一个文件的git仓库。

        git init 
      echotest> test.txt
      git add。
      git commit -m初始提交


    2. 创建分支

        git branch branchA 


    3. 删除master中的文件

        git rm test.txt 
      git commit -m已删除文件从主


    4. 在branchA中进行任何未更改的文件它必须保持不变,以避免冲突)

        git checkout branchA 
      touch something.txt
      git add 。
      git commit -m一些分支变更


    从这里,我发现合并这两个分支的任何方式都会删除test.txt文件。假设我们依靠文件 branchA ,这是个大问题。






    失败示例:

    合并1

      git checkout branchA 
    git merge master
    ls test.txt

    合并2

      git checkout master 
    git merge branchA
    ls test.txt

    Rebase 1

      git checkout branchA 
    git rebase master
    ls test.txt


    解决方案

    这是一个有趣的问题。因为您在创建 BranchA 后删除了该文件,然后将 master 合并到 BranchA ,我不确定Git如何能够意识到存在冲突。



    在错误的合并之后,您可以撤消,然后重新-merge,但是加回文件:

      git checkout HEAD @ {1}。 
    git merge --no-commit master
    git checkout master test.txt
    git add test.txt
    git commit


    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:

    • A file exists in master when you make a new branch
    • you remove the file from master since we don't need it (yet)
    • you make changes in the branch to add a feature, which relies on the file existing
    • you make bug fixes in master (cannot be discarded)
    • you merge some day, and the file is gone!

    How to Reproduce:

    1. Create a git repo with one file.

      git init
      echo "test" > test.txt
      git add .
      git commit -m "initial commit"
      

    2. Create a branch

      git branch branchA
      

    3. Delete the file in master

      git rm test.txt
      git commit -m "removed file from master"
      

    4. Make ANY changes in branchA that don't touch the deleted file (it has to be unchanged to avoid Conflict)

      git checkout branchA
      touch something.txt
      git add .
      git commit -m "some branch changes"
      

    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 branchA, this is a big problem.


    Failing examples:

    Merge 1

    git checkout branchA
    git merge master
    ls test.txt
    

    Merge 2

    git checkout master
    git merge branchA
    ls test.txt
    

    Rebase 1

    git checkout branchA
    git rebase master
    ls test.txt
    
    解决方案

    This is an interesting issue. Because you deleted the file after 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.

    After the bad merge you can undo, and then re-merge, but add back the file:

    git checkout HEAD@{1} .
    git merge --no-commit master
    git checkout master test.txt
    git add test.txt
    git commit
    

    这篇关于git merge:删除我想保留的文件!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 08:28