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,如需转载请自行联系原作者
相关文章
|
10月前
|
API 开发工具 git
git常用的API以及每个的应用场景
【4月更文挑战第5天】Git是流行的分布式版本控制系统,用于代码管理,提供丰富的API。本文概述了Git常用API,如`git init`(初始化仓库)、`git add`(添加到暂存区)、`git commit`(提交)、`git remote add origin`(添加远程仓库)、`git pull`和`push`(同步远程仓库)、`git branch`(分支管理)以及`git checkout`(切换分支或恢复文件)。了解和熟练使用这些API能提升开发效率和代码质量,更多Git功能可参考官方文档。
491 0
|
10月前
|
测试技术 持续交付 开发工具
《Git 简易速速上手小册》第6章:Git 在持续集成/持续部署(CI/CD)中的应用(2024 最新版)
《Git 简易速速上手小册》第6章:Git 在持续集成/持续部署(CI/CD)中的应用(2024 最新版)
153 2
|
测试技术 网络安全 开发工具
Git系列之分支与标签的使用及应用场景模拟
Git系列之分支与标签的使用及应用场景模拟
161 0
|
测试技术 Linux 开发工具
Git之分支与版本->课程目标及知识点的应用场景,分支的场景应用,标签的场景应用
Git之分支与版本->课程目标及知识点的应用场景,分支的场景应用,标签的场景应用
93 0
|
4月前
|
Linux 网络安全 开发工具
Git学习笔记(一):基础与应用
本文档详细介绍了如何将本地项目关联到Gitee上的空仓库并上传代码,以及如何验证本机与Git服务器的SSH连接。同时,还概述了Git的基本概念、安装步骤、初始配置、常见命令及如何配置多个SSH-Key,适用于初学者快速上手Git操作。
156 51
Git学习笔记(一):基础与应用
|
7月前
|
存储 Linux 开发工具
【Azure App Service】本地Git部署Python Flask应用上云(Azure App Service For Linux)关键错误
【Azure App Service】本地Git部署Python Flask应用上云(Azure App Service For Linux)关键错误
|
7月前
|
jenkins 测试技术 开发工具
协同开发的艺术:Git 在团队项目中的高效应用
【8月更文第16天】在现代软件开发中,团队成员之间的高效协作是至关重要的。Git 作为一种分布式版本控制系统,为开发者提供了强大的工具来管理代码的变化和协作。本文将介绍如何利用 Git 来优化团队的工作流程,并提供实际操作的代码示例。
238 1
|
9月前
|
前端开发 持续交付 开发工具
详细介绍Git的基本原理、在前端开发中的应用以及如何使用Git来优化团队协作
【6月更文挑战第14天】Git是前端开发中的必备工具,它通过分布式版本控制管理代码历史,支持分支、合并和冲突解决,促进团队协作。在前端开发中,Git用于代码追踪、版本控制、代码审查和持续集成部署,优化团队协作。制定分支策略、编写清晰提交信息、定期合并清理分支以及使用Git钩子和自动化工具能进一步提升效率。理解并善用Git,能有效提升前端项目的质量和开发效率。
103 3
|
9月前
|
中间件 Java 生物认证
Web应用&源码泄漏&开源闭源&指纹识别&GIT&SVN&DS&备份
Web应用&源码泄漏&开源闭源&指纹识别&GIT&SVN&DS&备份
|
10月前
|
前端开发 持续交付 开发工具
【专栏:工具与技巧篇】版本控制与Git在前端开发中的应用
【4月更文挑战第30天】Git是前端开发中的必备工具,它通过分布式版本控制管理代码历史,支持分支、合并、回滚等操作,促进团队协作和冲突解决。在前端项目中,Git用于代码追踪、代码审查、持续集成与部署,提升效率和质量。优化协作包括制定分支策略、编写清晰提交信息、定期合并清理分支及使用Git钩子和自动化工具。掌握Git能有效提升开发效率和代码质量。
146 2

相关实验场景

更多