在服务器端创建一个git存储库(Create a git repository on server side)

编程入门 行业动态 更新时间:2024-10-23 04:57:36
在服务器端创建一个git存储库(Create a git repository on server side)

我有一个大问题,我无法理解这个话题。 我有一个网站服务器。 我用git init在那里创建了一个存储库。 比我做了一个git add *来将我服务器上的所有文件添加到存储库。 比我commit将所有文件commit到存储库。

比我用git clone ssh://username@mysite.com/wordpress/.git将它git clone ssh://username@mysite.com/wordpress/.git到我的本地客户端。

一切正常,我从我的项目中得到了一份副本。 不,我在我的本地版本上改变了一些内容,并通过push进行了commit 。 我查看了FileZilla,但文件中的内容没有改变。 在另一个方向,当我在服务器上更改了某些内容并将其pulled本地副本时,我看到了更改。 你知道我为什么在我的服务器上看不到我在本地副本上所做的更改吗?

感谢您的帮助!

I have a big problem and I can't understand this topic. I have a server with a website. I created a repository there with git init. Than I made a git add * to add all files from my server to the repository. Than I made a commit to commit all files to the repository.

Than I cloned it with git clone ssh://username@mysite.com/wordpress/.git to my local client.

All worked fine and I got a copy from my project. No I changed something on my local version and made a commit with a push. I looked in FileZilla but the content in the file don't changed. In the other direction when I changed something on the sever and pulled it to the local copy I saw the changes. Do you know why the changes which I made on the local copy are not visible on my sever?

Thank you for your help!

最满意答案

您需要将更改推送到本地计算机和服务器可以从中取出的中央存储库(或将它们作为远程添加到彼此)。 像GitHub这样的服务很适合这个。 以下是适用于此的完整工作流程的说明。 更新的说明可以在这个要点中找到。 此工作流程使用挂钩来完成繁重的工作,以便自动更新服务器。

使用Git管理实时网站

概观

作为一名自由职业者,我建立了很多网站。 这需要跟踪很多代码更改。 值得庆幸的是,具有适当分支的支持Git的工作流程可以缩短项目跟踪工作。 我可以很容易地看到分支机构中的开发功能以及站点生产代码的快照。 该工作流程的一个很好的补充是能够使用Git将更新推送到我提交更改时所处理的各个站点。

您需要在开发计算机上以及要托管网站的服务器或服务器上安装Git。 此过程甚至可以适用于多个服务器,例如负载均衡器后面的镜像。

设置无密码SSH访问

更新实时Web服务器的过程依赖于在Git环境中使用post hooks。 由于这是完全自动化的,因此在建立与远程服务器的SSH连接时无法输入登录凭据。 要解决此问题,我们将设置无密码SSH访问。 首先,您需要SSH到您的服务器。

ssh user@hostname

接下来,您需要确保在用户的主目录中有~/.ssh 。 如果没有,请继续创建一个。

mkdir ~/.ssh

在Mac和Linux上,您可以利用终端的强大功能一次性完成这两项任务。

if [ ! -d ~/.ssh ]; then mkdir ~/.ssh; fi

接下来,如果您还没有公钥,则需要生成公钥SSH密钥 。 列出~/.ssh目录中的文件进行检查。

ls -al ~/.ssh

您正在寻找的文件通常与id_rsa.pub类似。 如果您不确定,可以生成一个新的。 以下命令将使用提供的电子邮件作为标签创建SSH密钥。

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

您可能希望保留所有默认设置。 这将在之前创建的~/.ssh目录中创建一个名为id_rsa的文件。

出现提示时,请务必提供安全的SSH密码 。

如果必须创建SSH密钥,则需要配置ssh-agent程序以使用它。

ssh-add ~/.ssh/id_rsa

如果您知道自己在做什么,可以通过向ssh-agent提供私钥文件,在~/.ssh目录中使用现有的SSH密钥。

如果您仍然不确定发生了什么,您应该在~/.ssh目录中有两个与私钥和公钥文件对应的文件。 通常,公钥将是添加了.pub扩展名的同名文件。 一个示例是名为id_rsa的私钥文件和名为id_rsa.pub公钥文件。

