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

前端 未结 25 1616
慢半拍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条回答
  •  旧时难觅i
    2020-11-22 03:26

    1800 INFORMATION's answer is completely correct. As someone new to Git, though, "use git cherry-pick" wasn't enough for me to figure this out without a bit more digging on the Internet, so I thought I'd post a more detailed guide in case anyone else is in a similar boat.

    My use case was wanting to selectively pull changes from someone else's GitHub branch into my own. If you already have a local branch with the changes, you only need to do steps 2 and 5-7.

    1. Create (if not created) a local branch with the changes you want to bring in.

      $ git branch mybranch

    2. Switch into it.

      $ git checkout mybranch

    3. Pull down the changes you want from the other person's account. If you haven't already, you'll want to add them as a remote.

      $ git remote add repos-w-changes

    4. Pull down everything from their branch.

      $ git pull repos-w-changes branch-i-want

    5. View the commit logs to see which changes you want:

      $ git log

    6. Switch back to the branch you want to pull the changes into.

      $ git checkout originalbranch

    7. Cherry pick your commits, one by one, with the hashes.

      $ git cherry-pick -x hash-of-commit

提交回复
热议问题