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

前端 未结 25 1613
慢半拍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:13

    I wrote my own script called 'pmerge' to partially merge directories. It's a work in progress and I'm still learning both Git and Bash scripting.

    This command uses git merge --no-commit and then unapplies changes that don't match the path provided.

    Usage: git pmerge branch path
    Example: git merge develop src/

    I haven't tested it extensively. The working directory should be free of any uncommitted changes and untracked files.

    #!/bin/bash
    
    E_BADARGS=65
    
    if [ $# -ne 2 ]
    then
        echo "Usage: `basename $0` branch path"
        exit $E_BADARGS
    fi
    
    git merge $1 --no-commit
    IFS=$'\n'
    
    # List of changes due to merge | replace nulls with newlines | strip lines to just filenames | ensure lines are unique
    for f in $(git status --porcelain -z -uno | tr '\000' '\n' | sed -e 's/^[[:graph:]][[:space:]]\{1,\}//' | uniq); do
        [[ $f == $2* ]] && continue
        if git reset $f >/dev/null 2>&1; then
            # Reset failed... file was previously unversioned
            echo Deleting $f
            rm $f
        else
            echo Reverting $f
            git checkout -- $f >/dev/null 2>&1
        fi
    done
    unset IFS
    

提交回复
热议问题