How to merge specific files from Git branches

后端 未结 11 1882
长发绾君心
长发绾君心 2020-11-30 16:23

I have 2 git branches branch1 and branch2 and I want to merge file.py in branch2 into file.py in branch1 and only that file.

In essence I just want to work on the fi

11条回答
  •  孤街浪徒
    2020-11-30 16:56

    If you only care about the conflict resolution and not about keeping the commit history, the following method should work. Say you want to merge a.py b.py from BRANCHA into BRANCHB. First, make sure any changes in BRANCHB are either committed or stashed away, and that there are no untracked files. Then:

    git checkout BRANCHB
    git merge BRANCHA
    # 'Accept' all changes
    git add .
    # Clear staging area
    git reset HEAD -- .
    # Stash only the files you want to keep
    git stash push a.py b.py
    # Remove all other changes
    git add .
    git reset --hard
    # Now, pull the changes
    git stash pop
    

    git won't recognize that there are conflicts in a.py b.py, but the merge conflict markers are there if there were in fact conflicts. Using a third-party merge tool, such as VSCode, one will be able to resolve conflicts more comfortably.

提交回复
热议问题