git部署

一.git简介

Git是一个免费的开源分布式版本控制系统,旨在快速高效地处理从小型到大型项目的所有涉及的内容。
Git 易于学习, 占地面积小,具有闪电般的快速性能。它具有诸如Subversion,CVS,Perforce和ClearCase之类的SCM工具,并且具有廉价的本地分支,方便的暂存区域和 多个工作流等功能。了解更多功能请参考官方链接:https://git-scm.com/

二.部署并配置git。

1.yum安装git

#安装git
[root@git ~]# yum -y install git
#查看git的安装版本
[root@git ~]# git --version
git version 1.8.3.1

2.git全局设置

#方法一
[root@git ~]# git config --global user.name "weiaixiong"
[root@git ~]# git config --global user.email "123456789@qq.com"  #这里写入你自己的邮箱地址,以便后期接收邮件提醒
[root@git ~]# git config --global color.ui   "true"
#方法二
[root@git ~]# vim /root/.gitconfig 
[user]
        name = weiaixiong
        email = 123456789@qq.com
[color]
        ui = true
#对应修改保存退出即可。

3.创建一个新的本地git库

#新建一个本地目录
[root@git ~]# cd /opt/
[root@git /opt]# mkdir weiaixiong
#初始化版本库
[root@git /opt/weiaixiong]# git init
Initialized empty Git repository in /opt/weiaixiong/.git/
[root@git /opt/weiaixiong]# ls -a
.  ..  .git
[root@git /opt/weiaixiong]# ll .git
total 28
drwxr-xr-x  2 root root   6 Nov 20 19:45 branches
-rw-r--r--  1 root root  30 Nov 20 20:42 COMMIT_EDITMSG
-rw-r--r--  1 root root 468 Nov 21 10:22 config
-rw-r--r--  1 root root  73 Nov 20 19:45 description
-rw-r--r--  1 root root  98 Nov 21 10:25 FETCH_HEAD
-rw-r--r--  1 root root  23 Nov 20 19:45 HEAD
drwxr-xr-x  2 root root 242 Nov 20 19:45 hooks
-rw-r--r--  1 root root 406 Nov 24 20:33 index
drwxr-xr-x  2 root root  21 Nov 20 19:45 info
drwxr-xr-x  3 root root  30 Nov 20 20:09 logs
drwxr-xr-x 20 root root 190 Nov 20 22:26 objects
-rw-r--r--  1 root root  41 Nov 20 20:57 ORIG_HEAD
drwxr-xr-x  5 root root  46 Nov 21 08:29 refs
#隐藏文件的介绍:.git
branches 	 # 分区目录
config		 # 定义项目特有的配置选项
description  # 仅供git web程序使
HEAD 	     # 指定当前的分支
hooks		 # 包含git的钩子文件
info		 # 包含一个全局排除文件(exclude文件)
objects 	 # 存放所有数据内容,有info和pack两个子文件夹
refs 		 # 存放指向数据(分支)的提交对象的指
index 		 # 保存暂存区信息,在执行git init的时候,这个文件还没有

三.git的常规使用

1.创建数据-提交数据

2.git的四种状态

3.git基础命令使用

这里在本地版本库中创建5个txt文件具体操作如下:

[root@git /opt/weiaixiong]# for i in `seq 5`;do echo $i >$i.txt;done
[root@git /opt/weiaixiong]# ls
1.txt  2.txt  3.txt  4.txt  5.txt
[root@git /opt/weiaixiong]# cat *.txt
1
2
3
4
5
1)添加文件到缓冲区上传到本地仓库

命令格式:
git add a 添加文件到暂存区域

