Common causes
- The terminal is in the wrong directory, often one level above or below the project
- The project was uploaded by FTP, ZIP or a hosting file manager, which does not include the hidden .git folder
- git init or git clone was never run for this folder
- A deploy script or cron job runs git from a different working directory
- The .git folder was deleted, or a .git file points to a submodule/worktree path that no longer exists
- GIT_DIR or GIT_WORK_TREE environment variables point somewhere else
How to fix it
- Check where you are. Run pwd and ls -la. The project root should contain a .git directory (hidden). If it is in a subfolder, cd into it.
- Find the repository. Search for it with find ~ -maxdepth 3 -name .git -type d, then cd into the folder that contains the .git directory you found.
- Clone instead of uploading. If the code was uploaded without .git, clone it on the server instead: git clone [email protected]:user/repo.git into an empty folder, or use cPanel's Git Version Control.
- Initialise a new repository. If this folder should become a repo, run git init, git add ., git commit -m "Initial commit", then git remote add origin <url>.
- Fix scripts and cron jobs. Use git -C /home/user/site pull or cd /home/user/site && git pull so the command runs in the right directory regardless of where the script starts.
- Check broken worktree or env vars. Run env | grep GIT and unset stray GIT_DIR values. If .git is a file pointing to a missing path, re-clone or run git worktree prune in the main repository.
Shell
pwd && ls -la # is there a .git directory here?
git rev-parse --show-toplevel # works only inside a repo
# run git against a specific folder from anywhere (cron, scripts)
git -C /home/user/site status
git -C /home/user/site pull --ff-only How to stop it happening again
- Deploy with git clone/pull or CI instead of uploading files that lack .git
- Use git -C <path> or absolute paths in scripts and cron jobs
- Never delete hidden folders when cleaning up project directories