相关命令:
- git init
- git status
- git diff
- git add
- git commit
1.创建本地仓库
仓库的概念:
在Git中,我们将需要管理的项目称为一个仓库,可以简单的理解成一个目录,Git就是管理这个目录下的文件,每个文件的修改、删除都能被Git追踪,以便将来可以回滚。
使用git init
命令创建仓库
在仓库目录(C:\IdeaProjects\GitTest\localRepository)
下进入Git Bash,输入以下命令:
$ git init
示例如下:
qiyue@Surface-qiyue MINGW64 /c/IdeaProjects/GitTest/localRepository
$ git init
Initialized empty Git repository in C:/IdeaProjects/GitTest/localRepository/.git/
qiyue@Surface-qiyue MINGW64 /c/IdeaProjects/GitTest/localRepository (master)
$ls -a
./ ../ .gti
如上,Git提示已经初始化了一个空的仓库,并在仓库目录下创建了一个名为git
的隐藏文件夹,可以使用命令ls -a
查看,这个文件夹就是Git用来管理我们的版本的重要依据,该文件夹里面的文件不要轻易去动。
同时可以注意到,在文件路径后面有一个master
标注,表示现在进入的是master
分支,分支内容后面再详细展开。
2.版本控制
上一步已经创建了一个空的仓库,现在往仓库中添加一个文件:
$ touch README.md
示例如下
qiyue@Surface-qiyue MINGW64 /c/IdeaProjects/GitTest/localRepository (master)
$ touch README.md
qiyue@Surface-qiyue MINGW64 /c/IdeaProjects/GitTest/localRepository (master)
$ ls
README.md
使用VIM往文件中添加内容:
$ vim README.md
修改后示例:
qiyue@Surface-qiyue MINGW64 /c/IdeaProjects/GitTest/localRepository (master)
$ cat README.md
The first time to change the context of README.md
2.1.查看当前仓库状态
2.1.1.使用git status
命令可以显示仓库当前的状态。
$ git status
示例如下:
qiyue@Surface-qiyue MINGW64 /c/IdeaProjects/GitTest/localRepository (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
nothing added to commit but untracked files present (use "git add" to track)
一步步解读上面的信息:
1.on branch master
说明现在正在master分支。
2.No commits yet
说明目前还没有提交任何一个版本。
3.Untracked files:
表示下面的文件还没有被追踪。
4.括号内提示:使用git add
命令来添加需要提交的文件。
5.暂存区中没有文件等待被提交,现在有还未被追踪的文件。
2.2.提交版本
2.2.1使用git add
命令将文件添加到暂存区:
$ git add <filename>
示例如下:
qiyue@Surface-qiyue MINGW64 /c/IdeaProjects/GitTest/localRepository (master)
$ git add README.md
warning: LF will be replaced by CRLF in README.md.
The file will have its original line endings in your working directory.
使用git status
命令查看当前状态:
qiyue@Surface-qiyue MINGW64 /c/IdeaProjects/GitTest/localRepository (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README.md
这次的状态信息显示Changes to be committed
,表示现在有待提交的修改。
2.2.2.使用git commit
命令进行提交:
$git commit -m "版本信息"
qiyue@Surface-qiyue MINGW64 /c/IdeaProjects/GitTest/localRepository (master)
$ git commit -m "Version 1.0"
[master (root-commit) 349aca7] Version 1.0
1 file changed, 1 insertion(+)
create mode 100644 README.md
使用git status
命令再次查看仓库状态:
qiyue@Surface-qiyue MINGW64 /c/IdeaProjects/GitTest/localRepository (master)
$ git status
On branch master
nothing to commit, working tree clean
上述信息说明当前没需要被提交的修改,并且工作区是空的。
2.3版本回退
2.3.1对文件做一些修改
修改后示例:
qiyue@Surface-qiyue MINGW64 /c/IdeaProjects/GitTest/localRepository (master)
$ cat README.md
The second time to change the context of README.md
使用git status
命令查看状态:
qiyue@Surface-qiyue MINGW64 /c/IdeaProjects/GitTest/localRepository (master)
$ 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: README.md
no changes added to commit (use "git add" and/or "git commit -a")
2.3.2.使用git diff
命令查看与上一版本的修改内容
$ git diff
虽然我们使用git status
命令可以看到有内容被修改了,但是并不知道具体修改内容,这时我们可以使用git diff
命令比较文件。
示例如下:
qiyue@Surface-qiyue MINGW64 /c/IdeaProjects/GitTest/localRepository (master)
$ git diff README.md
warning: LF will be replaced by CRLF in README.md.
The file will have its original line endings in your working directory.
diff --git a/README.md b/README.md
index 7f4c53c..40fe672 100644
--- a/README.md
+++ b/README.md
@@ -1 +1 @@
-The first time to change the context of README.md
+The second time to change the context of README.md
-
表示删除的内容,+
表示增加的内容。
git diff
命令的功能远不止如此,这里不进行详细介绍。
2.3.3.提交新版本
qiyue@Surface-qiyue MINGW64 /c/IdeaProjects/GitTest/localRepository (master)
$ git add README.md
warning: LF will be replaced by CRLF in README.md.
The file will have its original line endings in your working directory.
qiyue@Surface-qiyue MINGW64 /c/IdeaProjects/GitTest/localRepository (master)
$ git commit -m "Version 2.0"
[master 2c5a1ae] Version 2.0
1 file changed, 1 insertion(+), 1 deletion(-)
2.3.4.使用git log
查看历史版本信息
$ git log
示例如下:
qiyue@Surface-qiyue MINGW64 /c/IdeaProjects/GitTest/localRepository (master)
$ git log
commit 2c5a1aee260a60f28caf693633d594ea32f4bf78 (HEAD -> master)
Author: qiyue <qiyuemaster@outlook.com>
Date: Thu Aug 17 10:49:35 2017 +0800
Version 2.0
commit 349aca7b5d4bf5adae41a9f67bbc41aae15305f2
Author: qiyue <qiyuemaster@outlook.com>
Date: Thu Aug 17 10:25:31 2017 +0800
Version 1.0
在这里可以查看到历史提交的版本。
commit
后的一大串字符就是版本号。
HEAD
指针表示当前版本,上一个版本就是HEAD^
,上上一个版本就是HEAD^^
,往上超过三个版本时,可以写成HEAD~XX
。
2.3.5使用git reset
命令回退
$git reset
示例如下:
qiyue@Surface-qiyue MINGW64 /c/IdeaProjects/GitTest/localRepository (master)
$ git reset --hard HEAD^
HEAD is now at 349aca7 Version 1.0
查看文件内容:
qiyue@Surface-qiyue MINGW64 /c/IdeaProjects/GitTest/localRepository (master)
$ cat README.md
The first time to change the context of README.md
利用该命令顺利的回到了`Version 1版本。
使用git log
查看版本信息:
qiyue@Surface-qiyue MINGW64 /c/IdeaProjects/GitTest/localRepository (master)
$ git log
commit 349aca7b5d4bf5adae41a9f67bbc41aae15305f2 (HEAD -> master)
Author: qiyue <qiyuemaster@outlook.com>
Date: Thu Aug 17 10:25:31 2017 +0800
Version 1.0
这时会发现Version 2.0
已经不见了,这种情况是我们不希望看到的,我们希望在退回之前的版本时仍保留当前版本。下一节中介绍如果找回HEAD指针之前的版本。
2.4.找回HEAD
指针之前的版本
廖雪峰的博客中将版本管理比喻成时光穿梭,上一节中版本回溯相当于从21世纪回到19世纪,那么这一节就相当于从19世纪前往21世纪。
注意:并不能前往22世纪,最新版本是在21世纪。
2.4.1.找回需要前往版本的版本号
在上一节中我们曾使用git log
命令查看到Version 2.0
的版本号
,我们就可以使用git reset
命令配合版本号
恢复到Version 2.0
.
如果之前没有获得Vsersion 2.0
的版本号
,那么要去哪里找呢?
使用git reflog
命令查看历史命令
$ git reflog
示例:
qiyue@Surface-qiyue MINGW64 /c/IdeaProjects/GitTest/localRepository (master)
$ git reflog
349aca7 (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
2c5a1ae HEAD@{1}: commit: Version 2.0
349aca7 (HEAD -> master) HEAD@{2}: commit (initial): Version 1.0
这里我们就可以看到Vesion 2.0
的版本号
为2c5a1ae...
。
2.4.2. 使用git reset
前往目标版本
示例:
qiyue@Surface-qiyue MINGW64 /c/IdeaProjects/GitTest/localRepository (master)
$ git reset --hard 2c5a1ae
HEAD is now at 2c5a1ae Version 2.0
查看文件内容:
qiyue@Surface-qiyue MINGW64 /c/IdeaProjects/GitTest/localRepository (master)
$ cat README.md
The second time to change the context of README.md
这时又回到Version2.0
版本。
利用上述一些操作,我们可以在指定版本间进行时间跳跃
。
3.有关HEAD指针解析
Git中的将各个版本利用链表的形式进行表示,HEAD
指针指向当前正在使用的版本。
在版本管理中做回退操作时,Git仅仅将HEAD
指针指向目标版本,所以Git在进行版本更换时速度非常快。
当前HEAD
指向Version2.0
.
当回退到Version 1.0
之后
HEAD
指针产生了变化。