git status will show the differences between your currently selected branch (probably main on prod) and the current state of the files in the directory on your server. So:
You SSH and make a change to index.php for example.. this file is now different to what's on the main branch in your repo.. so will flag up as a difference in the status output. This has nothing to do with whatever you may, or may not have on your local box. Running a hard reset on the server would indeed reset that index.php as it will reset the code state on the server to the commit hash you provide from the repo.. so in reality, you could go back to the previous commit that would just reset this change to index.php but you could specify a commit hash from 2 years ago and reset the code to that state. Think of hard reset as reverting back to a snapshot sometime in the past. How far in the past you go is up to you. This however, ideally, should never be run on a production server manually.. and please read and understand the implications of this command as it's destructive and could result in a lot of lost work.
If a file is changed on the server, and you try to overwrite that file from git (a deploy will just use a git pull command), it will fail as you need to make a choice in the state of that file you want.. git is smart enough not to just overwrite something without permission.. so you have a choice to either commit or stash the change. You can safely try this locally. Update one of you files and then run a git pull, you'll be presented with the same notification.
detached head means that the code state isn't attached to a particular branch. I'm assuming you set a "deployment branch" or something like that in forge that it will use.. so it doesn't matter if the head is detached or not as it will deploy the main (for example) branch.
I think there's a misunderstanding here on how and what git works and is.. especially between your local box and your server. In short:
- Your local box knows nothing of what's actually on the server
- The server knows nothing of what's actually on your local box
- Git is a "proxy" between the 2.
You make a change locally. Only your local box knows about the change. A git status here would compare your local state to whatever is in git for the same branch.
You commit and push the changes from your local box to your git repo. At this point, the server and your local box will differ only because the server doesn't automatically receive the changes.
When you hit deploy, the server pulls the new changes (the difference between the code state currently deployed and what's in the repo) from git (nothing related to your local box.. you could have already made a bunch of new changes locally).
Once you understand this process more, the reasons for not editing files live in production will likely become more apparent and you'll have a smoother process and less issues in the future.
What I recommend to test this concept locally is:
- Create a new folder on your local box.. call it
prod or something. This will represent your server.
- Make a change in your normal project folder.
- In the
prod folder, run a git pull command (to simulate a deployment).
- Note no updates happen in the
prod folder, "nothing to update".
- Commit and push the change in your project folder as normal.
- Run
git pull in the prod folder and see that there was 1 file changed.
If you want to test the "hacking" scenario:
- Make a change to a file in the
prod folder.
- Run a
git pull command in the prod folder.
This will show a notification that you saw on the server about committing changes because git won't just blindly overwrite the manual changes you made with a pull.
Hope this helps explain the process more.