git的使用

一、基本操作

1.查看帮助

[root@node1 ~]# git config 或者 git help

2.设置添加用户,最好用户+主机名和邮箱地址

[root@node1 ~]# git config --global user.name "yfacesclub2018"
[root@node1 ~]# git config --global user.email "yfacesclub2018@163.com"

3.设置编辑器

[root@node1 ~]# git config --global core.editor vim

4.设置颜色

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

5.查看已经设置的列表

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

6.建立一个目录用作版本仓库

[root@node1 ~]# mkdir gitlab
[root@node1 ~]# cd gitlab

7.初始化,记住不要用记事本可以用nodepad++,字符集会不一样,只能管理文本

[root@node1 gitlab]# git init
Initialized empty Git repository in /root/gitlab/.git/

8.查看目录所有的文件

[root@node1 gitlab]# ll -a
total 12
drwxr-xr-x. 3 root root 4096 Oct 21 17:16 .
dr-xr-x---. 5 root root 4096 Oct 21 17:15 ..
drwxr-xr-x. 7 root root 4096 Oct 21 17:16 .git

二、提交文件到本地仓库

例如:

1、添加一个文本文件
[root@node1 gitlab]# vi readme.txt
[root@node1 gitlab]# cat readme.txt
hehe

2、查看git的状态
[root@node1 gitlab]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# readme.txt
nothing added to commit but untracked files present (use "git add" to track)

3、标记这个将要提交的文件
[root@node1 gitlab]# git add readme.txt

[root@node1 gitlab]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: readme.txt
#

4、提交到本地仓库,并添加注释
[root@node1 gitlab]# git commit -m "the first commit"
[master (root-commit) 0caf800] the first commit
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 readme.txt

再次查看状态已经提交
[root@node1 gitlab]# git status
# On branch master
nothing to commit (working directory clean)

三、通过git log查看版本提交日志

1.建立文件deploy.sh,并编辑文件内容

[root@node1 gitlab]# vi deploy.sh
[root@node1 gitlab]# cat deploy.sh
#!/bin/bash 
echo hehe

2.查看状态

[root@node1 gitlab]# git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# deploy.sh
nothing added to commit but untracked files present (use "git add" to track)

3.标记将要提交的文件

[root@node1 gitlab]# git add deploy.sh
[root@node1 gitlab]# git commit -m "the second commit"
[master (root-commit) 0caf800] the second commit
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 deploy.sh

4.查看git log日志
[root@node1 gitlab]# git log
commit 74a771a9213697cc776394c4d827295dfe4a96bf
Author: yfacesclub2018 <yfacesclub@163.com>
Date: Wed Oct 21 17:30:10 2015 +0800

second commit

commit 0caf800b267d3ab9a9c9509033137e9694f025c3
Author: yfacesclub2018 <yfacesclub@163.com>
Date: Wed Oct 21 17:22:13 2015 +0800

the first commit

5.查看git log日志,只显示注释
[root@node1 gitlab]# git log --oneline

四、对第一个版本进行更新(比较两次提交的文件的差别,比较有3个空间的问题)

1.编辑文本文件readme.txt

[root@node1 gitlab]# vi readme.txt 
[root@node1 gitlab]# cat readme.txt
hehe
hehe

(1)缓冲区的和原来的文本进行对比

[root@node1 gitlab]# git diff readme.txt 
diff --git a/readme.txt b/readme.txt
index 91ca0fa..197cac2 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1 +1,2 @@
hehe
+hehe

(2)添加标记到缓冲区

[root@node1 gitlab]# git add readme.txt

(3)缓冲区和本地仓库的比较

[root@node1 gitlab]# git diff --cached readme.txt

(4)提交本地仓库,并添加注释

[root@node1 gitlab]# git commit -m "add 2hehe"
[master 7c10652] add 2hehe
1 files changed, 1 insertions(+), 0 deletions(-)

(5)查看git log

[root@node1 gitlab]# git log
commit 7c10652f853a28e323ef71f3d51934a55c9e08c6
Author: yfacesclub2018 <yfacesclub@163.com>
Date: Wed Oct 21 17:48:28 2015 +0800

add 2hehe