#添加文件到缓存区
[root@git /opt/weiaixiong]# git add 1.txt #添加单个文件
#查看添加的结果
[root@git /opt/weiaixiong]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   1.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	2.txt
#	3.txt
#	4.txt
#	5.txt
#批量添加文件到缓冲区
[root@git /opt/weiaixiong]# git add .
#查看git状态
[root@git /opt/weiaixiong]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   1.txt
#	new file:   2.txt
#	new file:   3.txt
#	new file:   4.txt
#	new file:   5.txt
#添加文件到本地git仓库
命令格式:
git commit -m "说明内容"
[root@git /opt/weiaixiong]# git commit -m "第一次提交数据"
[master (root-commit) fe0dd0c] 第一次提交数据
 5 files changed, 5 insertions(+)
 create mode 100644 1.txt
 create mode 100644 2.txt
 create mode 100644 3.txt
 create mode 100644 4.txt
 create mode 100644 5.txt
2)撤回缓冲区文件以及删除撤回文件
#撤回缓存区的文件之后删除
命令格式:
git rm --cached a 撤出暂存区的文件
[root@git /opt/weiaixiong]# git rm --cached 1.txt
rm '1.txt'
[root@git /opt/weiaixiong]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   2.txt
#	new file:   3.txt
#	new file:   4.txt
#	new file:   5.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	1.txt
[root@git /opt/weiaixiong]# rm -f 1.txt
#直接执行撤回并删除
命令格式:
git rm -f 同时删除工作区域和暂存区域
[root@git /opt/weiaixiong]# git rm -f 1.txt

2.修改文件名并提交

命令格式:
git mv a b

[root@git /opt/weiaixiong]# git mv 1.txt 6.txt
[root@git /opt/weiaixiong]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	renamed:    1.txt -> 6.txt
#
[root@git /opt/weiaixiong]# ls
2.txt  3.txt  4.txt  5.txt  6.txt
[root@git /opt/weiaixiong]# git commit -m "第一次修改文件名"
[master e62459c] 第一次修改文件名
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename 1.txt => 6.txt (100%)

3.git文件对比

命令格式:
git diff 比对工作目录和暂存区的不同
git diff --cached 比对暂存区和本地仓库的不同
说明:这里追加一些内容至6.txt文件中,具体参考如下:

[root@git /opt/weiaixiong]# vim 6.txt
1
2
3
4
保存退出
#本地目录和缓存区文件对比
[root@git /opt/weiaixiong]# git diff 6.txt
diff --git a/6.txt b/6.txt
index d00491f..94ebaf9 100644
--- a/6.txt
+++ b/6.txt
@@ -1 +1,4 @@
 1
+2
+3
+4
#将变化的文件添加到缓存区,之后对比缓存区文件和仓库文件的对比
[root@git /opt/weiaixiong]# git add 6.txt
[root@git /opt/weiaixiong]# git diff --cached 6.txt
diff --git a/6.txt b/6.txt
index d00491f..94ebaf9 100644
--- a/6.txt
+++ b/6.txt
@@ -1 +1,4 @@
 1
+2
+3
+4

4.git仓库实现回退功能

说明:这里先将变化的文件重新提交至仓库

#提交数据至仓库
[root@git /opt/weiaixiong]# git commit -m "第一次追加数据至6.txt"
[master b45c55f] 第一次追加数据至6.txt
 1 file changed, 3 insertions(+)
#查看历史的版本
命令格式:
git log
[root@git /opt/weiaixiong]# git log --oneline  #只显示一条提交信息
b45c55f 第一次追加数据至6.txt
e62459c 第一次修改文件名
fe0dd0c 第一次提交数据
#查看当前的指针
[root@git /opt/weiaixiong]# git log --oneline --decorate
b45c55f (HEAD, tag: v1.5, origin1/master, origin/master, master) 第一次追加数据至6.txt
e62459c 第一次修改文件名
fe0dd0c 第一次提交数据
#回退前查看内容
[root@git /opt/weiaixiong]# cat 6.txt
1
2
3
4
#执行回退到第一修改文件名状态
[root@git /opt/weiaixiong]# git reset --hard e62459c
HEAD is now at e62459c 第一次修改文件名
#查看回退的结果
[root@git /opt/weiaixiong]# cat 6.txt
1
#查看历史所有的系统版本
[root@git /opt/weiaixiong]# git reflog
e62459c HEAD@{0}: reset: moving to e62459c
b45c55f HEAD@{1}: commit: 第一次追加数据至6.txt
e62459c HEAD@{2}: commit: 第一次修改文件名
fe0dd0c HEAD@{3}: commit (initial): 第一次提交数据
注意:这里还是有之前的记录的,如果你又误操作或其他需求还需回到之前的版本,就可以再执行以下回撤即可
[root@git /opt/weiaixiong]# git reset --hard b45c55f
HEAD is now at b45c55f 第一次追加数据至6.txt
[root@git /opt/weiaixiong]# cat 6.txt
1
2
3
4

