Home > OS >  Git remote rejected on new commit after successful pull
Git remote rejected on new commit after successful pull

Time:11-19

New user to git, I'm running a website accessible via cpanel. Usually use FTP but noticed the git option so giving it a whirl.

Set my public_html directory as git remote, cloned local repository onto my laptop (although nothing was copied to my local directory, another issue.)

Right now I'm setup and have done a few commits and pushed successfully but ran into an issue now where every time I commit and push I get this response -

[remote rejected] master -> master (Working directory has unstaged changes)
error: failed to push some refs to 'ssh://mywebsite/public_html/'

I've used git reset --hard origin/master to reset and I can successfully fetch, pull and push without any error.

PC MINGW64 ~/Desktop/mywebsite (master)
$ git pull
Already up to date.

$ git push
Everything up-to-date

So I created a file for testing, git_commands.txt

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        git_commands.txt

nothing added to commit but untracked files present (use "git add" to track)

$ git add .

$ git commit -m "added git_command.txt"
[master 695416e] added git_command.txt
 1 file changed, 1 insertion( )
 create mode 100644 git_commands.txt

$ git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 405 bytes | 405.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To ssh://mywebsite/public_html/
 ! [remote rejected] master -> master (Working directory has unstaged changes)
error: failed to push some refs to 'ssh://mywebsite/public_html/'

Any assistance would be appreciated, seems everything I do gets the above error as soon as I attempt to push even though on the surface I'm all 'up to date'.

CodePudding user response:

! [remote rejected] master -> master (Working directory has unstaged changes)

What this is telling you is the remote repository at ssh://mywebsite/public_html/ has unstaged changes in its working directory. Go to that repository and either erase or commit those changes.


Normally remote repositories are "bare"; they have no files checked out. Pushing commits to a bare repository is simple.

A "non-bare" repository has files checked out. When you push, you're updating both the repository and the checked out files. This is sort of ok if there are no changes in the checkout, Git can just update the checkout to the new commit.

If there are uncommitted changes in the remote checkout and you push, that is a very confusing situation. Imagine you're working on your public_html checkout and someone else pushes to it; suddenly, and without warning, everything changes out from under you. Git is refusing to let that happen.


For this reason, and others, git push is a poor release mechanism. Use SFTP, FTPS, or SCP to send your changes.

CodePudding user response:

you can try:

git pull --rebase origin master

git push --all

enter image description here

  • Related