关于Git的实践总结(一)

2020-03-27  本文已影响0人  你好树洞先生

关于Git的总结:

一、Git的安装:

1、系统环境准备

[root@git ~]# cat /etc/redhat-release #查看系统版本

CentOS Linux release 7.1.1503(core)

[root@git ~]# uname -r #查看内核版本

3.10.0-229.e7.x86_64

[root@git ~]# gentenforce #确认SELinux关闭状态

Disabled

[root@git ~]# systemctl stop firewalld #关闭防火墙

[root@git ~]# rpm -qa git  #查看git是否安装

git-1.8.3.1-13.el7.x86_64

2.Git安装部署

 #安装Git

[root@git ~]# yum install git

[root@git ~]# git config

--global    使用全局配置文件

--system    使用系统级配置文件

--local    使用版本库级配置文件

#配置git使用用户

[root@git ~]# git config --global user.name "zhangsan"

#配置git使用邮箱

[root@git ~]# git config --global user.email "zhangsan@mail.com"

#语法高亮

[root@git ~]# git config --global color.ui true

#查看当前的配置:

[root@git ~]# git config --list

user.name=zhangsan

user.email=zhangsan@mail.com

color.ui=true

#查看用户:

[root@git ~]# cat .gitconfig

[user]

      name = zhangsan

  email = zhangsan@mail.com

[color]

      ui = true

3.Git初始化

初始化工作目录,对已存在的目录都可以进行初始化

mkdir git_data

cd git_data/

(1)#初始化

git init

(2)#查看工作区状态

git status

#隐藏文件介绍

branches  #分支目录

config    #定义项目特有的配置选项

description  #仅供git web 程序使用

HEAD  #指示当前的分支

hooks # 包含git钩子文件

info  # 包含一个全局排除文件(exclude文件)

objects #存放所有数据内容,有info和pack两个子文件夹

refs    #存放指向数据(分支)的提交对象的指针

index  #保存暂存区信息,在执行git init 的时候,这个文件还没有

---------------------------------------------------------------

(3)#创建空目录:

[root@git ~]# mkdir data

#进行初始化:

[root@git ~]# cd data/

[root@git data]# git init

#初始化了一个空的仓库,位于root下的data的.git

#查看代码仓库:

[root@git data]# ll -a

drwxr-xr-x 7 root root 225 Jan 24 16:36 .git 

#查看工作区的状态:

[root@git data]# git status

#On branch master

#Initial commit

nothing to commit

#git的工作目录:

[root@git data]# pwd

/root/data

4.Git常规使用

(1)创建数据--提交数据

注意:

把代码提交到本地仓库,必须经过 暂存区域。

只有提交到本地仓库的代码,才被系统管理起来。

(2)git基础命令:

#查看工作区的状态:

[root@git data]# git status

#位于分支 master

#初始提交

无文件要提交(创建/拷贝文件并使用"git add"建立跟踪)

(3)创建文件:

[root@git data]# touch a b c

[root@git data]# git status

#位于分支 master

#初始提交

#未跟踪的文件

#(使用"git add <file>..."以包含要提交的内容)

#

#  a

#  b

#  c

提交为空,但是存在尚未跟踪的文件(使用"git add"建立跟踪)

=======================================================

(4)#把a文件提交到暂存区:

[root@git data]# git add a

[root@git data]# git status

#位于分支 master

#初始提交

# 要提交的变更

# (使用 "git rm --cached <file>..."撤出暂存区)

#  新文件:  a

#未跟踪的文件:

#(使用"git add <file>..."以包含要提交的内容)

#

#  b

#  c

=======================================================

(5)#把所有的文件提交到暂存区域:

[root@git data]# git add .

[root@git data]#

#查看状态:

[root@git data]# git status

#On branch master

#Initial commit

#Changes to be commited:

# (Use "git rm --cached <file>..." to unstage)

#

#  new file:  a

#  new file:  b

#  new file:  c

=======================================================

(6)#删除文件

1.先从暂存区撤回到工作区,然后直接删除文件

git rm --cached c

rm -f c

2.直接从暂存区域同工作区域一同删除文件命令

git rm -f b

(7)#把C文件从暂存区撤回来:

[root@git data]# git rm --cached c

rm 'c'

[root@git data]# ll

总用量 0

#查看状态:

[root@git data]# git status

#On branch master

#Initial commit

#Changes to be commited:

# (Use "git rm --cached <file>..." to unstage)

#

#  new file:  a

#  new file:  b

#

#Untracked files

# (use "git add<file>..." to include in what will be comminted)

#

#  c

=======================================================

(8)#提交到本地仓库

[root@git data]# git commit -m "add newfile a"

[master (root-commit)295e997] add newfile a

1 file changed,0 insertions(+),0 deletions(-)

create mode 100644 a

[root@git data]# git status

#On branch master

nothing to commit,working directory clean

=======================================================

(9)修改文件名称的两种方法:

方法一:

[root@git data]# mv a a.txt

[root@git data]# git status

#从缓存区删除a文件

[root@git data]# git rm --cached a

[root@git data]# git status

方法二:(推荐)

直接使用git命令重命名

#把工作区域和暂存区域的文件同时修改文件名称

[root@git data]# git mv a.txt a

[root@git data]# git status

(10)查看两个文件有什么不同:

[root@git ~]# echo 0123 > 1.txt

[root@git ~]# echo 7890 > 2.txt

[root@git ~]# diff 1.txt 2.txt

1c1

< 0123

---

> 7890 

=======================================================

(11)查看工作目录和暂存区有啥不同:

[root@git data]# git diff

[root@git data]# ll

total 0

-rw-r--r-- 1 root root 0 Jan 24 16:46 a

--------------------------------------------------------

#对比暂存区和本地仓库有什么不同:

#把index添加到a:(在工作目录中)

[root@git data]# echo index > a

[root@git data]# cat a

index

[root@git data]# git diff

diff  --git a/a b/a

index e69de29..9015a7a 100644

--- a/a

+++ b/a

@@ -0,0 +1 @@

+index

---------------------------------------------------------

[root@git data]# git diff  --cached

[root@git data]# git diff

diff --git a/a b/a

index e69de29..9015a7a 100644

@@ -0,0 +1 @@

+index

[root@git data]# cat a

index

=======================================================

#出现暂存区和仓库区,不一致,需要把文件添加到仓库区,就一致了。

[root@git data]# git add a

[root@git data]# git diff

[root@git data]# git diff --cached

diff --git a/a b/a

index e69de29..9015a7a 100644

@@ -0,0 +1 @@

+index

[root@git data]# git commit -m "add index"

[master bd2dea9] add index

1 file change,1 insertion(+)

[root@git data]# git diff

[root@git data]# git diff --cached

-------------------------------------------------

#这两段代码是一致的:

[root@git data]# echo 123 >> a

[root@git data]# git add .

[root@git data]# git commit -m "XXX"

#(推荐使用)

[root@git data]# git commit -am "add 123 > a"

[master dcccbf1] add 123 > a

1 file change,1 insertion(+)

[root@git data]#

[root@git data]# git status

#On branch master

nothing to commit,working directory clean

=================================================

#提交后在对比暂存区和本地仓库内容相同:

[root@git data]# git commit -m "modified a"

[master 6r78bf1] modified a

[root@git data]# git diff --cached a

#相当于虚拟机的镜像,任何操作都被做了一次快照,

可恢复到任意一个位置:

#git commit

----------------------------------------------------

#查看历史的git commit快照操作

[root@git data]# git log

#一行简单的显示commit信息

[root@git data]# git log --oneline

#显示当前的指针指向哪里:

[root@git data]# git log --oneline --decorate

#显示具体内容的变化

[root@git data]# git log -p

#只显示1条内容:

[root@git data]# git log -1

====================================================

5.恢复历史数据:

(1)只更改了当前目录:

[root@git data]# echo "333" >> a

[root@git data]# git status

#位于分支 master

#尚未暂存区以备提交的变更:

# (使用"git add <file>..."更新要提交的内容)

#看提示使用此命令覆盖工作区的改动(解释下面)

#  (使用"git checkout --<file>..."丢弃工作区的改动)

#

#  修改:  a

#

修改尚未加入提交(使用 "git add"和/或"git commit -a")

#从暂存区覆盖本地工作目录

[root@git data]# git checkout -- a

[root@git data]# git status

#位于分支 master

无文件要提交,干净的工作区

[root@git ~]# cat a

aaa

(2)修改了本地目录且同时提交到了暂存区

#添加新内容:

[root@git data]# echo ccc >> a

#提交到暂存区:

[root@git data]# git add .

#对比暂存区和本地仓库的内容

[root@git data]# git diff --cached

diff --git a/a b/a

index e69de29..9015a7a 100644

--- a/a

+++ b/a

@@ -1,+1,2 @@

aaa

ccc

------------------------------------------

[root@git data]# git status

#位于分支 master

#要提交的变更:

# (使用"git reset <file>..."撤出暂存区)

#  修改:  a

#本地仓库覆盖暂存区域

[root@git data]# git reset HEAD a

#重置后撤出暂存区的变更:

M    a

[root@git data]# git diff a

diff --git a/a b/a

index e69de29..9015a7a 100644

--- a/a

+++ b/a

@@ -1,+1,2 @@

aaa

+ccc

=========================================

(3)恢复原始数据:

[root@git data]# git log --online

dcccbf1 add 123 > a

bd2dea9 add index

951adcc mv a.txt a

799bc0a modified a a.txt

295e997 add newfile a

[root@git data]# cat a

index

123

[root@git data]# git reset --hard 295e997

HEAD is now at 295e997 add newfile a

[root@git data]# git status

#On branch master

nothing to commit,working directory clean

#说明已经回到了原始的a:

[root@git data]# git log

commit 295e997ff

Author:zhangsan<zhangsan@mail.com>

Date: 

  add newfile a

[root@git data]# cat a

[root@git data]# ll

total 0

-rw-r--r-- 1 root root 0 Jan 24 17:46 a

#发现没有问题,想回到最后一次的修改:

[root@git data]# git log --online

295e997 add newfile a

[root@git data]# git reset --hard dcccbf1

HEAD is now at dcccbf1 add 123 > a

[root@git data]# ll

total 4

-rw-r--r-- 1 root root 0 Jan 24 17:47 a

[root@git data]# cat a

index

#查看所有的历史操作:

[root@git data]# git relog

=====================(持续更新)=====================

上一篇下一篇

猜你喜欢

热点阅读