7

我想用git来管理远程服务器上的一些数据,所以我在那里建立了一个非裸存储库。我可以毫无问题地推送它,并且存储库本身会更新,但实际文件不会更改/添加/删除。我必须 ssh 进入服务器并执行

git reset --hard HEAD

让文件结构实际更新。

要做什么?

4

4 回答 4

8

You shouldn't do this. It's recommended to have bare repositories. In other words, no files checked out, just the .git directory itself. You can then checkout the repository to some other location on your server -- say, your web root. This way, you get:

  • git best practice. According to the Git docs, you can get "unexpected results" if you don't follow it. Anyone who's done a good bit of programming knows that "unexpected results" is code for "will probably eat your children and should be avoided at all costs."

  • better security, if you're planning to have the checked out files on the server accessible from a webserver.

  • Local modifications on your checked-out code, and the ability to make quick changes on the live checked out code. You could attempt to do this directly on the repository, but it would be messy and more prone to error.

  • The ability to update your server repository independently of updating your live service code. This is pretty crucial, if you're working remotely and need to send something to the server and then do further work before it will be ready for your live service, or if you have changes in your live service code (say, different configuration settings) and need to merge those changes with the changes in the repo, but can't do it just now.

I'd recommend the following steps:

  • Follow the git docs on setting up a bare repository
  • Check out code from your repo to your live service target dir
  • Setup a git hook (post-commit should be the right one IIRC) to update your live service when the repository is updated. It should probably cd to the live service dir, and do a git pull --rebase, and maybe set some file permissions.
  • Just push your code to the repo from your dev box from now on.
于 2009-09-16T16:59:51.607 回答
8

令人高兴的是,现在 git 本身直接支持这一点!您可以在此答案中找到详细信息,我刚刚对此表示赞同:

https://stackoverflow.com/a/38363683/85360

它建议将遥控器配置为

git config receive.denyCurrentBranch updateInstead

这样推送会导致更新的工作副本!

于 2016-11-15T02:31:31.953 回答
4

使用更新后挂钩:

挂钩

在那里,您可以在每次更新时执行任何您想要的操作。

于 2009-09-16T16:04:57.083 回答
1

Git 版本 1.9.1
Ubuntu 服务器 14.04 LTS
LAMP 服务器

每当我的一位 Web 开发人员将更改推送到服务器时,我都会将我的 LAMP 服务器设置为更新我的 Git 存储库的工作目录。我注意到日志会记录新的提交,但不会更新工作目录。无需为每次更新手动执行此操作(git checkout -f),而是可以在收到推送后自动设置为执行此操作。

  1. 在“.git”目录中,进入“hooks”文件夹。
  2. 在“hooks”文件夹中创建一个名为“post-receive”的文件,其中包含以下内容:

    #!/bin/sh

    # 收到远程客户端推送后更新工作目录。
    # 这应该指向 git 工作目录。

    GIT_WORK_TREE=/var/www/dev_site git checkout -f

  3. 通过在“hooks”文件夹中键入“chmod +x post-receive”来启用执行文件的权限。

现在,当提交被推送到 Git 存储库时,它会更新工作目录。当我在浏览器中访问时,我的网站现在会显示更改。

我的工作目录是 /var/www/dev_site

于 2015-01-28T00:01:28.313 回答