撤销修改的文件这里只是在本地提交的缓存区的文件修改错误了,撤销相应的设置操作。如果已经提交到仓库中,只能按上面的撤回到历史版本。

#在2.txt中添加如下
[root@git /opt/weiaixiong]# vim 2.txt
2
3
3
4
#之后将文件提交到缓存区,然后对比查看数据的变化
[root@git /opt/weiaixiong]# git add 2.txt
[root@git /opt/weiaixiong]# git diff 2.txt
[root@git /opt/weiaixiong]# git diff --cached 2.txt
diff --git a/2.txt b/2.txt
index 0cfbf08..c0a7407 100644
--- a/2.txt
+++ b/2.txt
@@ -1 +1,4 @@
 2
+3
+3
+4
[root@git /opt/weiaixiong]# git reset HEAD 2.txt
Unstaged changes after reset:
M	2.txt
[root@git /opt/weiaixiong]# git diff 2.txt
diff --git a/2.txt b/2.txt
index 0cfbf08..c0a7407 100644
--- a/2.txt
+++ b/2.txt
@@ -1 +1,4 @@
 2
+3
+3
+4
[root@git /opt/weiaixiong]# git diff --cached 2.txt
#本地撤销修改,这个可以是回撤缓存后修改也可以是本地修改未上传至缓存前修改。
[root@git /opt/weiaixiong]# git checkout 2.txt
[root@git /opt/weiaixiong]# cat 2.txt
2

5.git标签管理

标签给commit起一个容易记的别名,每一次提交都会产生一个新的commit的id,这样的话就会出现无法分辨的情况,这里为每个版本设置标签,以便分辨。默认情况是没有标签的。

#默认是没有标签
[root@git /opt/weiaixiong]# git tag
#为当前的版本的添加标签
[root@git /opt/weiaixiong]# git tag v1.5
[root@git /opt/weiaixiong]# git tag
v1.5
[root@git /opt/weiaixiong]# git show v1.5 --oneline
b45c55f 第一次追加数据至6.txt
#这里也可以为历史绑定标签
#查看版本信息
[root@git /opt/weiaixiong]# git log --oneline
b45c55f 第一次追加数据至6.txt
e62459c 第一次修改文件名
fe0dd0c 第一次提交数据
#给任意版本打标签
[root@git /opt/weiaixiong]# git tag  -a v1.0  -m  '第一次修改文件名'  e62459c
[root@git /opt/weiaixiong]# git tag
v1.0
v1.5
#删除标签
[root@git /opt/weiaixiong]# git tag -d v1.0
Deleted tag 'v1.0' (was fb87f28)

四.git远程仓库gitee

这里需要自行注册一个码云的账号,链接地址请参考:https://gitee.com/
根据提示自行创建仓库即可。
之后可以参看以下步骤操作。
具体可以参考以下图片提示:

这个是官方的简易教程

简易的命令行入门教程:
Git 全局设置:

git config --global user.name "云海禅心"
git config --global user.email "5350617+123@user.noreply.gitee.com"
创建 git 仓库:

mkdir only_bear
cd only_bear
git init
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin https://gitee.com/123/only_bear.git #创建上传的用户,默认的是origin,修改用户名,直接更换名称即可,这里如果git全局设置的用户名等变了,你就需要新建一个新的上传用户。下面就是如此操作的。
git push -u origin master
已有仓库?

