git的日常应用

简介:

环境:

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,如需转载请自行联系原作者
相关文章
|
24天前
|
测试技术 持续交付 开发工具
《Git 简易速速上手小册》第6章:Git 在持续集成/持续部署(CI/CD)中的应用(2024 最新版)
《Git 简易速速上手小册》第6章:Git 在持续集成/持续部署(CI/CD)中的应用(2024 最新版)
42 2
|
存储 安全 前端开发
QT应用编程: QtCreator配置Git版本控制(码云)
QT应用编程: QtCreator配置Git版本控制(码云)
587 0
QT应用编程: QtCreator配置Git版本控制(码云)
|
13天前
|
前端开发 持续交付 开发工具
【专栏:工具与技巧篇】版本控制与Git在前端开发中的应用
【4月更文挑战第30天】Git是前端开发中的必备工具,它通过分布式版本控制管理代码历史,支持分支、合并、回滚等操作,促进团队协作和冲突解决。在前端项目中,Git用于代码追踪、代码审查、持续集成与部署,提升效率和质量。优化协作包括制定分支策略、编写清晰提交信息、定期合并清理分支及使用Git钩子和自动化工具。掌握Git能有效提升开发效率和代码质量。
|
15天前
|
开发工具 git 开发者
【专栏】探讨了 Git 中的 `git rebase` 操作,它用于重新应用提交到另一分支,改变历史顺序
【4月更文挑战第29天】本文探讨了 Git 中的 `git rebase` 操作,它用于重新应用提交到另一分支,改变历史顺序。与 `git merge` 不同,rebase 重写提交历史,提供简洁线性的历史记录。文章介绍了 rebase 的基本操作、应用场景,如整理提交历史、解决冲突和整合分支,并强调了使用注意事项,如避免在公共分支上操作。尽管 rebase 可以带来整洁的历史和冲突解决便利,但其潜在的风险和可能导致的历史混乱需谨慎对待。理解并恰当使用 `git rebase` 可以提升开发效率和代码质量。
|
2月前
|
开发工具 git
Git命令大全:从基础到高级应用
Git命令大全:从基础到高级应用
|
6月前
|
安全 测试技术 开发工具
【Git】Git分支与应用分支&Git标签与应用标签
【Git】Git分支与应用分支&Git标签与应用标签
76 0
|
Shell Linux 网络安全
Git学习笔记(git应用)
Git学习笔记(git应用)
Git学习笔记(git应用)
|
开发工具 git
Git----Git远端仓库地址管理命令应用
Git----Git远端仓库地址管理命令应用
156 0
|
开发工具 git
Git----Git命令rebase应用
Git----Git命令rebase应用
147 0
Git----Git命令rebase应用
|
开发工具 git
Git----Git命令stash应用
Git----Git命令stash应用
201 0