2

Recently, both our Git remotes (one a production server, and the other a staging one) cause issues when pushing changes.

Typically :

Local

git add <list of untracked files>
git commit -m <message>
git push <remote> <remote active branch>

Remote

git status
On branch <active branch>
Untracked files:
  (use "git add <file>..." to include in what will be committed)

.... all the files that have been committed ....

We are not sure what could've changed in the recent weeks because everything worked fine before.

We currently fix this by connecting to the remove server and executing

git reset --hard HEAD

Why is this happening and how can we fix this?

  • Hello and welcome to Server Fault! Does your remote application write logs on its working directory? Looks like on the remote server there are (new?) files which aren't on your local repository. – Mr Shunz Oct 25 '18 at 15:35
  • 1
    Hi, how can there be remote files that aren't in my local when I push the changes from the local repository? I have tried adding the git reset --hard HEAD in the post-receive hook, but it does nothing; the script looks like it's never get called. – Yanick Rochon Oct 25 '18 at 17:55
0

If I understand your setup correctly, you are pushing to a non-bare repository. What happens is that the working directory is not automatically updated when you do this. You are correct that the git reset --hard command fixes this. The reason your hook does not run is that the working directory and/or work tree may not be correctly set in the middle of a pull. To fix this, you can explicitly set it. There is an environment variable, GIT_DIR, that git sets for hooks. It corresponds to your .git folder. Using this, we can set your git tree correctly.

Here is the script I have used for this exact scenario before:

#!/bin/bash

git --work-tree "$GIT_DIR/.." reset --hard

As you can see, since your .git is always a subdirectory of the root directory of the repo, this just ensures the work tree is the right place to reset it.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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