Take this scenario:
What would be the most efficient method of getting these select amount of changes, merged into my 'fork'?
You just need to fetch the list of commits from the other repository and then merge his branch into your tree. Merging should do the right thing with your commits on both branches.
This can be done by simply clicking the pull request button on the GitHub page of your fork. The owner of the original repository will then be notified of your changes and may merge them.
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:
First you have to be in your repository, do the necessary cd
-ing to get to your working directory.
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
You can now run commands such as git log someUser/master
to find the commit SHA1 you want to merge in 'partially'.
Once you have the SHA, you can now run:
git cherry-pick -n SHA1
where SHA1
is the commit SHA, duh!
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.
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
Yay! Commit time:
git commit -m "I merged a select amount of changes"
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.
I really like Tim's solution, however sometimes I like tinkering in vimdiff. My solution to this problem is crude, but it works for me because I like vim.
I have vimdiff set as my difftool, and then to selectively merge I diff the branch:
git difftool <branch> <file>
Then I go to the pane with the current branch's version and edit the original in vim (sometimes this isn't necessary, but sometimes vimdiff opens a version in /tmp) and disable read-only mode:
:e <file>
:set readonly!
Now I can use vim's patch tools such as do
and dp
to apply what I want, and make other little edits as I go. When I am done, I save the file, exit out of vim, and then stage and commit the file in git like a regular edit.
As I said, this isn't particularly sophisticated, but it is very powerful, and still purely in the command line. Just be sure to add a clear commit message, as git won't automatically include a merge message for you.
I'm not clear on what you want. If you want to bring in only certain commits from the other forks, you can use git cherry-pick SHA
. Full explanation on gitready.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With