在本地计算机上生成SSH密钥后,就可以将匹配的共享密钥文件放在服务器上了。

ssh user@hostname 'cat >> ~/.ssh/authorized_keys' < ~/.ssh/id_rsa.pub

这会将您的公钥添加到远程服务器上的授权密钥。 可以从每个开发机器重复该过程,以根据需要向服务器添加许多授权密钥。 当你关闭连接并重新连接而没有提示输入密码时,你就会知道你做得对了。


配置远程服务器存储库

您打算用作实时生产服务器的计算机需要具有可以写入适当的Web可访问目录的Git存储库。 Git元数据(.git目录)不需要位于Web可访问的位置。 相反,它可以是SSH用户可由用户编写的任何地方。

设置裸存储库

要将文件推送到Web服务器,您需要在Web服务器上拥有存储库的副本。 您将首先创建一个裸存储库来容纳您的网站。 应该在Web根目录之外的某处设置存储库。 我们将指示Git稍后将实际文件放在何处。 确定存储库的位置后,以下命令将创建裸存储库。

mkdir mywebsite.git cd mywebsite.git git init --bare

裸存储库包含所有没有任何HEAD的Git元数据。 从本质上讲,这意味着您的存储库具有.git目录,但没有签出任何工作文件。 下一步是创建一个Git钩子,它会在你指示它时检查这些文件。

如果您希望从分离的工作树中运行git命令,则需要在运行任何命令之前将环境变量GIT_DIR设置为mywebsite.git的路径。

添加接收后挂钩

使用以下内容在存储库的hooks目录中创建名为post-receive的文件。

#!/bin/sh GIT_WORK_TREE=/path/to/webroot/of/mywebsite git checkout -f

创建挂钩后,继续将其标记为可执行文件。

chmod +x hooks/post-receive

GIT_WORK_TREE允许您指示Git工作目录应该用于存储库的位置。 这允许您将存储库保留在Web根目录之外,并在Web可访问的位置使用分离的工作树。 确保您指定的路径存在,Git不会为您创建它。


配置本地开发机器

本地开发机器将容纳网站存储库。 只要您选择推送这些更改,相关文件就会被复制到实时服务器。 这意味着您应该在开发计算机上保留存储库的工作副本。 您还可以使用任何集中式存储库,包括基于云的存储库,如GitHub或BitBucket。 您的工作流程完全取决于您。 由于所有更改都是从本地存储库推送的,因此此过程不受您选择处理项目的方式的影响。

设置工作存储库

在您的开发机器上,您应该有一个可用的Git存储库。 如果没有,您可以使用以下命令在现有项目目录中创建。

git init git add -A git commit -m "Initial Commit"

添加指向Web服务器的远程存储库

拥有工作存储库后,您需要添加一个指向您在服务器上设置的遥控器的遥控器。

git remote add live ssh://server1.example.com/home/user/mywebsite.git

确保您提供的主机名和路径指向您先前设置的服务器和存储库。 最后,是时候将您当前的网站首次推送到实时服务器了。

git push live +master:refs/head/master

此命令指示Git将当前主分支推送到live远程分支。 (不需要发送任何其他分支。)将来,服务器只会从主分支中检出,因此您不需要每次都明确指定。


建立美好的东西

一切都准备好了。 现在是时候让创意果汁流淌! 您的工作流程根本不需要更改。 无论何时准备好,将更改推送到实时Web服务器就像运行以下命令一样简单。

git push live

在服务器receive.denycurrentbranch设置为“ignore”会消除当您将更新推送到服务器上的签出分支时由最新版本的Git发出的警告。


其他提示

在使用这种工作流程时,您可能会发现一些有用的提示和技巧。

推动对多个服务器的更改

您可能会发现需要推送到多个服务器。 也许您有多个测试服务器,或者您的实时站点在负载均衡器后面的多个服务器上进行镜像。 在任何情况下,推送到多个服务器就像在.git/config的[remote "live"]部分添加更多URL一样简单。

[remote "live"] url = ssh://server1.example.com/home/user/mywebsite.git url = ssh://server2.example.com/home/user/mywebsite.git

