2

I set up a repo on a shared hosting server. I want to use git from now on for pushing my local changes directly to my shared host, but despite the ok message I still see no changes in files:

ON SHARED HOST (REMOTE SERVER)

Initialize git repo

cd public_html/.git
git init
git add .
git commit -m 'First commit'

ON MY LAPTOP (LOCAL SERVER)

I cloned it the central remote repo on local machine

git clone ssh://me@mdomain.ro:15554/home/mydomain/public_html/.git

Now I have a working copy of the remote repository I only have one branch called 'master'. I can see it:

git branch

I add the remote repository under the name 'origin':

git remote add origin ssh://me@mdomain.ro:15554/home/mydomain/public_html/.git

I can list the remote repositories:

git remote

will display: origin At this stage I have done my work commit my changes again (as above) Now I push the changes back to the remote server where my main repository is.

git push origin master

ON SHARED HOST (REMOTE SERVER)

I managed to make it somehow by converting the remote repository to bare repository:

git config --bool core.bare true

ON MY LAPTOP (LOCAL SERVER)

I push again the data:

git push origin master

and got

Compressing objects: 100% (30/30), done.
Writing objects: 100% (32/32), 663.98 KiB, done.
Total 32 (delta 21), reused 0 (delta 0)
To ssh://me@mdomain.ro:15554/home/mdomain.ropublic_html/.git
   a416e72..bb19005  master -> master

However, when accessing my url in browser I still cannot see changes. What is wrong?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Kandinski
  • 953
  • 11
  • 30
  • The `git remote add` part isn’t necessary, by the way; `git clone` gets that for you. – Ry- Apr 01 '13 at 21:05
  • @minitech In my case it was useful to name the remote repository as'origin'. More handy. – Kandinski Apr 01 '13 at 21:13
  • you should consider to configure the remote repository as a bare repo. In order to convert a repo to a bare one you should run: `git config --bool core.bare true` – niculare Apr 01 '13 at 21:15
  • Thanks I did this and worked. Good point. See my update. However I see no changes in my website. What could be wrong? – Kandinski Apr 01 '13 at 21:19
  • You can’t just put the bare repository in `public_html`, either (that’s why I didn’t answer) — I *think* it takes hooks, but I’m inexperienced with Git, so maybe not. – Ry- Apr 01 '13 at 21:52

3 Answers3

3

The error message is quite clear and indicative that the server cannot update the working tree of the currently checked out branch (master).

To fix this, you could look into how to convert your server's repo to a bare repo.

Another option (though I highly recommend against this) is to set receive.denyCurrentBranch in the server repo's git config to either ignore or warn or false.

If what you're trying to do is to use git push as a means to deploy new code on the server, and you need to have a work-tree even with a bare repo, you could look at this answer I posted some time back, which details how to have a separate work-tree checked out on the server and setup a post-receive hook which takes care of updating this worktree from the bare repo.

Community
  • 1
  • 1
Tuxdude
  • 47,485
  • 15
  • 109
  • 110
  • Thanks I did this and worked. See my update. However despite the succesful message I see no changes in the files on website. What could be wrong? – Kandinski Apr 01 '13 at 21:27
  • @Kandinski - I've updated the answer with a possible link to another answer I posted some time back, which I'm guessing is what you're looking for ? – Tuxdude Apr 01 '13 at 21:41
  • I carefully read it so my work tree is public_html and my repo is public_html/.git (correct me if i am wrong). I don't know how to split them up. I have created the public_html/.git/hooks/post-receive on the main remote server. Now locally I run git push origin master which runs ok, no errors but I see no changes on the files on the main remote server. What is still wrong? – Kandinski Apr 02 '13 at 00:03
  • @Kandinski I assume these files are webpages. Do you need them to be in the public_html directory? Then you need to run `git reset --hard HEAD` in the public_html director. You can most likely automate this as a hook, but I don't have any experience with those. – Code-Apprentice Apr 02 '13 at 00:10
  • @Code-Guru, I run the git reset --hard HEAD but I get: fatal - this operation must be run in a work tree. Now where the earth is this work tree as I my work tree should be here? – Kandinski Apr 02 '13 at 11:16
  • @Tuxdude How do you suggest I should split up my work tree and repo to follow the best practice? I can't believe, isn't it my problem a common one? – Kandinski Apr 02 '13 at 11:18
  • @Kandinski - a git's bare repo only needs to contain all the files under the `.git` directory. The first link in my answer should explain how to convert your existing repo into a bare repo. You can think of the work-tree is the list of files checked out corresponding to a branch or a tag or any commit. A bare repo does not have a work-tree of its own. But using a post-receive hook, you can run a command such as `GIT_WORK_TREE=/foo/bar git checkout -f` to update the work-tree at `/foo/bar` on every `git push`, so that the work-tree gets updated. – Tuxdude Apr 02 '13 at 16:18
0

For a git repo to admit push it has to be configured as bare.

A bare-repository just contains the version information. The working files are not there. Instead it contains the whole content of the folder .git.

You created your repository in the 'server' as non-bare repository so you cannot push. If you have just started just create it again with git --bare init but if you already did important changes you need to convert it

iberbeu
  • 15,295
  • 5
  • 27
  • 48
  • Thanks I did this and worked. See my update. However despite the succesful message I see no changes in the files on website. What could be wrong? – Kandinski Apr 01 '13 at 21:27
0

This method is for git self-hosting or sharing host ... (isn't github host or private repo)

First step from git Bash of your win or other OS :

git push -u origin master

[origin] is you <alias> remote

In you hosting server (sharing or any Server that support git and SSH)

git log --oneline 

you see last commit that you pushed from you client git Bash. for example you see something like this:

6d523ed modify last commit
37b624b .gitignore is now working
f1e0413 add *.zip to ignore file
c5r0d74 add .gitignore
4e87i69 Installing

Note: above code was my commit. you commit is differance.

Now you have to using git reset for sharing you last commit on you local computer to your public_html or anyplace that you initialized git on your Hosting Server.

git reset --hard 6d523ed

Note: 6d523ed is SHA-1 Last Commit which you can see above text.

Important: I testing almost 100000 way for sharing direct (git bare) from my local computer to remote master (public_html) but it not working...!!!!

It may work in GitHub but other hosting server not working any change without use reset --hard

Rai Rz
  • 493
  • 9
  • 22