git基本操作 温馨提示 环境说明 git的安装 创建版本仓库 版本创建 查看版本记录 版本回退 工作区、暂存区和版本库 撤销修改 对比文件的不同 删除文件

git基本操作
温馨提示
环境说明
git的安装
创建版本仓库
版本创建
查看版本记录
版本回退
工作区、暂存区和版本库
撤销修改
对比文件的不同
删除文件

此博客用于记录git常用的命令参数使用方法

遗忘或想不起来了可以来看一眼

所以写的并不详细、不适合初学者学习

环境说明

[root@Check1 ~]# cat /etc/redhat-release 
CentOS release 6.10 (Final)
[root@Check1 ~]# uname -a
Linux Check1.net 2.6.32-754.2.1.el6.x86_64 #1 SMP Fri Jul 13 12:50:12 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

git的安装

[root@Check1 ~]# yum -y install git

创建版本仓库

[root@Check1 ~]# mkdir -p /usr/local/git/test
[root@Check1 ~]# cd /usr/local/git/test/
[root@Check1 test]# git init 
Initialized empty Git repository in /usr/local/git/test/.git/

创建版本库后会在目录内生成一个隐藏目录.git  初始化一个空版本

[root@Check1 test]# ls -al
总用量 12
drwxr-xr-x 3 root root 4096 12月 16 20:51 .
drwxr-xr-x 3 root root 4096 12月 16 20:51 ..
drwxr-xr-x 7 root root 4096 12月 16 20:51 .git
[root@Check1 test]# cd .git/
[root@Check1 .git]# ls
branches config description HEAD hooks info objects refs

版本创建

[root@Check1 test]# pwd
/usr/local/git/test
[root@Check1 test]# echo "version 1.0" >> code.txt   编辑一个文件或目录,在里面添加内容
[root@Check1 test]# git add code.txt   将此文件添加到缓存区
[root@Check1 test]# git commit -m "版本1.0"   提交此文件创建版本记录 -m "版本说明信息"
[master (root-commit) 0367a6a] 版本1.0
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 code.txt

再创建几个版本

[root@Check1 test]# echo "version 2.0" >> code.txt 
[root@Check1 test]# git add code.txt 
[root@Check1 test]# git commit -m "版本2.0"
[master c94bcab] 版本2.0
1 files changed, 1 insertions(+), 0 deletions(-)
[root@Check1 test]# echo "version 3.0" >> code.txt 
[root@Check1 test]# git add code.txt 
[root@Check1 test]# git commit -m "版本3.0"
[master 9f98991] 版本3.0
1 files changed, 1 insertions(+), 0 deletions(-)

在git中创建版本记录只是记录一个修改后面的版本是依赖于前面的版本的

查看版本记录

[root@Check1 test]# git log
commit 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5
Author: wcy <goodmoodwjl@163.com>
Date: Sun Dec 16 21:02:31 2018 +0800

版本3.0

commit c94bcab611218e72f9d806ee347dcfb1d12e73a3
Author: wcy <goodmoodwjl@163.com>
Date: Sun Dec 16 21:01:58 2018 +0800

版本2.0

commit 0367a6a3bd901309e282dc630e6562b7f270e39b
Author: wcy <goodmoodwjl@163.com>
Date: Sun Dec 16 20:55:10 2018 +0800

版本1.0

版本回退

根据HEAD指针回退

HEAD指向当前版本

[root@Check1 test]# git reset --hard HEAD^   回退至上一个版本
HEAD is now at c94bcab 版本2.0

HEAD^指的是上一个版本 HEAD^^指的是上两个版本
上100个版本可以用 HEAD~100
上200个版本可以用 HEAD~200表示

根据版本序列号回退

[root@Check1 test]# git reset --hard 9f9899114f
HEAD is now at 9f98991 版本3.0

版本序列号在上面git log中查看git commit对应的字符串就是  回退的时候不用填写全部版本序列号,前几位即可

如果Git的版本号通过git log查看不到了可以通过git reflog查看操作记录

比如回退到版本3.0

[root@Check1 test]# git reset --hard HEAD^
HEAD is now at c94bcab 版本2.0
[root@Check1 test]# git log
commit c94bcab611218e72f9d806ee347dcfb1d12e73a3
Author: wcy <goodmoodwjl@163.com>
Date: Sun Dec 16 21:01:58 2018 +0800

版本2.0

commit 0367a6a3bd901309e282dc630e6562b7f270e39b
Author: wcy <goodmoodwjl@163.com>
Date: Sun Dec 16 20:55:10 2018 +0800

版本1.0

查看操作记录

[root@Check1 test]# git reflog
c94bcab HEAD@{0}: HEAD^: updating HEAD
9f98991 HEAD@{1}: 9f9899114f: updating HEAD
c94bcab HEAD@{2}: HEAD^: updating HEAD
9f98991 HEAD@{3}: commit: 版本3.0
c94bcab HEAD@{4}: commit: 版本2.0
0367a6a HEAD@{5}: commit (initial): 版本1.0
[root@Check1 test]# git reset --hard 9f98991   回退到版本3.0
HEAD is now at 9f98991 版本3.0

可以看到上面版本3.0 最前面的就是之前版本序列号的前几位,然后再跟进这个序列号进行其他操作

9f98991 HEAD@{3}: commit: 版本3.0    序列号即:9f98991

工作区、暂存区和版本库

git基本操作
温馨提示
环境说明
git的安装
创建版本仓库
版本创建
查看版本记录
版本回退
工作区、暂存区和版本库
撤销修改
对比文件的不同
删除文件

git是管理版本的修改

git commit时只是会把暂存区的修改提交到版本记录中(也就是创建一个版本记录)

撤销修改

丢弃工作区的改动

在工作区修改 code.txt  使用git status查看一下状态

