git merge: Removing files I want to keep!

前端 未结 5 1276
别跟我提以往
别跟我提以往 2020-11-28 05:08

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 an

相关标签:
5条回答
  • 2020-11-28 05:37

    You need to modify the file in the branch, so that there's a merge conflict with the delete in the trunk.

    The exact same thing will happen if you, for example, delete a declaration for something in a headerfile in the trunk (because nothing needs it), and add a dependency on that declaration to some non-header file(s) in the branch. When you merge, since the branch doesn't touch (that part of) the header, it will just delete the declaration and things will break.

    Whenever you have stuff in multiple places that is interdependent and needs to be kept in sync, its very easy for a merge to silently introduce problems. Its just one of the things you have to know about and check when merging. Ideally, you use compile-time asserts or other build time checks that will make any failures immediately apparent.

    0 讨论(0)
  • 2020-11-28 05:41

    My solution to this was to simply modify the files I needed to keep (added a comment which was needed anyway) and commit those changes on the target branch, thus generating a merge conflict which could easily be resolved with a git add and a normal commit.

    My history went something like this. Branch names have been changed to protect the innocent.

    1. create and commit files for a new feature to master
    2. realize this addition going to be more involved than originally planned, thus, branched to feature_branch
    3. Removed files from master so as not to disrupt normal workflow with RBs and such
    4. Time passes, more commits on master, none on feature_branch
    5. Resume work on the feature, git merge master on feature_branch causes original files to be removed (of course), git reset --hard to before the merge
    6. Applied the solution described above
    0 讨论(0)
  • 2020-11-28 05:45

    Casey's example didn't work for my case - I couldn't checkout test.txt from master, because it was no longer in that branch:

    $ git checkout master test.txt
    error: pathspec 'test.txt' did not match any file(s) known to git.
    

    Happily I could pull the file out of branchA's own HEAD:

    $ git checkout branchA
    $ git merge --no-commit master
    $ git checkout HEAD test.txt
    $ git add test.txt
    $ git commit
    
    0 讨论(0)
  • 2020-11-28 05:54

    For a quick fix in this case, "git revert" the commit that deleted the file.

    When this situation comes up in the future, the better way to handle it is to ensure that the creation of the new file happens on the branch. Then it gets added on master when you merge, but you don't have the file lying around in master in the meantime.

    0 讨论(0)
  • 2020-11-28 05:56

    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
    
    0 讨论(0)
提交回复
热议问题