How can I selectively merge or pick changes from another branch in Git?

前端 未结 25 1934
慢半拍i
慢半拍i 2020-11-22 02:53

I\'m using Git on a new project that has two parallel -- but currently experimental -- development branches:

  • master: import of existing codebase pl
25条回答
  •  离开以前
    2020-11-22 03:14

    The simple way, to actually merge specific files from two branches, not just replace specific files with ones from another branch.

    Step one: Diff the branches

    git diff branch_b > my_patch_file.patch

    Creates a patch file of the difference between the current branch and branch_b

    Step two: Apply the patch on files matching a pattern

    git apply -p1 --include=pattern/matching/the/path/to/file/or/folder my_patch_file.patch

    Useful notes on the options

    You can use * as a wildcard in the include pattern.

    Slashes don't need to be escaped.

    Also, you could use --exclude instead and apply it to everything except the files matching the pattern, or reverse the patch with -R

    The -p1 option is a holdover from the *Unix patch command and the fact that the patch file's contents prepend each file name with a/ or b/ (or more depending on how the patch file was generated) which you need to strip, so that it can figure out the real file to the path to the file the patch needs to be applied to.

    Check out the man page for git-apply for more options.

    Step three: there is no step three

    Obviously you'd want to commit your changes, but who's to say you don't have some other related tweaks you want to do before making your commit.

提交回复
热议问题