Using GIT, how can I selectively merge changes from one commit on another 'fork'?

后端 未结 4 1141
傲寒
傲寒 2020-12-07 08:08

Take this scenario:

  1. I decide to \'fork\' a codebase on github.com, and start doing my routine: Edit - Commit - Push; aka hack hack hack.
  2. After I made
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-07 08:08

    After trolling the waves of the IRC world, someone gave a great solution:

    git cherry-pick SHA1 --no-commit
    git add --patch
    

    Hopefully that helps anyone else with the same question!

    EDIT: OK, maybe it isn't quite that simple. Here are the full steps:

    1. First you have to be in your repository, do the necessary cd-ing to get to your working directory.

    2. You now need to add the remote branch, and then fetch it. Do this by:

      git remote add someUser git://github.com/someUser/someRepo.git

      git fetch someUser

    3. You can now run commands such as git log someUser/master to find the commit SHA1 you want to merge in 'partially'.

    4. Once you have the SHA, you can now run:

      git cherry-pick -n SHA1

      where SHA1 is the commit SHA, duh!

    5. There quite possibly will be conflicts, depending how old the commit is, and how often that particular area of the project changes. Sorry, but you have to manually resolve these with your trusty editor. So pull out VIM, or whatever else you use, and hack away at the conflicts until you get to the stage where you like the changes.

    6. You now have to reset the index to the HEAD revision, then you can then use the trusty GIT add --patch command to pick and choose what changes you want:

      git reset HEAD

      git add --patch or git add -p

    7. Yay! Commit time:

      git commit -m "I merged a select amount of changes"

    8. To clean up the mess (The stuff you said no to in git add --patch) and only keep the selected changes in your working repository, run:

      git reset --hard HEAD

      Apparently git checkout -f is another option as well.

提交回复
热议问题