5

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?

Ram Rachum
  • 84,019
  • 84
  • 236
  • 374

2 Answers2

5

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)

Vi.
  • 37,014
  • 18
  • 93
  • 148
  • @Vi: Not a practical solution, I'd have to maintain that script/alias over many different servers. – Ram Rachum Apr 15 '11 at 10:00
  • @cool-RR, Why? Provided you access servers based on pubkeys (not typing passwords every time), it is just `for i in server1 server2 server3; do scp gitffmaster ${i}:/usr/local/bin/; done` – Vi. Apr 16 '11 at 13:00
  • @Vi: Does that work in shared servers as well, where I don't have root access? – Ram Rachum Apr 16 '11 at 18:02
  • @cool-RR, If no root access then (provided you already have usable `$HOME/bin` directory) it is like `for i in server1 server2 server3; do scp gitffmaster ${i}:bin/; done`. In my system `.profile` automatically adds `$HOME/bin` to PATH if found. Also you can distribute `.gitconfig` file with alias for this command. – Vi. Apr 16 '11 at 18:05
  • Is there a way to know for sure that a merge is fast-forward? – AeroCross Nov 23 '11 at 12:30
  • @AeroCross, You can check for fast-forwardness before that: `git merge-base refs/heads/master refs/remotes/origin/master` should be equal to the value of "refs/remotes/origin/master". – Vi. Nov 23 '11 at 14:39
1

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.

Karl Bielefeldt
  • 47,314
  • 10
  • 60
  • 94
  • Wouldn't work in this case-- These files were important and I wanted to save them. I can transfer them to a temporary folder, but that would be cumbersome. – Ram Rachum Apr 13 '11 at 21:23
  • @cool-RR, you want to save them, but not version them, and you have changed them from the version in `master`? It might be safer using a workflow like [this](http://stackoverflow.com/questions/928646/how-do-i-tell-git-to-always-select-my-local-version-for-conflicted-merges-on-a-sp) – Karl Bielefeldt Apr 13 '11 at 21:59