commit 74a771a9213697cc776394c4d827295dfe4a96bf
Author: yfacesclub2018 <yfacesclub@163.com>
Date: Wed Oct 21 17:30:10 2015 +0800

second commit

commit 0caf800b267d3ab9a9c9509033137e9694f025c3
Author: yfacesclub2018 <yfacesclub@163.com>
Date: Wed Oct 21 17:22:13 2015 +0800

the first commit

五、版本回滚

(1)版本回滚上一个版本,HEAD^表示上一个版本
[root@node1 gitlab]# git reset --hard HEAD^
HEAD is now at 74a771a second commit

[root@node1 gitlab]# cat readme.txt 
hehe

[root@node1 gitlab]# git log
commit 74a771a9213697cc776394c4d827295dfe4a96bf
Author: yfacesclub2018 <yfacesclub@163.com>
Date: Wed Oct 21 17:30:10 2015 +0800

second commit

commit 0caf800b267d3ab9a9c9509033137e9694f025c3
Author: yfacesclub2018 <yfacesclub@163.com>
Date: Wed Oct 21 17:22:13 2015 +0800

the first commit


(2)指定版本回滚
[root@node1 gitlab]# git reflog
74a771a HEAD@{0}: HEAD^: updating HEAD
7c10652 HEAD@{1}: commit: add 2hehe
74a771a HEAD@{2}: commit: second commit
0caf800 HEAD@{3}: commit (initial): the first commit

[root@node1 gitlab]# git reset --hard 0caf800
HEAD is now at 0caf800 the first commit

[root@node1 gitlab]# git log
commit 0caf800b267d3ab9a9c9509033137e9694f025c3
Author: yfacesclub2018 <yfacesclub@163.com>
Date: Wed Oct 21 17:22:13 2015 +0800

the first commit
[root@node1 gitlab]# ls
readme.txt
[root@node1 gitlab]# cat readme.txt 
hehe

看到只有readme.txt文件了deploy.sh这个是在第一次提交之后产生的,所以回滚到第一次所以deloy.sh文件没有了。

六、撤销修改的代码或者删除的文件,只要没有提交都可以恢复的

1.再一次修改版本添加提交

[root@node1 gitlab]# vi readme.txt 
[root@node1 gitlab]# cat readme.txt 
hehe
wo shi hehe

[root@node1 gitlab]# git add readme.txt 
[root@node1 gitlab]# git commit -m "add 2"
[master cbe0525] add 2
1 files changed, 1 insertions(+), 0 deletions(-)

[root@node1 gitlab]# git status
# On branch master
nothing to commit (working directory clean)

[root@node1 gitlab]# git log
commit cbe0525305bb22baccc19c837ebc8c39dd6228aa
Author: yfacesclub2018 <yfacesclub@163.com>
Date: Wed Oct 21 18:02:24 2015 +0800

add 2

commit 0caf800b267d3ab9a9c9509033137e9694f025c3
Author:yfacesclub2018 <yfacesclub@163.com>
Date: Wed Oct 21 17:22:13 2015 +0800

the first commit

再一次修改版本添加未提交,撤销修改的代码
[root@node1 gitlab]# vi readme.txt 
[root@node1 gitlab]# cat readme.txt 
hehe
wo shi hehe
hehe hehe

[root@node1 gitlab]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: readme.txt
#

[root@node1 gitlab]# git checkout -- readme.txt 
[root@node1 gitlab]# cat readme.txt
hehe
wo shi hehe


删掉这个文件,然后撤销删除
[root@node1 gitlab]# git rm readme.txt

然后恢复这个文件
[root@node1 gitlab]# git checkout -- readme.txt

七、要忽略某个文件不提交,在本地仓库下创建一个.gitignore文件

[root@node1 ~]# touch a.sh .gitignore

把要忽略的文件的名字写到这个文件里面
[root@node1 ~]# echo .gitignore >> .gitignore
[root@node1 ~]# echo a.sh >> .gitignore

[root@node1 ~]# vi readme.txt
hehe
wo shi hehe
wo shi heihei

[root@node1 ~]# git add . 
[root@node1 ~]# git commit -m "xxoo" 

********************************************************************************************************

参考地址:https://www.cnblogs.com/xiaozhiqi/p/5955836.html

原文地址:https://www.cnblogs.com/yfacesclub/p/10330092.html