Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make git merge handle uncommitted changes to my working tree?

A co-worker and I are both working on the master branch at the moment. I have some code in my working tree that I don't want to commit (debugging statements and the like). Now if he commits changes to some of those same files, I can't merge them:

$ git merge origin/master
Updating 1b8c5c6..eb44c23
error: Entry 'blah.java' not uptodate. Cannot merge.

Coming from a subversion background, I'm used to having my working tree automatically merged when I pull changes from the repository and if there are conflicts, I resolve them manually.

The quickest way I have found to do this in git is:

$ git stash
$ git merge origin/master
$ git stash pop

Essentially, removing my uncommitted changes, doing the merge and then re-applying the changes. How can I tell merge to automatically merge my working tree with the changes I'm trying to pull in?

like image 993
Jeremy Huiskamp Avatar asked May 02 '09 00:05

Jeremy Huiskamp


People also ask

Can't pull commit changes before merging?

The “commit your changes or stash them before you can merge” error is raised when you try to pull code from a remote repository that conflicts with a local change you have made to a repository. To solve this error, either commit your change to the repository, discard your change, or stash your change for later.

How do you solve fatal refusing to merge unrelated histories?

The alternative (and longer) way of fixing the fatal: refusing to merge unrelated histories issues is to unstage your current commits, stash them, clone your required remote repository, and then place your stashed branch contents into the new clone.


2 Answers

Forget everything you ever learned from subversion.

Always commit before introducing external changes.

Imagine you had a mostly-working tree -- maybe not perfect, but you're making some progress. Then you go to do a merge and the code you're bringing in just wreaked havoc (was buggy itself, too many conflicts to deal with, etc...). Wouldn't it be nice if you could just undo that?

If you commit, you can. If you don't, you're just going to suffer.

Remember: What you commit doesn't have to be what you push, but what you don't commit you can easily lose.

Just do the safe and easy thing and commit early and commit often.

like image 120
Dustin Avatar answered Oct 19 '22 05:10

Dustin


As far as I can tell, the best you can do is what you already have with git stash. I too find it strange that merge wants to deal only with clean trees.

like image 24
Norman Ramsey Avatar answered Oct 19 '22 05:10

Norman Ramsey