Git 实用教程(二)_本地命令

在这里,我们将要学习如何在本地仓库里更好的控制文件版本。

一、创建和提交

我们已经学习了创建仓库和提交文件的命令,在这里稍微复习一下就好,若是有小伙伴不清楚,可以阅读 Git 实用教程_简介
命令:git initgit addgit commit

二、查看仓库状态

命令:git status

使用方法:

  • 使用最频繁的命令,随时随地可以用,时刻掌握仓库状态

git status

实际操作:

//注:操作系统 Centos7,下同
[slashwan@study ~]$ git status  //家目录不是 Git 仓库,返回信息会告诉你
fatal: Not a git repository (or any parent up to mount point /home)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
[slashwan@study ~]$ cd test
[slashwan@study test]$ git status  //查看仓库状态
# On branch master
nothing to commit, working directory clean

git status 命令会根据仓库不同的状态返回不同的信息。On branch master 是说在主分支 master 上,分支的概念,会进一步学习;nothing to commit 告诉我们仓库没有修改,不需要提交,工作目录是干净的。

三、查看文件的修改细节

命令:git diff

使用方法:

  • 无参数,比较工作区与暂存区

git diff

  • 比较暂存区与版本库

git diff --cached [<path>…]

  • 比较工作区与版本库

git diff HEAD [<path>…]

实际操作:
在工作目录下改写 Hello.py 文件的第二行,
原内容:print (‘Hello slashwan!’)
改写内容为:

# study Git
print ('Hello, could you tell me your name please?')
[slashwan@study test]$ git diff  //无参数,默认比较工作区与暂存区
diff --git a/Hello.py b/Hello.py
index 25d83bb..9295908 100644
--- a/Hello.py
+++ b/Hello.py
@@ -1,2 +1,2 @@
 # study git
-print ('Hello slashwan!')
+print ('Hello, could you tell me your name please?')
[slashwan@study test]$ git diff --cached  //无返回信息,因修改后没提交至暂存区,目前暂存区和版本库一致
[slashwan@study test]$ git diff HEAD      //和比较工作区与暂存区的返回信息相同
diff --git a/Hello.py b/Hello.py
index 25d83bb..9295908 100644
--- a/Hello.py
+++ b/Hello.py
@@ -1,2 +1,2 @@
 # study git
-print ('Hello slashwan!')
+print ('Hello, could you tell me your name please?')

git diff 返回的信息里,标明了两个版本的差别。

[slashwan@study test]$ git add Hello.py          //防止意外,保存一下
[slashwan@study test]$ git commit -m "ask name"  //第二个版本信息 "ask name"
[master 20acc9d] ask name
 1 file changed, 1 insertion(+), 1 deletion(-)

四、撤销工作区修改

命令:git checkout --

使用方法:

  • 让工作区回到最近一次的暂存区,若暂存区未修改过,回到暂存区也就和最近版本库保持了一致,参数 -- 很重要

git checkout -- <file>

实际操作:
我们在 Hello.py 中添加些乱码

# study Git
print ('Hello, could you tell me your name please?')
abcdefg
[slashwan@study test]$ git status  //查看仓库状态
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)  //告诉我们可以 add 工作区修改
#   (use "git checkout -- <file>..." to discard changes in working directory)  //告诉我们如何撤销工作区修改
#
#       modified:   Hello.py
#
no changes added to commit (use "git add" and/or "git commit -a")
[slashwan@study test]$ git checkout -- Hello.py  //撤销工作区修改
[slashwan@study test]$ git status
# On branch master
nothing to commit, working directory clean

此时,我们工作区 Hello.py 文件的修改就撤销了,git status 告诉我们工作目录是干净的。

五、撤销暂存区

命令:git reset HEAD

使用方法:

  • 把 add 至暂存区还未 commit 的文件撤销回工作区

git reset HEAD <file>

实际操作:
我们在 Hello.py 中又写了些乱码

# study Git
print ('Hello, could you tell me your name please?')
abcdefg
[slashwan@study test]$ git add Hello.py  //添加至暂存区
[slashwan@study test]$ git status        //查看下仓库状态
# On branch master
# Changes to be committed:  //告诉我们暂存区文件还未提交至版本库
#   (use "git reset HEAD <file>..." to unstage)  //括号里告诉了我们如何撤销
#
#       modified:   Hello.py
#
[slashwan@study test]$ git reset HEAD Hello.py  //把暂存区文件撤回工作区
Unstaged changes after reset:
M       Hello.py

