Git repository on server reverts my changes but has same log as local

Viewed 268

I have a really weird issue that I can't seem to resolve, although I've search for 4 hours now.

Basically, I have a Git repository on a server. I managed to clone that repository locally through SSH.

If I make local changes, I can push them to the server. But the server will revert my changes.

Step 1: making local changes

I add a test.php file locally. It shows up if I do git log. enter image description here

Step 2: pushing my changes

I push my changes to the remote repository. There is no error. enter image description here

Step 3: check the server repository

If I ssh onto the server, and do git status, it should show no local changes. However, it shows changes to be committed. These changes actually undo exactly what I just pushed. In this case, it will delete the file I just added. enter image description here

Step 4: check log on server

If I run git log, I have the exact same log as my local repository. enter image description here

I really don't understand what's happening. I can push changes but they're undone as soon as they reach the server.

Any help would be greatly appreciated because this is driving me crazy, as I can't find anyone who has encountered the same issue as me.

2 Answers

That's because when pushing, the server only updates its commit history and index area, without touching your working tree. This way after pushing, your working tree on the server remains at its previous state, while its branch has been updated.

It's generally recommened against pushing to the checked-out branch a non-bare repo, so perhaps pushing to another server (e.g. GitHub) and pulling from there on your server might be a better idea.

If you're sure you're doing everything right on your server, manually run git checkout -f everything something's pushed onto it, to ensure its working tree matches its index area.

The answer iBug provided was helpful as it explained why my changes were being reverted. I tried to make a bare git repo on the server but it didn't help me since I needed the files to actually be present on the server.

Anyway, here is what I did: I made the remote repo update when changes are pushed to it.

git config receive.denyCurrentBranch updateInstead

Now, when I push to the server, it keeps the changes and updates its working tree.

I found the solution on this page.

Related