How do I merge locally a master and a fork in git?

2019-08-09 00:40发布

问题:

I need to use Active_admin with Formtastic 2 and the main branch doesn't support it yet.

A couple of weeks ago someone made a fork to support Formtastic 2 But then other additions were added to the master branch and were not commited to the fork. And now the fork is outdated with other things, but yet it support Formtastic.

How can I merge both of them locally in my computer using git?

回答1:

This simplest way is to switch to your local formtastic branch, then run git merge master to merge the master branch changes in (you may have to deal with conflicts after that):

git branch formtastic
git merge master

If you want your history to be a little more structured, you can rebase instead:

git branch formtastic
git rebase -i master

Rebasing will make your history cleaner because the way it works is it takes whatever changes you made in formtastic and caches them, then in merges in new changes from master, then it re-plays your formtastic changes on top. This may take a little more work than simply merging though (and you'll have to look up rebasing to understand how it works).

Either way, once everything is conflict-free, tested, and committed in your branch, then you can go back and merge your changes into master like this:

git branch master
git merge formtastic


回答2:

You need to add a new remote reference to your upstream repo, the upstream repo being the original repo you have forked. See:

  • "Git fork is git clone?",
  • "What is the difference between origin and upstream in GitHub?".

git remote add upstream https://github.com/gregbell/active_admin

Then you can fetch/pull from upstream and update your own local branch.
The various options are explained in "How do I clean up my Github fork so I can make clean pull requests?".