cd existing_git_repo
git remote add origin https://gitee.com/123/only_bear.git
git push -u origin master

这里使用ssh的方式进行上传,这里使用秘钥的方式上传,这样就需要先上传秘钥到服务端,具体操作请参考图片操作


测试上传并下载

#git全局设置
[root@git /opt/weiaixiong]# git config --global user.name "云海禅心"
[root@git /opt/weiaixiong]# git config --global user.email "5350617+123@user.noreply.gitee.com"
[root@git /opt/weiaixiong]# git remote add origin1 git@gitee.com:123/only_bear.git
[root@git /opt/weiaixiong]# git push -u origin1 master
Counting objects: 12, done.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (12/12), 914 bytes | 0 bytes/s, done.
Total 12 (delta 2), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-3.8]
To git@gitee.com:gftfyfgf/only_bear.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin1.
[root@git /opt/weiaixiong]# git push -u origin1 --tags
Total 0 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-3.8]
To git@gitee.com:gftfyfgf/only_bear.git
 * [new tag]         v1.5 -> v1.5
[root@git /opt/weiaixiong]# git pull -u origin1 --tags
Fetching tags only, you probably meant:
  git fetch --tags

查看gitee的仓库数据

五.git分支管理

命令格式:
git branch 查看git分支
git brach 分支名称 创建分支
git checkout -b 分支名称 创建并切换到新的分支
git checkout 分支名称 切换分支
git branch -d 分支名称 删除分支

1.git分支创建与删除

#查看分支
[root@git /opt/weiaixiong]# git branch
* master
#创建分支
[root@git /opt/weiaixiong]# git branch test
[root@git /opt/weiaixiong]# git branch
* master
  test
#切换分支
[root@git /opt/weiaixiong]# git checkout test
M	2.txt
Switched to branch 'test'
[root@git /opt/weiaixiong]# ll
total 20
-rw-r--r-- 1 root root 8 Nov 20 21:51 2.txt
-rw-r--r-- 1 root root 2 Nov 20 19:49 3.txt
-rw-r--r-- 1 root root 2 Nov 20 19:49 4.txt
-rw-r--r-- 1 root root 2 Nov 20 19:49 5.txt
-rw-r--r-- 1 root root 8 Nov 20 21:34 6.txt
#删除分支
[root@git /opt/weiaixiong]# git branch -d test
Deleted branch test (was b45c55f)

2.git分支合并

1)没有冲突的合并
[root@git /opt/weiaixiong]# touch aaa
[root@git /opt/weiaixiong]# git add .
[root@git /opt/weiaixiong]# git commit -m "add new file"
[master 31a5a60] add new file
 2 files changed, 3 insertions(+)
 create mode 100644 aaa

[root@git /opt/weiaixiong]# git log --oneline --decorate
31a5a60 (HEAD, master) add new file
b45c55f (tag: v1.5, origin1/master, origin/master, test) 第一次追加数据至6.txt
e62459c 第一次修改文件名
fe0dd0c 第一次提交数据

[root@git /opt/weiaixiong]# touch bbb
[root@git /opt/weiaixiong]# git add .
[root@git /opt/weiaixiong]# git commit -m "add second file"
[master e009bf7] add second file
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 bbb
[root@git /opt/weiaixiong]# git log --oneline --decorate
e009bf7 (HEAD, master) add second file
31a5a60 add new file
b45c55f (tag: v1.5, origin1/master, origin/master, test) 第一次追加数据至6.txt
e62459c 第一次修改文件名
fe0dd0c 第一次提交数据

#创建并切换分支
[root@git /opt/weiaixiong]# git checkout -b test
Switched to a new branch 'test'

[root@git /opt/weiaixiong]# git branch
  master
* test

[root@git /opt/weiaixiong]# git log --oneline --decorate
e009bf7 (HEAD, test, master) add second file
31a5a60 add new file
b45c55f (tag: v1.5, origin1/master, origin/master) 第一次追加数据至6.txt
e62459c 第一次修改文件名
fe0dd0c 第一次提交数据

