2

Occasionally when a pull fails and I end up with dozens of untracked files. These files appear to be all the files changed after the last pull. eg:

$:/var/django/freshplum$ sudo git pull
Updating acb962a..eaf8c43
error: Your local changes to 'plum/api/resources/product.py' would be overwritten by merge.  Aborting.
Please, commit your changes or stash them before you can merge.


$:/var/django/freshplum$ git status
# On branch master
# Your branch is behind 'origin/master' by 140 commits, and can be fast-forwarded.
#
# Changed but not updated:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   deleted:    plum/api/resources/feed.py
... dozens of files ...
#   modified:   plum/www/views.py
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   plum/bacon/tests/models/segment.py
... dozens of files ...
#   plum/www/templates/www/internship.html
no changes added to commit (use "git add" and/or "git commit -a")

I can solve this problem with:

git reset --hard HEAD
git clean -f -d
git pull

What's the best way to resolve this situation?

Sam Odio
  • 2,717
  • 5
  • 22
  • 25
  • Edit: Noufal offers an explanation of Git's handling of merge conflicts here: http://stackoverflow.com/questions/9189867/after-a-git-merge-conflict-a-lot-of-files-i-didnt-touch-become-changes-to-be-c – Sam Odio Jul 17 '12 at 17:57
  • Also, kylben offers a description of how to use git stash here: http://stackoverflow.com/questions/7694467/resolving-git-merge-conflicts. – Sam Odio Jul 17 '12 at 18:04
  • 1
    I still find it strange that git would add all the changed files to your working directory instead of just rejecting the pull and asking you to stash your changes first. – Sam Odio Jul 17 '12 at 18:05
  • Git does reject the pull, and does ask you to stash your changes first. Why are you saying it's not the case? – Matthieu Moy Jun 16 '15 at 15:00
  • Do not _ever_ use sudo to run a Git command. – Matthieu Moy Jun 16 '15 at 15:01
  • @MatthieuMoy Git does reject the pull, but seems to leave successfully pulled files in the working directory even if the pull as a whole failed – lucidbrot Oct 18 '18 at 06:19
  • @lucidbrot: either you are in a different situation, or you're confusing files that were already there and the ones that "git pull" would have brought. Git takes great care of not starting a pull won't be able to complete. The error message "Please commit your changes or stash them before you can merge" stops the merge before checking out any files. – Matthieu Moy Oct 18 '18 at 19:55
  • @MatthieuMoy I must have been exausted... I actually did not find that line you quoted in the question when I wrote my previous comment and answer. I was actually in a different situation (where git actually created files from the new commit but then aborted the pull because of permission problems). But.. I'm confused now: If git tells Sam to stash his changes (as it should), where are his untracked files coming from? – lucidbrot Oct 18 '18 at 20:04

3 Answers3

1

Another way to do it, though not neccessarily better: Do what git tells you to do.

Stashing takes the dirty state of your working directory — that is, your modified tracked files and staged changes — and saves it on a stack of unfinished changes that you can reapply at any time.
- Git - Stashing

git pull failed, you have many untracked files that were 'pulled' but then the pull was aborted and the files stayed. Now:

git add -A        # stage all files so they are now stashable
git stash         # stash them - the working dir is now clean
git pull          # pull
git stash drop    # forget the stashed changes. Alternatively: git stash pop

Note that this will also get rid of any untracked files you had before pulling.


There's a second way that git is reommending you:

Please, commit your changes

git add -A
git commit -m "new commit"
git pull

This makes sense if you want to keep your local changes and get the new commits from the remote. You might then encounter merge conflicts which you can resolve.

lucidbrot
  • 5,378
  • 3
  • 39
  • 68
0

Try this in order first:

git add --all (or specific files)
git commit -a (or specific)
git pull
git push origin master (or your branch)

Then if you don't have request try the same lines you post (git clean or reset).

Finally if you have conflicts remove them.

-1

Well, for starters, you should probably pull more often so you don't have 140 commits to merge.

Second, you should either stash or commit your local changes before you try to pull, just as the message says.

tpg2114
  • 14,112
  • 6
  • 42
  • 57