Sometimes you need to script a git pull or you just want to completely reset a repo to HEAD. You there are loads of errors git can throw you in the process; such as error: unable to unlink old ‘sites/default/settings.php’ or error: The following untracked working tree files would be overwritten by checkout. Below is my take on the complete repository reset/clean/pull. Let me know in the comments if you’ve got a better way or find errors I’m missing.

  # Find and record unwriteable files.
  NOWRITEFILES=$(find * ! -perm -u+w)
  # If there are unwriteable files
  if [ -n "$NOWRITEFILES" ]; then
    # Change permission to writeable
    chmod u+w $NOWRITEFILES
  # Clean the directory, but don't remove files specified in .gitignore.
  git clean -d -f
  # Reset the files
  git reset --hard HEAD
  # Get the changes
  git pull -f
  # If there were unwritable files
  if [ -n "$NOWRITEFILES" ]; then
    # Change permission back to unwritable
    chmod u-w $NOWRITEFILES