1

I honestly don't understand git that well, and I'm beginning to resent even attempting to use it.

I'm trying to push a new version of my project to my remote repository (git push origin master), but when I do, I get the message:

Error: failed to push some refs to 'myRepo'. Updates where rejected because the tip of your current branch is behind its remote counterpart. Integrate the remote changes (e.g. git pull ...) before pushing again.

So I try to pull (git pull origin master), and then I get the following error:

Error: The following untracked working tree files would be overwritten by merge: <bunch of image files and a few PHP files>

What am I doing wrong here, in plain english (and a bit of git to help me solve this)?

1 Answer 1

3
  1. You cannot push because your local branch and the remote branch have diverged. After the last time the two branches were synchronized, there were new commits in your local branch and the remote branch too. In situations like this you cannot push, the branches need to be merged first.

  2. A pull tries to do a merge. Apparently new files were added in the remote branch, and when Git tries to create them in your working tree, it turns out that files exist with the same name. Git cannot know for sure what to do with the existing files: should it delete them? Should it merge the incoming new content into them? This is impossible to guess, only you can make these decisions.

To resolve this, either move those untracked files out of the way or commit them to your local branch. The first option will be easier for you probably: move these files somewhere else, temporarily.

After that git pull will work, unless there are some conflicting changes in the remote branch and your local branch. I hope this helps.

If you want to remove the untracked files, you can use the git clean command. Run first with the -n flag, which will not do anything just tell you what it would do ("dry run"):

git clean -n

As @Chris pointed out in a comment, you can save your untracked files on the stash:

git stash -u

This will also save all your other pending changes too, staged and unstaged. You can restore everything later with:

git stash pop
5
  • Amazingly, I understood that. I can see that a divergence has taken place: Your branch and 'origin/master' have diverged and have 5 different commits each, respectively. What command would I use to "move those untracked files out of the way"? Jan 5, 2014 at 14:45
  • Do you want to move them somewhere else (to a temp directory) or delete them?
    – janos
    Jan 5, 2014 at 14:49
  • Definitely temporary directory. I've made a few backups in case things go bad though. Jan 5, 2014 at 14:51
  • 2
    Actually, there is: git stash -u will stash untracked files. You may want to play with some of the other options as well.
    – Chris
    Jan 5, 2014 at 15:07
  • Thank you @Chris, you're absolutely right! (added to my answer)
    – janos
    Jan 5, 2014 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.