现在发出命令git push live将更新您一次添加的所有网址。 简单!

忽略对已跟踪文件的本地更改

您有时会发现存储库中存在要跟踪的文件,但每次更新网站时都不希望更改这些文件。 一个很好的例子是您的网站中的配置文件,其中包含特定于该站点所在服务器的设置。 推送更新到您的站点通常会使用开发计算机上的任何文件版本覆盖这些文件。 防止这种情况很容易。 SSH进入远程服务器并导航到Git存储库。 输入以下命令,列出要忽略的每个文件。

git update-index --assume-unchanged <file...>

这指示Git忽略对任何将来签出的指定文件的任何更改。 您可以在任何必要时随时撤消对一个或多个文件的此影响。

git update-index --no-assume-unchanged <file...>

如果您想查看被忽略文件的列表,那也很容易。

git ls-files -v | grep ^[a-z]

参考

使用Git部署您的网站更改 静态站点的简单Git部署策略 使用Git管理网站 忽略Git中对跟踪文件的本地更改

You need to push changes to a central repository that both your local machine and server can pull from (or add them as remotes for each other). A service such as GitHub works nicely for this. Here are instructions for a full workflow that works well for this. Updated instructions can be found in this gist. This workflow uses hooks to do the heavy lifting so that updates to your server are automated.

Using Git to Manage a Live Web Site

Overview

As a freelancer, I build a lot of web sites. That's a lot of code changes to track. Thankfully, a Git-enabled workflow with proper branching makes short work of project tracking. I can easily see development features in branches as well as a snapshot of the sites' production code. A nice addition to that workflow is that ability to use Git to push updates to any of the various sites I work on while committing changes.

You'll need to have Git installed on your development machines as well as on the server or servers where you wish to host your website. This process can even be adapted to work with multiple servers such as mirrors behind a load balancer.

Setting up Passwordless SSH Access

The process for updating a live web server relies on the use of post hooks within the Git environment. Since this is fully automated, there is no opportunity to enter login credentials while establishing the SSH connection to the remote server. To work around this, we are going to set up passwordless SSH access. To begin, you will need to SSH into your server.

ssh user@hostname

Next, you'll need to make sure you have a ~/.ssh in your user's home directory. If not, go ahead and create one now.

mkdir ~/.ssh

On Mac and Linux, you can harness the power of terminal to do both in one go.

if [ ! -d ~/.ssh ]; then mkdir ~/.ssh; fi

Next you'll need to generate a public SSH key if you don't already have one. List the files in your ~/.ssh directory to check.

ls -al ~/.ssh

The file you're looking for is usually named similarly to id_rsa.pub. If you're not sure, you can generate a new one. The command below will create an SSH key using the provided email as a label.

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

You'll probably want to keep all of the default settings. This will should create a file named id_rsa in the ~/.ssh directory created earlier.

When prompted, be sure to provide a secure SSH passphrase.

If you had to create an SSH key, you'll need to configure the ssh-agent program to use it.

ssh-add ~/.ssh/id_rsa

If you know what you are doing, you can use an existing SSH key in your ~/.ssh directory by providing the private key file to ssh-agent.

If you're still not sure what's going on, you should two files in your ~/.ssh directory that correspond to the private and public key files. Typically, the public key will be a file by the same name with a .pub extension added. An example would be a private key file named id_rsa and a public key file named id_rsa.pub.

Once you have generated an SSH key on your local machine, it's time to put the matching shared key file on the server.

ssh user@hostname 'cat >> ~/.ssh/authorized_keys' < ~/.ssh/id_rsa.pub

This will add your public key to the authorized keys on the remote server. This process can be repeated from each development machine to add as many authorized keys as necessary to the server. You'll know you did it correctly when you close your connection and reconnect without being prompted for a password.


Configuring the Remote Server Repository

The machine you intend to use as a live production server needs to have a Git repository that can write to an appropriate web-accessible directory. The Git metadata (the .git directory) does not need to be in a web-accessible location. Instead, it can be anywhere that is user-writeable by your SSH user.

Setting up a Bare Repository

