Git是一个分布式的文件版本控制系统,每个电脑都有一个完整的文件库和版本库,文件库之间可以互相推送和抓取版本信息。
CVS和SVN是集中式的文件版本控制系统,版本库是集中存放在中央服务器的,每个电脑只跟服务器交互信息。使用时首先需要从服务器下载最新的版本,然后工作完提交给服务器。集中式版本控制系统最大的问题就是必须联网才能工作
1.创建版本库
DELL@DELL-PC MINGW64 ~
$ mkdir mygit //创建一个版本库
DELL@DELL-PC MINGW64 ~
$ cd mygit //跳转到新创建的版本库
DELL@DELL-PC MINGW64 ~/mygit
$ pwd
/c/Users/DELL/mygit //显示路径
2.把这个目录变成Git可以管理的仓库:
DELL@DELL-PC MINGW64 ~/mygit
$ git init
Initialized empty Git repository in C:/Users/DELL/mygit/.git/
git init,仓库的初始化,把这个目录变成Git可以管理的仓库,此时当前目录下多了一个.git的目录。
3.创建一个mycode.txt,并添加到仓库
在当前目录下,创建一个mycode.txt文件,里面写上this is the first code
(1)把文件添加到仓库,git add
DELL@DELL-PC MINGW64 ~/mygit (master)
$ git add mycode.txt
添加后没有任何显示
(2)把文件提交到仓库
DELL@DELL-PC MINGW64 ~/mygit (master)
$ git commit -m "this is the first code"
[master (root-commit) 54f3324] this is the first code
1 file changed, 1 insertion(+)
create mode 100644 mycode.txt
-m后面输入的是本次提交的说明
首先需要git add添加到暂存区,然后再git commit提交。不添加到暂存区不能提交。
4.查看仓库当前的状态,git status
mycode.txt中增加一行,add a new code,然后查看状态
DELL@DELL-PC MINGW64 ~/mygit (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: mycode.txt
no changes added to commit (use "git add" and/or "git commit -a")
文件已经修改过,但是没有提交。
5.查看修改了什么内容,git diff
DELL@DELL-PC MINGW64 ~/mygit (master)
$ git diff
diff --git a/mycode.txt b/mycode.txt
index faa0f05..d756750 100644
--- a/mycode.txt
+++ b/mycode.txt
@@ -1 +1,2 @@
-this is the first code
\ No newline at end of file
+this is the first code
+add a new code
\ No newline at end of file
6.显示版本的修改情况,git log命令显示从最近到最远的提交日志
再对mycode.txt修改,增加change the code,然后提交。
DELL@DELL-PC MINGW64 ~/mygit (master)
$ git log
commit e10935008173acd1b944867d1b36e13ce3f61f44
Author: lwg <1030818018@qq.com>
Date: Mon May 30 10:56:39 2016 +0800
change the code
commit 2233e4587ea0b01f34787f7ebd2c3e519289a033
Author: lwg <1030818018@qq.com>
Date: Mon May 30 10:55:30 2016 +0800
add a new code
commit 54f33247ea4525a687934a6b0f8dffeb6e098df7
Author: lwg <1030818018@qq.com>
Date: Mon May 30 10:41:17 2016 +0800
this is the first code
//每个版本在一行显示
DELL@DELL-PC MINGW64 ~/mygit (master)
$ git log --pretty=oneline
e10935008173acd1b944867d1b36e13ce3f61f44 change the code
2233e4587ea0b01f34787f7ebd2c3e519289a033 add a new code
54f33247ea4525a687934a6b0f8dffeb6e098df7 this is the first code
显示每个版本的信息,修改时间,从最远到最近
使用过github客户端的都知道,每次修改后,实Git就会把它们自动串成一条时间线。在客户端可以看到一个节点一个节点在时间线上,每一个节点都是修改后的一个版本。最前边的是最初的版本,最后的是最新的版本。点击不同的节点,可以看到不同的版本信息。
7.回退到上一个版本
用HEAD表示当前版本,上一个版本就是HEAD^,上上一个版本就是HEAD^^,上100个版本写成HEAD~100。
DELL@DELL-PC MINGW64 ~/mygit (master)
$ git reset --hard HEAD^
HEAD is now at 2233e45 add a new code//退回到了第二版本,增加一行代码
8.查看历史命令,git reflog
DELL@DELL-PC MINGW64 ~/mygit (master)
$ git reflog
e109350 HEAD@{0}: reset: moving to e109350
2233e45 HEAD@{1}: reset: moving to HEAD^
e109350 HEAD@{2}: commit: change the code
2233e45 HEAD@{3}: commit: add a new code
54f3324 HEAD@{4}: commit (initial): this is the first code
根据commit id,可以回到任意版本
9.文件的内容打印到屏幕上,cat
DELL@DELL-PC MINGW64 ~/mygit (master)
$ cat mycode.txt
this is the first code
add a new code
change the code
10.删除文件
创建一个test.txt文件,添加到Git并且提交
DELL@DELL-PC MINGW64 ~/mygit (master)
$ rm test.txt //在工作区删除,此时版本库中仍然存在
此时目录下没有了test.txt的文件了。但是版本库中存在。
如果误删了,可以利用版本库中的恢复
DELL@DELL-PC MINGW64 ~/mygit (master)
$ git checkout -- test.txt //利用版本库中的恢复,此时目录下test.txt恢复了
如果想彻底删除,文件从版本库中彻底删除了。
DELL@DELL-PC MINGW64 ~/mygit (master)
$ git checkout -- test.txt
DELL@DELL-PC MINGW64 ~/mygit (master)
$ rm test.txt
DELL@DELL-PC MINGW64 ~/mygit (master)
$ git commit -m "remove test"
On branch master
Changes not staged for commit:
deleted: test.txt
no changes added to commit
学习参考的是廖雪峰的git教程