[root@Check1 test]# echo "version 4.0" >> code.txt 
[root@Check1 test]# git status
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
#    modified: code.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

可以看到已修改文件 code.txt

可以操作的选项是

1、git add 提交至暂存区

2、git checkout -- 删除工作区的修改

[root@Check1 test]# git checkout -- code.txt 
[root@Check1 test]# cat code.txt 
version 1.0
version 2.0
version 3.0

删除工作区的修改后 发现刚才添加的version 4.0 已经没有了

已经添加至暂存区未commit

[root@Check1 test]# echo "version 4.0" >> code.txt 
[root@Check1 test]# git add code.txt 
[root@Check1 test]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
#    modified: code.txt
#

把暂存区的文件拉回工作区

[root@Check1 test]# git reset HEAD code.txt 
Unstaged changes after reset:
M    code.txt

再把文件从工作区中checkout出去 丢弃工作区的改动

[root@Check1 test]# git status
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
#    modified: code.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@Check1 test]# git checkout -- code.txt 
[root@Check1 test]# git status
# On branch master
nothing to commit (working directory clean)

已经commit

[root@Check1 test]# echo "version 4.0" >> code.txt 
[root@Check1 test]# cat code.txt 
version 1.0
version 2.0
version 3.0
version 4.0
[root@Check1 test]# git add code.txt 
[root@Check1 test]# git commit -m "版本4.0"
[master d61d2f2] 版本4.0
1 files changed, 1 insertions(+), 0 deletions(-)

进行版本回退

[root@Check1 test]# git log --pretty=oneline
d61d2f2cfdf2e2feb0dec63227315fcb7c004ff9 版本4.0
9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5 版本3.0
c94bcab611218e72f9d806ee347dcfb1d12e73a3 版本2.0
0367a6a3bd901309e282dc630e6562b7f270e39b 版本1.0
[root@Check1 test]# git reset --hard 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5
HEAD is now at 9f98991 版本3.0
[root@Check1 test]# cat code.txt 
version 1.0
version 2.0
version 3.0

对比文件的不同

对比工作区和版本库的某个文件

[root@Check1 test]# git diff HEAD^ -- code.txt 
diff --git a/code.txt b/code.txt
index d1f9bad..69afb9a 100644
--- a/code.txt
+++ b/code.txt
@@ -1,2 +1,3 @@
version 1.0
version 2.0
+version 3.0

---为三个版本 HEAD^

+++为工作区的版本

结果为+version 3.0 也就是工作区比上个版本多这一行

对比两个版本库中文件

[root@Check1 test]# git diff HEAD~1 HEAD~2 -- code.txt 
diff --git a/code.txt b/code.txt
index d1f9bad..7c8de03 100644
--- a/code.txt
+++ b/code.txt
@@ -1,2 +1 @@
version 1.0
-version 2.0

由此可以看出---为前者 +++为后者

可看出 上个版本比上上个版本多一行 version 2.0

删除文件

新建一个测试文件code2.txt

[root@Check1 test]# echo "version 1.0" >> code2.txt
[root@Check1 test]# git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
#    code2.txt
nothing added to commit but untracked files present (use "git add" to track)
[root@Check1 test]# git add code2.txt 
[root@Check1 test]# git commit -m "new code"
[master e39c2ef] new code
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 code2.txt

rm直接删除工作区文件

[root@Check1 test]# ls
code2.txt code.txt
[root@Check1 test]# rm code2.txt 
rm:是否删除普通文件 "code2.txt"?y
[root@Check1 test]# git status
# On branch master
# Changed but not updated:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
#    deleted: code2.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

删除后可以通过git checkout -- 撤回删除

[root@Check1 test]# git checkout -- code2.txt
[root@Check1 test]# ls
code2.txt code.txt

git rm删除暂存区文件

[root@Check1 test]# rm code2.txt 
rm:是否删除普通文件 "code2.txt"?y
[root@Check1 test]# git status
# On branch master
# Changed but not updated:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
#    deleted: code2.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@Check1 test]# git rm code2.txt
rm 'code2.txt'
[root@Check1 test]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
#    deleted: code2.txt
#

删除后可以通过

1、git reset HEAD <file>...

2、git checkout -- <file>...

进行恢复

[root@Check1 test]# git reset HEAD code2.txt
Unstaged changes after reset:
M    code2.txt
[root@Check1 test]# git checkout -- code2.txt
[root@Check1 test]# ls
code2.txt code.txt

git commit提交删除

[root@Check1 test]# rm code2.txt 
rm:是否删除普通文件 "code2.txt"?y
[root@Check1 test]# git rm code2.txt
rm 'code2.txt'
[root@Check1 test]# git commit -m "DEL code2.txt"
[master c88df5f] DEL code2.txt
1 files changed, 0 insertions(+), 1 deletions(-)
delete mode 100644 code2.txt

恢复只能版本回退

[root@Check1 test]# git reflog
c88df5f HEAD@{0}: commit: DEL code2.txt
e39c2ef HEAD@{1}: commit: new code
9f98991 HEAD@{2}: 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5: updating HEAD
d61d2f2 HEAD@{3}: commit: 版本4.0
9f98991 HEAD@{4}: 9f98991: updating HEAD
c94bcab HEAD@{5}: HEAD^: updating HEAD
9f98991 HEAD@{6}: 9f9899114f: updating HEAD
c94bcab HEAD@{7}: HEAD^: updating HEAD
9f98991 HEAD@{8}: commit: 版本3.0
c94bcab HEAD@{9}: commit: 版本2.0
0367a6a HEAD@{10}: commit (initial): 版本1.0
[root@Check1 test]# git reset --hard e39c2ef
HEAD is now at e39c2ef new code
[root@Check1 test]# ls
code2.txt code.txt