Is it possible to merge in Git without being on the merged-to branch?
Today I was using Git and something happene开发者_高级运维d to me which I didn't know how to deal with.
I was on branch development
, and I did git fetch
to get the new origin/master
. I wanted to merge origin/master
into master
, and end up with the updated master
checked out. Normally, I would do this:
git checkout master
git pull
But there was a problem; the currently checked-out branch development
had a .gitignore
which included a lot of files that the old master
didn't. The old master
had these files version-controlled. So Git wouldn't let me checkout master
, because then these files would be overwritten.
I didn't know what to do, so I simply checked out origin/master
instead.
If there was a way to merge origin/master
into master
without checking out master
, I think that would have saved me. (And it was a fast-forward merge, so Merge-Fail wasn't an option.)
What can I do about this?
Fully-fledged merge requires work tree to resolve conflicts.
If you know that merge is fast-forward, you can use git update-ref
Example:
git update-ref -m "Fast-forward merge" refs/heads/master refs/remotes/origin/master
Don't do just git update-ref master
, as it will create "master" in .git/
instead of .git/refs/heads/
(refs/remotes/origin/master
can be abbreviated to origin/master
)
I usually delete all the newly ignored files from the working tree in that case, so the checkout
won't be overwriting them any more. git clean
can help you with that. You might also look at the -f
or -m
options of checkout
for other ways to do it.
精彩评论