In order to push files to your web server, you'll need to have a copy of your repository on your web server. You'll want to start by creating a bare repository to house your web site. The repository should be set up somewhere outside of your web root. We'll instruct Git where to put the actual files later. Once you decide on location for your repository, the following commands will create the bare repository.

mkdir mywebsite.git cd mywebsite.git git init --bare

A bare repository contains all of the Git metadata without any HEAD. Essentially, this means that your repository has a .git directory, but does not have any working files checked out. The next step is to create a Git hook that will check out those files any time you instruct it to.

If you wish to run git commands from the detached work tree, you'll need to set the environmental variable GIT_DIR to the path of mywebsite.git before running any commands.

Add a Post-Receive Hook

Create a file named post-receive in the hooks directory of your repository with the following contents.

#!/bin/sh GIT_WORK_TREE=/path/to/webroot/of/mywebsite git checkout -f

Once you create your hook, go ahead and mark it as executable.

chmod +x hooks/post-receive

GIT_WORK_TREE allows you to instruct Git where the working directory should be for a repository. This allows you to keep the repository outside of the web root with a detached work tree in a web accessible location. Make sure the path you specify exists, Git will not create it for you.


Configuring the Local Development Machine

The local development machine will house the web site repository. Relevant files will be copied to the live server whenever you choose to push those changes. This means you should keep a working copy of the repository on your development machine. You could also employ the use of any centralized repository including cloud-based ones such as GitHub or BitBucket. Your workflow is entirely up to you. Since all changes are pushed from the local repository, this process is not affected by how you choose to handle your project.

Setting up the Working Repository

On your development machine, you should have a working Git repository. If not, you can create on in an existing project directory with the following commands.

git init git add -A git commit -m "Initial Commit"

Add a Remote Repository Pointing to the Web Server

Once you have a working repository, you'll need to add a remote pointing to the one you set up on your server.

git remote add live ssh://server1.example.com/home/user/mywebsite.git

Make sure the hostname and path you provide point to the server and repository you set up previously. Finally, it's time to push your current website to the live server for the first time.

git push live +master:refs/head/master

This command instructs Git to push the current master branch to the live remote. (There's no need to send any other branches.) In the future, the server will only check out from the master branch so you won't need to specify that explicitly every time.


Build Something Beautiful

Everything is ready to go. It's time to let the creative juices flow! Your workflow doesn't need to change at all. Whenever you are ready, pushing changes to the live web server is as simple as running the following command.

git push live

Setting receive.denycurrentbranch to "ignore" on the server eliminates a warning issued by recent versions of Git when you push an update to a checked-out branch on the server.


Additional Tips

Here are a few more tips and tricks that you may find useful when employing this style of workflow.

Pushing Changes to Multiple Servers

You may find the need to push to multiple servers. Perhaps you have multiple testing servers or your live site is mirrored across multiple servers behind a load balancer. In any case, pushing to multiple servers is as easy as adding more urls to the [remote "live"] section in .git/config.

[remote "live"] url = ssh://server1.example.com/home/user/mywebsite.git url = ssh://server2.example.com/home/user/mywebsite.git

Now issuing the command git push live will update all of the urls you've added at one time. Simple!

Ignoring Local Changes to Tracked Files

From time to time you'll find there are files you want to track in your repository but don't wish to have changed every time you update your website. A good example would be configuration files in your web site that have settings specific to the server the site is on. Pushing updates to your site would ordinarily overwrite these files with whatever version of the file lives on your development machine. Preventing this is easy. SSH into the remote server and navigate into the Git repository. Enter the following command, listing each file you wish to ignore.

git update-index --assume-unchanged <file...>

This instructs Git to ignore any changes to the specified files with any future checkouts. You can reverse this effect on one or more files any time you deem necessary.

git update-index --no-assume-unchanged <file...>

If you want to see a list of ignored files, that's easy too.

git ls-files -v | grep ^[a-z]

References

Deploy Your Website Changes Using Git A simple Git deployment strategy for static sites Using Git to manage a website Ignoring Local Changes to Tracked Files in Git

更多推荐

本文发布于:2023-08-06 09:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1446667.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:创建一个   服务器端   git   Create   server

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!