使用Git替代由FTP上传更新代码

单位以前服务器代码更新使用的是FTP上传,这样无法保证服务器代码和仓库代码保持一致,忍不了了,果断改成git拉取

第一步,有代码的管理仓库

第二步,拉取服务器的代码到本地,然后再提交到代码仓库,保持当前服务器和代码库文件和文件内容一致

第三步,在服务器上安装git,并生成公钥

1
# ssh-keygen -t rsa

第四步,把服务器的公钥配置到代码库的部署公钥管理中

第五步,在服务器上代码根目录初始化git

1
2
[root@localhost project]# git init
Initialized empty Git repository in /data/www/html/project/.git/

第六步,添加远程代码库地址

1
[root@localhost project]# git remote add origin git@gitee.com:xxxxx/project.git

第七步,拉取代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@localhost project]# git pull
remote: Enumerating objects: 894, done.
remote: Counting objects: 100% (894/894), done.
remote: Compressing objects: 100% (824/824), done.
remote: Total 4211 (delta 380), reused 370 (delta 55), pack-reused 3317
Receiving objects: 100% (4211/4211), 42.83 MiB | 9.86 MiB/s, done.
Resolving deltas: 100% (1953/1953), done.
From gitee.com:xxxxx/project
* [new branch] master -> origin/master
You asked me to pull without telling me which branch you
want to merge with, and 'branch.master.merge' in
your configuration file does not tell me, either. Please
specify which branch you want to use on the command line and
try again (e.g. 'git pull <repository> <refspec>').
See git-pull(1) for details.

If you often merge with the same branch, you may want to
use something like the following in your configuration file:

[branch "master"]
remote = <nickname>
merge = <remote-ref>

[remote "<nickname>"]
url = <url>
fetch = <refspec>

See git-config(1) for details.
You have new mail in /var/spool/mail/root

第八步,本地关联远程分支

1
2
[root@localhost project]# git branch --set-upstream master origin/master
Branch master set up to track remote branch master from origin.

这样就可以了,然后在执行 git pull 看一下

1
2
[root@localhost project]# git pull
Already up-to-date.

更新已完成,这样更新代码是不是很爽呢O(∩_∩)O哈哈~

坚持原创技术分享,您的支持将鼓励我继续创作!
0%