环境:
1.代码部署在内网git服务器上,简称git服务器。
2.个人办公机器,简称个人电脑
3.线上服务器
4.个人电脑通过ssh公钥联通git服务器以及线上服务器
需求:通过个人电脑把代码发布更新到线上服务器
一.个人电脑上的git操作。
在工作目录中初始化新仓库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
huwei@huwei:
/web/www
.kutongji.com$
mkdir
www.gits
huwei@huwei:
/web/www
.kutongji.com$
cd
huwei@huwei:
/web/www
.kutongji.com
/www
.gits$ git init
huwei@huwei:
/web/www
.kutongji.com
/www
.gits$ git status
On branch master
Initial commit
nothing to commit (create
/copy
files and use
"git add"
to track)
huwei@huwei:
/web/www
.kutongji.com
/www
.gits$ ll -a
total 12
drwxrwxr-x 3 huwei huwei 4096 7月 24 14:14 ./
drwxrwxr-x 4 huwei huwei 4096 7月 24 14:14 ../
drwxrwxr-x 7 huwei huwei 4096 7月 24 14:21 .git/
|
用户信息
第一个要配置的是你个人的用户名称和电子邮件地址。这两条配置很重要,每次 Git 提交时都会引用这两条信息,说明是谁提交了更新,所以会随更新内容一起被永久纳入历史记录:
1
2
|
huwei@huwei:
/web/www
.kutongji.com
/www
.gits$ git config --global user.name
"weihu"
huwei@huwei:
/web/www
.kutongji.com
/www
.gits$ git config --global user.email
"1072353300@qq.com"
|
查看配置信息
1
2
3
4
5
6
7
|
huwei@huwei:
/web/www
.kutongji.com
/www
.gits$ git config --list
user.name=weihu
user.email=1072353300@qq.com
core.repositoryformatversion=0
core.filemode=
true
core.bare=
false
core.logallrefupdates=
true
|
克隆Git服务器上代码到本地git目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
huwei@huwei:
/web/www
.kutongji.com
/www
.gits$ git clone git@gitxm.com:kutongji
/www_old
.git
Cloning into
'www_old'
...
remote: Counting objects: 31397,
done
.
remote: Compressing objects: 100% (8608
/8608
),
done
.
remote: Total 31397 (delta 20722), reused 31300 (delta 20671)
Receiving objects: 100% (31397
/31397
), 63.94 MiB | 10.93 MiB
/s
,
done
.
Resolving deltas: 100% (20722
/20722
),
done
.
Checking connectivity...
done
.
huwei@huwei:
/web/www
.kutongji.com
/www
.gits$ ll -a
total 16
drwxrwxr-x 4 huwei huwei 4096 7月 24 14:31 ./
drwxrwxr-x 4 huwei huwei 4096 7月 24 14:14 ../
drwxrwxr-x 7 huwei huwei 4096 7月 24 14:21 .git/
drwxrwxr-x 10 huwei huwei 4096 7月 24 14:31 www_old/
|
其中www_old文件为本地git服务器文件,即开发环境。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
huwei@huwei:
/web/www
.kutongji.com
/www
.gits$
cd
www_old/
huwei@huwei:
/web/www
.kutongji.com
/www
.gits
/www_old
$ ll -a
total 48
drwxrwxr-x 10 huwei huwei 4096 7月 24 14:31 ./
drwxrwxr-x 4 huwei huwei 4096 7月 24 14:31 ../
drwxrwxr-x 9 huwei huwei 4096 7月 24 14:31 application/
drwxrwxr-x 6 huwei huwei 4096 7月 24 14:31 data/
drwxrwxr-x 4 huwei huwei 4096 7月 24 14:31 docs/
drwxrwxr-x 8 huwei huwei 4096 7月 24 14:31 .git/
-rw-rw-r-- 1 huwei huwei 137 7月 24 14:31 .gitignore
drwxrwxr-x 3 huwei huwei 4096 7月 24 14:31 library/
drwxrwxr-x 7 huwei huwei 4096 7月 24 14:31 public/
-rwxrwxr-x 1 huwei huwei 1335 7月 24 14:31 README.md*
drwxrwxr-x 4 huwei huwei 4096 7月 24 14:31 scripts/
drwxrwxr-x 3 huwei huwei 4096 7月 24 14:31 tests/
huwei@huwei:
/web/www
.kutongji.com
/www
.gits
/www_old
$ git remote -
v
origin git@gitxm.com:kutongji
/www_old
.git (fetch)
origin git@gitxm.com:kutongji
/www_old
.git (push)
|
二.线上服务器操作
思路:线上服务器创建两个git目录A,B。其中A为git裸库,负责接受个人电脑的推送以及更新
目录B为代码发布区,clone目录A,在裸库更新后通过git pull获取更新,实现代码更新以及回滚。
1
|
git init --bare
|
创建裸库的操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
[root@localhost www.gits]
# git init --bare
Initialized empty Git repository
in
/web/www
.kutongji.com
/www
.gits/
[root@localhost www.gits]
# ll -a
total 40
drwxr-xr-x. 7 root root 4096 7月 24 14:43 .
drwxr-xr-x. 4 root root 4096 7月 24 14:40 ..
drwxr-xr-x. 2 root root 4096 7月 24 14:43 branches
-rw-r--r--. 1 root root 66 7月 24 14:43 config
-rw-r--r--. 1 root root 73 7月 24 14:43 description
-rw-r--r--. 1 root root 23 7月 24 14:43 HEAD
drwxr-xr-x. 2 root root 4096 7月 24 14:43 hooks
drwxr-xr-x. 2 root root 4096 7月 24 14:43 info
drwxr-xr-x. 4 root root 4096 7月 24 14:43 objects
drwxr-xr-x. 4 root root 4096 7月 24 14:43 refs
[root@localhost www.gits]
# git config --global user.name "weihu"
[root@localhost www.gits]
# git config --global user.email "1072353300@qq.com"
[root@localhost www.gits]
# git config --list
user.name=weihu
user.email=1072353300@qq.com
core.repositoryformatversion=0
core.filemode=
true
core.bare=
true
|
在个人电脑目录上操作
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
|
切换到代码目录:
huwei@huwei:/web/www.kutongji.com/www.gits/www_old$ cd ..
huwei@huwei:/web/www.kutongji.com/www.gits$ cd www_old/
查看目录关联的远程机,很明显因为从git服务器上clone,因此目录里面有一个主机是origin表示git主机
huwei@huwei:/web/www.kutongji.com/www.gits/www_old$ git remote -v
origin git@gitxm.com:kutongji/www_old.git (fetch)
origin git@gitxm.com:kutongji/www_old.git (push)
添加线上服务器主机,其中root@
192.168
.
66.35
:/web/www.kutongji.com/www.gits为裸库git地址,kutongji为一个主机别名,并非真实主机名称
huwei@huwei:/web/www.kutongji.com/www.gits$ git remote add kutongji root@
192.168
.
66.35
:/web/www.kutongji.com/www.gits
huwei@huwei:/web/www.kutongji.com/www.gits/www_old$ git remote -v
kutongji root@
192.168
.
66.35
:/web/www.kutongji.com/www.gits (fetch)
kutongji root@
192.168
.
66.35
:/web/www.kutongji.com/www.gits (push)
origin git@gitxm.com:kutongji/www_old.git (fetch)
origin git@gitxm.com:kutongji/www_old.git (push)
查看本地目录的分支
huwei@huwei:/web/www.kutongji.com/www.gits/www_old$ git branch
* develop
推送本地分支到线上服务器裸库
huwei@huwei:/web/www.kutongji.com/www.gits/www_old$ git push kutongji develop
Counting objects:
31357
, done.
Delta compression using up to
2
threads.
Compressing objects:
100
% (
8517
/
8517
), done.
Writing objects:
100
% (
31357
/
31357
),
63.93
MiB |
10.19
MiB/s, done.
Total
31357
(delta
20722
), reused
31357
(delta
20722
)
To root@
192.168
.
66.35
:/web/www.kutongji.com/www.gits
* [
new
branch] develop -> develop
|
1
2
|
推送成功
切换到线上服务器
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[root@localhost www.kutongji.com]
# ll -a
total 16
drwxr-xr-x. 4 root root 4096 7月 24 14:40 .
drwxr-xr-x. 3 root root 4096 7月 24 14:40 ..
drwxr-xr-x. 7 root root 4096 7月 24 14:43 www.gits
drwxr-xr-x. 2 root root 4096 7月 24 14:40 wwwroot
[root@localhost www.kutongji.com]
# cd wwwroot/
配置git库以及个人信息
[root@localhost wwwroot]
# git init
Initialized empty Git repository
in
/web/www
.kutongji.com
/wwwroot/
.git/
[root@localhost wwwroot]
# git config --global user.name "weihu"
[root@localhost wwwroot]
# git config --global user.email "1072353300@qq.com"
[root@localhost wwwroot]
# git config --list
user.name=weihu
user.email=1072353300@qq.com
core.repositoryformatversion=0
core.filemode=
true
core.bare=
false
core.logallrefupdates=
true
|
使用git remote add添加主机
1
2
3
4
|
[root@localhost wwwroot]
# git remote add kutongji /web/www.kutongji.com/www.gits
[root@localhost wwwroot]
# git remote -v
kutongji
/web/www
.kutongji.com
/www
.gits (fetch)
kutongji
/web/www
.kutongji.com
/www
.gits (push)
|
更新代码
1
2
3
4
5
6
7
8
|
[root@localhost wwwroot]
# git pull kutongji develop
remote: Counting objects: 31357,
done
.
remote: Compressing objects: 100% (8517
/8517
),
done
.
remote: Total 31357 (delta 20722), reused 31357 (delta 20722)
Receiving objects: 100% (31357
/31357
), 63.93 MiB | 18.00 MiB
/s
,
done
.
Resolving deltas: 100% (20722
/20722
),
done
.
From
/web/www
.kutongji.com
/www
.gits
* branch develop -> FETCH_HEAD
|
查看是否成功
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@localhost wwwroot]
# ll -a
total 48
drwxr-xr-x. 10 root root 4096 7月 24 15:26 .
drwxr-xr-x. 4 root root 4096 7月 24 15:23 ..
drwxr-xr-x. 9 root root 4096 7月 24 15:26 application
drwxr-xr-x. 6 root root 4096 7月 24 15:26 data
drwxr-xr-x. 4 root root 4096 7月 24 15:26 docs
drwxr-xr-x. 8 root root 4096 7月 24 15:26 .git
-rw-r--r--. 1 root root 137 7月 24 15:26 .gitignore
drwxr-xr-x. 3 root root 4096 7月 24 15:26 library
drwxr-xr-x. 7 root root 4096 7月 24 15:26 public
-rwxr-xr-x. 1 root root 1335 7月 24 15:26 README.md
drwxr-xr-x. 4 root root 4096 7月 24 15:26 scripts
drwxr-xr-x. 3 root root 4096 7月 24 15:26 tests
|
本文转自 yawei555 51CTO博客,原文链接:http://blog.51cto.com/huwei555/1678030,如需转载请自行联系原作者