然后,仓库状态回到了「四、撤销工作区修改 」。

六、查看提交日志

命令:git log

使用方法:

  • 查看历史的提交日志

git log

  • 该命令有很多参数,查看日志的命令通常都很重要

git log --graph --oneline

  • --graph:会显示 ASCII 图形表示的分支合并历史
  • --oneline:一行显示,而且只显示哈希值和提交说明

实际操作:

[slashwan@study test]$ git log
commit 20acc9d0160bd268623a6182485b9aceba45d2c7
Author: slashwan <slashwan@xx.com>
Date:   Tue Dec 18 14:08:54 2018 +0800

    ask name

commit 538132190821522aa88065e16fc432d017c2c4b2
Author: slashwan <slashwan@xx.com>
Date:   Mon Dec 17 17:04:53 2018 +0800

    a new file Hello.py

可以看到我们提交过的两个版本信息。而 commit 后 20acc9d... 这串数字是 commit-id,是 Git 计算出的哈希值,代表每次提交的版本。请以自己的机器为准。

七、撤销版本库

命令:git reset --hard

使用方法:

  • 版本库控制,可以利用 commit-id 撤销到指定版本库,工作区也会同步撤销

git reset --hard <commit-id>

实际操作:

[slashwan@study test]$ git reset --hard HEAD^  //回退到上个版本
HEAD is now at 5381321 a new file Hello.py

HEAD 代表当前版本,HEAD^ 代表上个版本,用上个版本的 commit-id 效果相同(commit-id 可以只写前几位,保证不冲突就好)。撤销后仿佛没有过第二个版本,提交日志里也没了。

八、查看命令历史

命令:git reflog

使用方法:

  • 查看关于版本库命令的历史

git reflog

实际操作:

[slashwan@study test]$ git reflog
5381321 HEAD@{1}: reset: moving to HEAD^
20acc9d HEAD@{2}: commit: ask name
5381321 HEAD@{3}: commit (initial): a new file Hello.py

可以看到对版本库的三次改动,第 @{2} 是我们提交 ask name 版本的信息,前面是省略的 commit-id ,可以利用它再次撤销版本库。

[slashwan@study test]$  git reset --hard 20acc9d
HEAD is now at 20acc9d ask name

又回到了第二个版本,提交日志也恢复了,相应的命令历史会变成四个。可以看到,只要是提交到版本库的信息,总有些办法恢复;但未提交至版本库,就进行撤销,几种特定情况下无法找回,例如我们写了乱码的版本,从未提交至仓库,已经找不到它的信息了。

九、删除文件

命令:git rmgit commit

使用方法:

  • 删除文件至暂存区,和 git add 相反

git rm <file>

  • 将暂存区内容提交至仓库,版本库更新

git commit -m <message>

实际操作:
先提交一个测试文件 bug.py

[slashwan@study test]$ git add bug.py
[slashwan@study test]$ git commit -m "test git rm bug.py"
[master c37169b] test git rm bug.py
 1 file changed, 1 insertion(+)
 create mode 100644 bug.py

删除版本库已经跟踪的文件,可以认为和添加文件至仓库相反。

[slashwan@study test]$ git rm bug.py
rm 'bug.py'
[slashwan@study test]$ git status  //查看下现在的仓库状态
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       deleted:    bug.py
#
[slashwan@study test]$ git commit -m "delete bug.py"
[master 5946401] delete bug.py
 1 file changed, 1 deletion(-)
 delete mode 100644 bug.py

至此版本库有了四个 commit ,可以用 git log 查看提交日志。

十、移动文件

命令:git mv

使用方法:

  • 移动或重命名一个文件、目录或符号链接,同是修改操作,需要 commit

git mv <file-from> <file-to>

实际操作:
把文件 Hello.py 改名为 hello.py

[slashwan@study test]$ git mv Hello.py hello.py
[slashwan@study test]$ git commit -m "Hello.py -> hello.py"
[master d82b985] Hello.py -> hello.py
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename Hello.py => hello.py (100%)

第五个提交日志生成。

猜你喜欢

转载自blog.csdn.net/huaxuewan/article/details/84999101