如何从本地存储库推送到GitHub和实时服务器?

时间:2013-07-26 08:44:21

标签: git github

我是git和GitHub的新手但是我设置了一个本地存储库,当我输入时会更新GitHub存储库:

[username@yourhostname Program] git add .
[username@yourhostname Program] git status
[username@yourhostname Program] git commit -m "a message"
[username@yourhostname Program] git push -u origin master

我希望能够做到的另一件事是,当我进行更改并推送它们时,这些也会反映在实际站点的目录中。

我在实时服务器上安装了git,并设置了一个名为'testdir'的目录。

我很想在这个目录中输入'git init',但我也在网上看到'git init --bare'的引用并且有点困惑。

如果有人能提供一步一步的命令列表,我就会感激不尽,这样我就可以将本地存储库中的更改推送到GitHub和实时服务器。

3 个答案:

答案 0 :(得分:8)

这些是我的步骤并证明:

  • 将文本文件添加到本地存储库
  • 将这些更改(即新文件)推送到GitHub和实时服务器

以下假设您:

  • 已经在GitHub上设置了一个遥控器并且可以推送更改
  • 正在本地存储库中名为'Program_One'
  • 的目录中工作

此过程包括8个步骤:

  • 检查您是否具有对实时服务器的ssh访问权限
  • 在您的实时服务器上安装git
  • ‘testdir’
  • 处创建目录'yoursite.com/testdir'
  • 在该目录中创建名为'.git'
  • 的目录
  • 在该目录中创建一个“裸仓库”
  • '.git/hooks'中创建一个接收后文件并chmod其权限
  • 将实时服务器添加为远程服务器
  • 推送到实时服务器和GitHub

打开终端并输入以下内容ssh到您的实时服务器:

ssh root@your.ip.address.here # the password will be your root password

询问您的主机在您的服务器上安装git的首选方法

创建目录,裸仓库和收件后文件

[root@host /home/username/public_html] mkdir testdir
[root@host /home/username/public_html] cd testdir
[root@host /home/username/public_html/testdir] mkdir .git
[root@host /home/username/public_html/testdir] cd .git
[root@host /home/username/public_html/testdir/.git] git init --bare
[root@host /home/username/public_html/testdir/.git] cd hooks
[root@host /home/username/public_html/testdir/.git/hooks] vi post-receive
#  press 'i', paste the following 2 lines, replacing with your details
#!/bin/sh
GIT_WORK_TREE=/home/username/public_html/livetest git checkout -f
#  press 'esc', type :w, press enter, type shift+zz
[root@host /home/username/public_html/testdir/.git/hooks] chmod +x post-receive
[root@host /home/username/public_html/testdir/.git/hooks] exit
终端中的

,在您的本地存储库中,将您的实时服务器添加为远程服务器:

[username@yourhostname Program_One] # make sure you are in your local repository
[username@yourhostname Program_One] git remote add my_great_remote root@your.ip.address.here:/home/username/public_html/livetest/.git

# change ‘my_great_remote’ to the name you want to call your remote, taking note that the github remote is called ‘origin’.

将名为“my_text_file.txt”的文本文件添加到本地存储库,然后在终端中键入以下内容:

[username@yourhostname Program_One] # make sure you are in your local repository
[username@yourhostname Program_One] git add -all
[username@yourhostname Program_One] git status
[username@yourhostname Program_One] git commit -m "added text file"
[username@yourhostname Program_One] git push -u origin master
[username@yourhostname Program_One] git push -u my_great_remote master

接收后文件然后将本地存储库中的文件复制到“testdir”目录,允许您访问以下文本文件:

mysite.com/testdir/my_text_file.txt

答案 1 :(得分:2)

您需要在git配置中设置两个遥控器,一个用于github(这可能是您现在的“原点”),另一个用于您的其他服务器。像往常一样推送到github,然后使用git push yourserver推送到其他服务器。

如果我理解正确,你希望推送的更改在某个地方发布(无论这是否是一个好主意我都不会辩论) - 这可以通过添加一个“钩子”脚本来实现,该脚本在每个之后运行已经对服务器上的git存储库进行了推送。钩子可以例如将一些文件复制到你的www /目录。

有关添加遥控器(github和您的服务器)的信息,请参阅man git-remote 有关添加挂钩的信息,请参阅man githooks - 我认为收件后挂钩适合您。

答案 2 :(得分:0)

还有另一种解决此问题的方法 - 而不是使用自定义脚本来使用通过FTP / SFTP从GitHub存储库部署到服务器的托管服务。其中一项服务是dploy.io

基本上只需点击几下,您就可以将GitHub仓库链接到dploy.io,输入服务器详细信息后,您可以通过UI在每次推送或手动上进行自动部署。您还可以通过SFTP运行部署后的shell命令并触发Web挂钩。还有一种方法可以预览部署并在实时运行时进行观察。您还可以获得可以部署的目标服务的广泛列表 - 例如S3,Heroku,DreamObjects和很快的Elastic Beanstalk。

免责声明:我是背后的开发人员。