'I made changes on my server and now git won't let me push to master because it has "unstaged changes." How can I get around this?

Noob question about Git:

I made a few minor changes on my server directly (without using Git). I just opened cPanel and changed the names of a few image files and deleted a few unused images.

Then I went back to my code editor and made changes in a few files. But I can't push to master now, because Git says, "! [remote rejected] master -> master (Working directory has unstaged changes)"

I'm not sure how to get around this. I wouldn't mind a solution that undoes the changes I made directly to the server files. I wouldn't mind a solution that loses the changes I made to my text in the editor either. They were all very minor. I just want them back in sync!

I tried to create a new branch with "git checkout -b example" and did "git pull" into that branch. That went fine. I can push too, and I can "git checkout master" and then "git merge example" and that seems successful. But then when I "git checkout master" and then "git push origin master" it gives me the error message.

I appreciate any advice you can give!

git


Solution 1:[1]

A simple solution for your case is to:

  1. enter your server's repo directory
  2. rm -r *
  3. git reset --hard or git checkout -- .

Having step 2 is just in case your future changes have files in conflict.

Step 3 resets every tracked files on your working directory back to the current branch's version.


If you would like to keep the changes, you can perform git add . and then git commit on your server, and then on your machine do git pull, which should trigger a merge. Then you can push as usual.

Solution 2:[2]

To clear the recent changes and go back to the last state (last commit), just make sure you're at the top-level of your repo, and do

git checkout -- .

Another way to clear these changes is git stash which would put these changes in an isolated branch, just in case you regret having wiped them earlier.

Solution 3:[3]

Sometimes file changes could be file permissions. You can check which with.

git add -p .

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 renyuneyun
Solution 2 Romain Valeri
Solution 3 TvC