There is a working site, on the same server there is a git-server (gitstack).

How to create a repository on the server with this working site?

Those. I need to create a repository, I clone it to a local computer, modify something, push it, and after that - what I just started should be immediately visible on the working site .

It is clear that it is better to have a separate server for development, separately or automatically deploy everything, but alas - such conditions as I stated.

  • 2
    It is necessary to configure the hooks that were made to the repository according to the push. On a normal git server, a bare repository is stored, that is, there are no sorts in the form that you represent them. - KoVadim
  • one
    accepted your answer, study git. I realized that I needed to use linux and stopped now at gitlab'e - sanu0074
  • one
    In general, this is for study (for myself) I want to raise the hosting of repositories, well, to push my site there. I have a few small projects of my own, ftp is tired, and knowing git / gitlab will be very useful - sanu0074
  • one
    By the way, there is a third way - through automation systems, for example Jenkins. Look at least briefly at this option, suddenly you are more suitable. - Nick Volynkin
  • one
    Pro benefit from the knowledge of git completely agree. By the way, there is GitHub Pages, where this process is already implemented. Very cool stuff. The repository is simply attached to the site, you push to the master branch or github-pages - and the site has been updated. pages.github.com - Nick Volynkin

1 answer 1

Deploy directly, without remote repository and Git server

Requirements:

  • SSH server access
  • git is installed on the local machine, as well as rsync or git-sync
  • on git server is not needed

What to do?

It is not necessary to install Git on the server or copy the .git folder there. To update the server from the git repository, you can use the following command:

 git ls-files -z | rsync --files-from - --copy-links -av0 . user@server.com:/var/www/project 

This command copies all files. rsync uses ssh (secure shell) , which is installed on the server in any case.

But at the same time, you will probably have to manually delete files that were deleted from the project (that is, they did not change, but ceased to exist in the next commit).

Instead of rsync, you can use the git-sync utility written by Ian Bicking . According to the author, it works with Git faster than rsync.

Why is this a good way?

The less software you have installed on the server, the more secure it is and the easier it is to administer and document it. By the way, it also excludes the need to keep a full Git repository with the entire history on the server. This would only complicate the server security task.

(Written based on the answer by @Christian , Ian Bicking blog)

Deploy via a remote repository on the server.

This method carries the risk of data leakage!

Sources of 3300 global Internet projects were received.

Requirements:

  • SSH server access
  • Git is installed on the server.
  • Git-server is installed on the server, for example Gitlab or Gitstack .
  • Git is installed on the local machine.

If you start from a server with files and without a repository

On the server in the project folder run:

 git init git add --all git commit -m'сообщение, описывающее текущее состояние проекта' 

And go to the next item.

If you start from the server with the files and repository

Select a folder on the local machine where the project will be located. Run there:

 git clone -o production username@webserver:/path/to/htdocs/.git 

If you start from a local repository

  1. Copy your local .git folder to the server.
  2. In the local copy, open .git/config and add your server as remote :

     [remote "production"] url = username@webserver:/path/to/htdocs/.git 

a common part

  1. On the server, replace .git/hooks/post-update with the code below in this answer.
  2. On the server, add write access to this file:

     chmod +x .git/hooks/post-update 
  3. Now, when you do git push from a local repository, the repository on the server should automatically update the working directory:

     git push production 

    Useful links on the topic:

http://toroid.org/ams/git-website-howto
https://www.digitalocean.com/community/tutorials/how-to-set-up-automatic-deployment-with-git-with-a-vps

post-update hook:

Git-hook found on this site .

 #!/bin/sh # # This hook does two things: # # 1. update the "info" files that allow the list of references to be # queries over dumb transports such as http # # 2. if this repository looks like it is a non-bare repository, and # the checked-out branch is pushed to, then update the working copy. # This makes "push" function somewhat similarly to darcs and bzr. # # To enable this hook, make this file executable by "chmod +x post-update". git-update-server-info is_bare=$(git-config --get --bool core.bare) if [ -z "$is_bare" ] then # for compatibility's sake, guess git_dir_full=$(cd $GIT_DIR; pwd) case $git_dir_full in */.git) is_bare=false;; *) is_bare=true;; esac fi update_wc() { ref=$1 echo "Push to checked out branch $ref" >&2 if [ ! -f $GIT_DIR/logs/HEAD ] then echo "E:push to non-bare repository requires a HEAD reflog" >&2 exit 1 fi if (cd $GIT_WORK_TREE; git-diff-files -q --exit-code >/dev/null) then wc_dirty=0 else echo "W:unstaged changes found in working copy" >&2 wc_dirty=1 desc="working copy" fi if git diff-index --cached HEAD@{1} >/dev/null then index_dirty=0 else echo "W:uncommitted, staged changes found" >&2 index_dirty=1 if [ -n "$desc" ] then desc="$desc and index" else desc="index" fi fi if [ "$wc_dirty" -ne 0 -o "$index_dirty" -ne 0 ] then new=$(git rev-parse HEAD) echo "W:stashing dirty $desc - see git-stash(1)" >&2 ( trap 'echo trapped $$; git symbolic-ref HEAD "'"$ref"'"' 2 3 13 15 ERR EXIT git-update-ref --no-deref HEAD HEAD@{1} cd $GIT_WORK_TREE git stash save "dirty $desc before update to $new"; git-symbolic-ref HEAD "$ref" ) fi # eye candy - show the WC updates :) echo "Updating working copy" >&2 (cd $GIT_WORK_TREE git-diff-index -R --name-status HEAD >&2 git-reset --hard HEAD) } if [ "$is_bare" = "false" ] then active_branch=`git-symbolic-ref HEAD` export GIT_DIR=$(cd $GIT_DIR; pwd) GIT_WORK_TREE=${GIT_WORK_TREE-..} for ref do if [ "$ref" = "$active_branch" ] then update_wc $ref fi done fi 

The answer is written based on responses from the English StackOverflow . Added, reworked, collected in one answer.

  • four
    At one time, there was a survey on Habré on the topic "how do you make a deployment on a production server", there all the possible approaches were briefly described: upload files to ftp, manually update from git, posthuk from git, deploy via CI systems. The question would be about how to deploy through git, and not about the deployment methods in general, but I consider it useful to complete the picture with an overview of the deployment methods. - AK
  • one
    I could somewhat better answer. You need to specify what git pull needs to be done under the apache user or www-data (from which the web server runs), which has its subtleties due to the fact that the user does not have a shell. Firstly, there will be problems in order to simply log in. Secondly, there may be problems with probing ssh-keys, if you do not use passwords and do not create your own ssh-key for the apache user. You can also automate the deployment operations through ansible - some pitfalls are indicated in this question . - AK
  • @AK I'm in the country, only the phone is in my hands. If you want, you can improve or rewrite this answer, I do not mind. Or publish your, with me plus. ) - Nick Volynkin
  • @AK and I have read and indicated this answer a long time ago. ) - Nick Volynkin