Git
Git 是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而是一个开放源码的版本控制软件。Git 与常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,不必有服务器端软件支持。
1、安装
[root@ci01 ~]# yum install git -y
[root@ci01 ~]# git --version
git version 1.8.3.1
#基本配置
设置用户名tom
git config --global user.name "tom"
#设置用户邮箱
git config --global user.email "tom@qq.com"
#查看配置信息
[root@ci01 ~]# git config -l
user.name=tom
user.email=tom@qq.com
2、仓库(版本库)
版本库又名仓库,英文名repository。对应的就是一个目录,这个目录中的所有文件被git管理起来。以后会将一个项目的根目录,作为仓库。仓库中的每个文件的改动 都由git跟踪。
2.1、新建仓库
mkdir repo01
cd repo01
#通过git init命令把这个目录变成Git可以管理的仓库:
[root@ci01 repo01]# git init
Initialized empty Git repository in /root/repo01/.git/
#初始化空的 Git 版本库于 /root/repo01/.git/
[root@ci01 repo01]# ll -a
total 12
drwxr-xr-x 3 root root 4096 Aug 16 09:53 .
dr-xr-x---. 11 root root 4096 Aug 16 09:53 ..
drwxr-xr-x 7 root root 4096 Aug 16 09:53 .git
瞬间Git就把仓库建好了,而且告诉你是一个空的仓库(empty Git repository),当前目录下多了一个.git的目录,这个目录是Git来跟踪管理版本库的,没事千万不要手动修改这个目录里面的文件,不然改乱了,就把Git仓库给破坏了。
2.2 远程连接仓库
git remote add origin git@github.com:yourName/repositoryname.git
git remote add origin https://github.com/yourName/repositoryname.git
git remote -v 查看远程连接
git remote rm origin 删除远程连接
3、将代码提交到Git暂存区

[root@ci01 repo01]# echo "123" > index.html
[root@ci01 repo01]#git add index.html
[root@ci01 repo01]# pwd
/root/repo01
#提交文件必须在当前仓库目录下,否则报错。
git add file2.txt file3.txt
#添加多个文件中间空格
4、文件提交到git缓冲区
[root@ci01 repo01]# git commit -m 'wrote a readme file' .
[master (root-commit) 304f7f1] init
1 file changed, 1 insertion(+)
create mode 100644 index.html
# 参数:
# -m : 添加注释
.: 表示当前目录下的所有文件
#commit可以一次提交很多文件
git commit -m "tom" index.html
- 修改index.html文件
[root@ci01 repo01]# cat > index.html << EOF
> what
> how are you
> ok
> i am fine
> FOE
> EOF
现在,运行git status命令看看结果:
[root@ci01 repo01]# git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: index.html
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# html
no changes added to commit (use "git add" and/or "git commit -a")
git status命令可以让我们时刻掌握仓库当前的状态,上面的命令输出告诉我们,readme.txt被修改过了,但还没有准备提交的修改。
虽然Git告诉我们readme.txt被修改了,但如果能看看具体修改了什么内容,自然是很好的。比如你上个厕所回来已经记不清上次怎么修改的index.htlm,所以,需要用git diff这个命令看看:
[root@ci01 repo01]# git diff
diff --git a/index.html b/index.html
index 3fe95bc..3714b35 100644
--- a/index.html
+++ b/index.html
@@ -1 +1,5 @@
-nihaoa
+what
+how are you
+ok
+i am fine
+FOE
git diff顾名思义就是查看difference,显示的格式正是Unix通用的diff格式。
然后重新add、commit
[root@ci01 repo01]# git add index.html
[root@ci01 repo01]# git commit -m "two" index.html
[master 406e0f7] two
1 file changed, 5 insertions(+), 1 deletion(-)
5、回滚
在实际工作中,我们脑子里怎么可能记得一个几千行的文件每次都改了什么内容,不然要版本控制系统干什么。版本控制系统肯定有某个命令可以告诉我们历史记录,在Git中,我们用git log命令查看:
[root@ci01 repo01]# git log
commit 406e0f7d8fe427d6b3f009937d1c7d4a9a581b9f
Author: tom <tom@qq.com>
Date: Wed Aug 18 13:13:52 2021 +0800
two
commit 425dbf1ec2fdbc0a2bb7abde79f3e75bb27e7a7b
Author: tom <tom@qq.com>
Date: Wed Aug 18 12:56:47 2021 +0800
tom
git log命令显示从最近到最远的提交日志,我们可以看到index.html文件2次提交,最近的一次是two,上一次是tom。
如果嫌输出信息太多,看得眼花缭乱的,可以试试加上--pretty=oneline参数:
我们要把当前版本two回退到上一个版本tom,就可以使用git reset命令:
[root@ci01 repo01]# git reset --hard HEAD^
HEAD is now at 425dbf1 tom
[root@ci01 repo01]# cat index.html
nihaoa
首先,Git必须知道当前版本是哪个版本,在Git中,用HEAD表示当前版本,也就是最新的提交1094adb...(注意我的提交ID和你的肯定不一样),上一个版本就是HEAD^,上上一个版本就是HEAD^^
hard接版本号
[root@ci01 repo01]# git reset --hard 425dbf1ec2fdbc0a2bb7abde79f3e75bb27e7a7b
HEAD is now at 425dbf1 tom
[root@ci01 repo01]# cat index.html
nihaoa
Git提供了一个命令git reflog用来记录你的每一次命令,类似history记录shell命令
本文来自博客园,作者:看啥,转载请注明原文链接:https://www.cnblogs.com/jykn92/p/15146623.html
浙公网安备 33010602011771号