Clean your Git up to Pull successfully

14 Feb 2009

Here's a quick Git pulling & merging tip for those of you learning this awesome Distributed Version Control System.

Git Pull's Double Duty

As you may be aware, the git pull command actually does two things under the covers. It does a git fetch to freshen your tracked remote branch and it does a git merge to merge it into the linked local branch. Sometimes this can go awry if you've been experimenting with files locally and forgot to return them to a checked-in state. Git errors out nicely, saying "I don't know what you want me to do, but I'm going to be cautious here and just let you tell me exactly how to handle this."

Error Message

One of the possible error messages you'll see is:

error: Entry 'myapp/src/main/resources/scripts/launchapp.bat' not uptodate

Solutions

If your local changes are unimportant to you and you just want to get back in alignment with the remote branch, you have two options to apply depending on the state of your local files. First, you can reset any tracked files to their last committed state via the following command. This discards any local changes to any tracked files.

git reset --hard

Second, you can discard any untracked local files via the git clean command, in the case that they are colliding with files that the remote branch has actually added and is now tracking. You can purge your repository's current branch of untracked local files by typing:

git clean -f

And in case you are paranoid about what this will remove, you can get a safe preview of what it would do by typing:

git clean -n

Which outputs a preview list like so:

[~/Documents/Code/myproj.git]: git clean -n
Would remove morecruft.java
Would remove unwatedfile.txt