31

A Git repository has been cloned on several developers' local machines. Some changes have been made to the code in the repository. We're now getting the error:

error: Your local changes to the following files would be overwritten by merge:

        public_html/sites/file
        public_html/sites/file1.txt
        public_html/sites/file2.txt
Please, commit your changes or stash them before you can merge.
Aborting

I've read quite a few threads online, and several different options have been suggested. One approach was run:

 git stash
 git pull
 git stash pop

I think I understand the basic principle of stashing. My question is, is this a good solution, and could I run into any issues using this approach? I have a reasonable understanding of web development in general, but I'm a fairly basic Git users and wouldn't have a lot of ability to get myself out of trouble at this point.

g_thom
  • 2,810
  • 2
  • 18
  • 18

4 Answers4

14

git stash is perfectly legitimate, though as Greg said, for some reason fixing the conflicts can get strange. But they are still fixable, you won't actually fubar anything. The command as I know to re-apply the stash is git stash apply, though pop may be an alternative that I'm not aware of (or it could do something different, I don't know, so you probably want to use apply.)

Is there a reason you don't want to commit those changes before merging? Generally that's the right thing to do.

Another option is:

git stash
git checkout -b newwork
git stash apply
git commit ...

This creates a new branch, which will allow you to get your master up to date without conflicts, (checkout master again, then pull or fetch + merge). Then you can merge your branch back with (while still on master) git merge newwork. You can resolve the conflicts on master, while still retaining the work on newwork without any conflicts. This is a bit safer if you are worried about conflicts really screwing things up, but generally, conflicts are just part of the process, so don't worry too much about them.

James M
  • 18,506
  • 3
  • 48
  • 56
kylben
  • 349
  • 1
  • 6
  • No reason - I'm just not a super knowledgeable user. Can I commit those changes that have been made to the remote branch? Ie, `git add file.txt git commit`? Or am I think of this in the wrong way? – g_thom Oct 08 '11 at 03:31
  • I was just wondering if there was a specific reason not to commit. so long as there are not, go ahead and commit. Did you come from SVN or CVS? It is one of the hardest things to get used to in git, is that you commit *then* fetch/merge, which is the opposite of wht you learn in something like SVN. Also, git commit -a -m "messge" saves the step of adding for files git already knows about. – kylben Oct 08 '11 at 03:39
  • 1
    Oh, another newbie thing that's easy to forget, commit only updates your local repo, you have to then push for it to go to the server. The advantage there is that any conflicts are local, and so you can get it all cleaned up, and push is a trivial operation. The sequence is usually: commit, fetch, merge, [fix conflicts and commit, if any], push. – kylben Oct 08 '11 at 03:42
  • I'm coming from a background of source control through things like shared drives and 'did you change this file' emails. So, this is my first real foray into source control. The challenge for me is getting the concepts, but I think I'm getting there. It sounds like what I need to do is commit the changes on the remote branch. Previously, I'd thought I could only commit changes made 'locally' (on my local machine). – g_thom Oct 08 '11 at 03:46
  • Maybe I have it totally backwards, though! – g_thom Oct 08 '11 at 03:47
  • I wonder if I explained clearly in my question - I'm getting the error message above when running `git merge` on the server. I'm honestly not sure if that's relevant or not. – g_thom Oct 08 '11 at 03:49
  • What do you mean by "running git merge on the server"? What do you mean by "commit the changes on the remote branch?" I suspect some confusion between local and remote repos and branches, which is understandable if you're new to git. – kylben Oct 08 '11 at 03:58
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/4101/discussion-between-kylben-and-g-thom) – kylben Oct 08 '11 at 04:01
  • Yes, I think that's exactly the issue. I think the problem I'm having is conceptual. The issue is this: a developer SSH'ed into the server where the live code for a site is. They edited several files without committing them via Git. Now, it seems the changes they made are causing this error when we SSH into the server and try to run `git merge` or `git push`. Hopefully, that makes sense! – g_thom Oct 08 '11 at 04:02
  • 2
    `apply` is like `pop`, but does not remove the state from the stash list. There are more details in the man page. – Paul Mar 26 '13 at 06:12
6

It's good practice to always commit any local changes before pulling (merging) new code. If you don't commit, then Git doesn't know how you want to manage your local changes. Merge only with a clean working tree.

There may be conflicts in the merge, due to the same files being changed locally and by somebody else. In my experience, resolving conflicts from an actual merge operation is slightly simpler than resolving the same conflict from a stash pop operation.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • When I run `git merge origin/master`, I get the error above. Pardon my naive question, but how do I merge conflicts? – g_thom Oct 08 '11 at 03:26
  • @g_thom: See http://book.git-scm.com/3_basic_branching_and_merging.html or http://progit.org/book/ch3-2.html for some good introductory material. – Greg Hewgill Oct 08 '11 at 20:16
  • Thanks, this is helpful. I went another way for this particular use case, but I learned something from the link provided. – g_thom Oct 10 '11 at 02:05
  • I committed some files to the git and then made a pull request. But it stops some of ma functionalities so I want to undo the pull I made. How can I do this to get the previously written code back. I didn't push the code. just committed and pulled it back – Sarfraz Ahmad Dec 16 '13 at 08:53
  • @SarfrazAhmad: If you have a question, please use the [Ask Question](http://stackoverflow.com/questions/ask) button instead of putting your question in a comment where only one person (me) will ever see it. – Greg Hewgill Dec 16 '13 at 08:58
  • yeah, i should, thanx – Sarfraz Ahmad Dec 16 '13 at 09:29
4

I have another solution:

git reset --hard FETCH_HEAD

It works in almost cases.

Narga
  • 59
  • 3
0

First you should:

git checkout -- public_html/sites/file
git checkout -- public_html/sites/file1.txt
git checkout -- public_html/sites/file2.txt

Next step:

git pull origin master
Đọc truyện hay
  • 1,913
  • 21
  • 17