[root@git /opt/weiaixiong]# touch testing
[root@git /opt/weiaixiong]# git add .
[root@git /opt/weiaixiong]# git commit -m "tets add first file"
[test 2541660] tets add first file
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 testing

[root@git /opt/weiaixiong]# git log --oneline --decorate
2541660 (HEAD, test) tets add first file
e009bf7 (master) add second file
31a5a60 add new file
b45c55f (tag: v1.5, origin1/master, origin/master) 第一次追加数据至6.txt
e62459c 第一次修改文件名
fe0dd0c 第一次提交数据

[root@git /opt/weiaixiong]# git checkout master 
Switched to branch 'master'
Your branch is ahead of 'origin1/master' by 2 commits.
  (use "git push" to publish your local commits)

[root@git /opt/weiaixiong]# git log --oneline --decorate
e009bf7 (HEAD, master) add second file
31a5a60 add new file
b45c55f (tag: v1.5, origin1/master, origin/master) 第一次追加数据至6.txt
e62459c 第一次修改文件名
fe0dd0c 第一次提交数据

[root@git /opt/weiaixiong]# touch master
[root@git /opt/weiaixiong]# git add .
[root@git /opt/weiaixiong]# git commit -m "master add first file"
[master 94e3c27] master add first file
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 master
[root@git /opt/weiaixiong]# git log --oneline --decorate
94e3c27 (HEAD, master) master add first file
e009bf7 add second file
31a5a60 add new file
b45c55f (tag: v1.5, origin1/master, origin/master) 第一次追加数据至6.txt
e62459c 第一次修改文件名
fe0dd0c 第一次提交数据

[root@git /opt/weiaixiong]# git checkout test
Switched to branch 'test'
[root@git /opt/weiaixiong]# git log --oneline --decorate
2541660 (HEAD, test) tets add first file
e009bf7 add second file
31a5a60 add new file
b45c55f (tag: v1.5, origin1/master, origin/master) 第一次追加数据至6.txt
e62459c 第一次修改文件名
fe0dd0c 第一次提交数据

[root@git /opt/weiaixiong]# git checkout master 
Switched to branch 'master'
Your branch is ahead of 'origin1/master' by 3 commits.
  (use "git push" to publish your local commits)
[root@git /opt/weiaixiong]# git merge test
Merge branch 'test'

# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
~                                                                                                                             
~                                                                                                                             
~                                                                                                                             
~                                                                                                                             
~                                                                                                                             
~                                                                                                                             
~                                                                                                                             
~                                                                                                                             
~                                                                                                                             
~                                                                                                                             
~                                                                                                                             
~                                                                                                                             
~                                                                                                                             
~                                                                                                                             
".git/MERGE_MSG" 7L, 247C written
Merge made by the 'recursive' strategy.
 testing | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 testing

[root@git /opt/weiaixiong]# ll
total 20
-rw-r--r-- 1 root root 8 Nov 20 21:51 2.txt
-rw-r--r-- 1 root root 2 Nov 20 19:49 3.txt
-rw-r--r-- 1 root root 2 Nov 20 19:49 4.txt
-rw-r--r-- 1 root root 2 Nov 20 19:49 5.txt
-rw-r--r-- 1 root root 8 Nov 20 21:34 6.txt
-rw-r--r-- 1 root root 0 Nov 26 13:49 aaa
-rw-r--r-- 1 root root 0 Nov 26 13:52 bbb
-rw-r--r-- 1 root root 0 Nov 26 14:17 master
-rw-r--r-- 1 root root 0 Nov 26 14:21 testing
[root@git /opt/weiaixiong]# git checkout test 
Switched to branch 'test'
[root@git /opt/weiaixiong]# git log --oneline --decorate
2541660 (HEAD, test) tets add first file
e009bf7 add second file
31a5a60 add new file
b45c55f (tag: v1.5, origin1/master, origin/master) 第一次追加数据至6.txt
e62459c 第一次修改文件名
fe0dd0c 第一次提交数据

