Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to merge in Git without being on the merged-to branch?

Tags:

git

merge

Today I was using Git and something happened 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?

like image 852
Ram Rachum Avatar asked Apr 13 '11 20:04

Ram Rachum


2 Answers

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)

like image 149
Vi. Avatar answered Sep 19 '22 17:09

Vi.


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.

like image 24
Karl Bielefeldt Avatar answered Sep 21 '22 17:09

Karl Bielefeldt