[root@git /opt/weiaixiong]# git checkout master 
Switched to branch 'master'
Your branch is ahead of 'origin1/master' by 5 commits.
  (use "git push" to publish your local commits)
[root@git /opt/weiaixiong]# git branch -d test
Deleted branch test (was 2541660).
2)git分支存在冲突的合并(这里如果是有代码冲突可以根据具体的需求进行合并和删除)
[root@git /opt/weiaixiong]# git branch
* master

[root@git /opt/weiaixiong]# git branch test
[root@git /opt/weiaixiong]# git branch
* master
  test

[root@git /opt/weiaixiong]# echo master >>aaa
[root@git /opt/weiaixiong]# git add .
[root@git /opt/weiaixiong]# git commit -m "master first add file"
[master 18c5805] master first add file
 1 file changed, 1 insertion(+)
[root@git /opt/weiaixiong]# git log --oneline --decorate
18c5805 (HEAD, master) master first add file
7caf0be (test) Merge branch 'test'
94e3c27 master add first file
2541660 tets add first file
e009bf7 add second file
31a5a60 add new file
b45c55f (tag: v1.5, origin1/master, origin/master) 第一次追加数据至6.txt
e62459c 第一次修改文件名
fe0dd0c 第一次提交数据

[root@git /opt/weiaixiong]# git checkout test
Switched to branch 'test'

[root@git /opt/weiaixiong]# ll
total 20
-rw-r--r-- 1 root root 8 Nov 20 21:51 2.txt
-rw-r--r-- 1 root root 2 Nov 20 19:49 3.txt
-rw-r--r-- 1 root root 2 Nov 20 19:49 4.txt
-rw-r--r-- 1 root root 2 Nov 20 19:49 5.txt
-rw-r--r-- 1 root root 8 Nov 20 21:34 6.txt
-rw-r--r-- 1 root root 0 Nov 26 14:35 aaa
-rw-r--r-- 1 root root 0 Nov 26 13:52 bbb
-rw-r--r-- 1 root root 0 Nov 26 14:25 master
-rw-r--r-- 1 root root 0 Nov 26 14:21 testing

[root@git /opt/weiaixiong]# cat aaa
[root@git /opt/weiaixiong]# echo test >>aaa
[root@git /opt/weiaixiong]# git add .
[root@git /opt/weiaixiong]# git commit -m "test first add new file"
[test 5f5f63c] test first add new file
 1 file changed, 1 insertion(+)
[root@git /opt/weiaixiong]# git log --oneline --decorate
5f5f63c (HEAD, test) test first add new file
7caf0be Merge branch 'test'
94e3c27 master add first file
2541660 tets add first file
e009bf7 add second file
31a5a60 add new file
b45c55f (tag: v1.5, origin1/master, origin/master) 第一次追加数据至6.txt
e62459c 第一次修改文件名
fe0dd0c 第一次提交数据

[root@git /opt/weiaixiong]# git merge test
Auto-merging aaa
CONFLICT (content): Merge conflict in aaa
Automatic merge failed; fix conflicts and then commit the result.

[root@git /opt/weiaixiong]# cat aaa
<<<<<<< HEAD
master
=======
test
>>>>>>> test
[root@git /opt/weiaixiong]# vim aaa
<<<<<<< HEAD
master
=======
test
>>>>>>> test
[root@git /opt/weiaixiong]# vim aaa
master
test
~    
 
[root@git /opt/weiaixiong]# git commit -am "merge test"
[master f5e4308] merge test
[root@git /opt/weiaixiong]# git status
# On branch master
# Your branch is ahead of 'origin1/master' by 8 commits.
#   (use "git push" to publish your local commits)
#
nothing to commit, working directory clean
posted @ 2019-12-04 16:51  唯爱熊  阅读(533)  评论(0编辑  收藏  举报