Git简介
git是一个分布式的版本控制系统,由linux的创始人linus研发,可以管理项目的修改!
本日志根据廖雪峰的官方网站的学习课程来学习git!
Git安装
linux
首先,你可以试着输入git,看看系统有没有安装Git:
$ git
The program 'git' is currently not installed.
如上所述便是没有安装git,这时候可以使用yum install -y git
命令来在线安装git版本控制器!
windows
Windows下要使用很多Linux/Unix的工具时,需要Cygwin这样的模拟环境,Git也一样。Cygwin的安装和配置都比较复杂,就不建议你折腾了。不过,有高人已经把模拟环境和Git都打包好了,名叫msysgit,只需要下载一个单独的exe安装程序,其他什么也不用装,绝对好用。
msysgit是Windows版的Git,从 点击访问下载,然后按默认选项安装即可。
安装完成后,在开始菜单里找到“Git”->“Git Bash”,蹦出一个类似命令行窗口的东西,就说明Git安装成功!
安装后执行的操作
因为Git是分布式版本控制系统,所以,每个机器都必须自报家门:你的名字和Email地址。你也许会担心,如果有人故意冒充别人怎么办?这个不必担心,首先我们相信大家都是善良无知的群众,其次,真的有冒充的也是有办法可查的。
$ git config --global user.name "Your Name"
$ git config --global user.email "email@example.com"
git基本使用
创建版本库
什么是版本库呢?版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改、删除,Git都能跟踪,以便任何时刻都可以追踪历史,或者在将来某个时刻可以“还原”。
所以,创建一个版本库非常简单,首先,选择一个合适的地方,创建一个空目录:
[root@localhost ~]# mkdir git
[root@localhost ~]# cd git
[root@localhost git]# ls
[root@localhost git]# git init
Initialized empty Git repository in /root/git/.git/
瞬间Git就把仓库建好了,而且告诉你是一个空的仓库(empty Git repository),细心的读者可以发现当前目录下多了一个.git的目录,这个目录是Git来跟踪管理版本库的,没事千万不要手动修改这个目录里面的文件,不然改乱了,就把Git仓库给破坏了。
如果你没有看到.git目录,那是因为这个目录默认是隐藏的,用ls -ah
命令就可以看见。
向项目中添加文件
接下来在git仓库中添加一个文件,并检查一下git库的状态变化
[root@localhost git]# vim readme.md
[root@localhost git]# git add readme.md
[root@localhost git]# git commit -m "add 1 file"
[master (root-commit) 1d4d94c] add 1 file
1 files changed, 3 insertions(+), 0 deletions(-)
create mode 100644 readme.md
[root@localhost git]# ls
readme.md
git add files
可以将新增文件或者其他修改保存到暂存区,(暂存区:编辑区和版本之间的一个中转缓冲区。git的工作流程是,先把编辑区内的改动保存到暂存区,再通过 git commit -m "decription"
命令提交到版本库中),然后通过git commit -m "decription"
命令提交修改。其中参数 files 可以是多个文件,也可以通过使用 git add .
来添加所有变更信息。
查看文件状态和修改的地方
[root@localhost git]# vi readme.md
[root@localhost git]# 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: readme.md
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@localhost git]# git diff
diff --git a/readme.md b/readme.md
index d146366..bebe864 100644
--- a/readme.md
+++ b/readme.md
@@ -1,3 +1,3 @@
nihaosoft
hha
-我是杜安邦
+我是秦仙游
[root@localhost git]#
git status
命令可以查看编辑区的文件有哪些变动,git diff [file]
则可以查看具体的不同之处,如果git diff
后不跟参数,默认显示所有变更。接下来继续提交代码。
[root@localhost git]# git add .
[root@localhost git]# git commit -m "change name"
[master def448c] change name
1 files changed, 1 insertions(+), 1 deletions(-)
[root@localhost git]# git status
# On branch master
nothing to commit (working directory clean)
[root@localhost git]#
版本回退
commit id不用写全,只写前面几个字母便可以,但是最好不要太短!项目回退使用的是廖雪峰的官方网站中的介绍!
单个文件
查看更新记录git log file
,返回结果如下
[root@localhost git]# git log readme.md
commit def448c8f7f372114eb256c090b1dad9c0f963a1
Author: dab1993 <dab1117@163.co>
Date: Thu Oct 1 08:51:19 2015 +0800
change name
commit 1d4d94c7a70acda47481e0be793125e5c7ee98c4
Author: dab1993 <dab1117@163.co>
Date: Thu Oct 1 08:38:22 2015 +0800
add 1 file
如果命令变成git log --pretty=oneline file
[root@localhost git]# git log --pretty=oneline readme.md
def448c8f7f372114eb256c090b1dad9c0f963a1 change name
1d4d94c7a70acda47481e0be793125e5c7ee98c4 add 1 file
先执行git reset [commit id] file
会退到指定版本;
然后提交到本地参考git commit -m "revert old file"
,
最后更新到工作目录:git checkout file
;这时候提交到远程服务或者commit都可以了。
项目回退
首先,Git必须知道当前版本是哪个版本,在Git中,用HEAD表示当前版本,也就是最新的提交3628164...882e1e0
(注意我的提交ID和你的肯定不一样),上一个版本就是HEAD^
,上上一个版本就是HEAD^^
,当然往上100个版本写100个^
比较容易数不过来,所以写成HEAD~100
。
现在,我们要把当前版本“append GPL”回退到上一个版本“add distributed”,就可以使用git reset
命令:
$ git reset --hard HEAD^
HEAD is now at ea34578 add distributed
--hard
参数有啥意义?这个后面再讲,现在你先放心使用。
看看readme.txt的内容是不是版本add distributed:
$ cat readme.txt
Git is a distributed version control system.
Git is free software.
果然。
还可以继续回退到上一个版本wrote a readme file,不过且慢,然我们用git log
再看看现在版本库的状态:
$ git log
commit ea34578d5496d7dd233c827ed32a8cd576c5ee85
Author: Michael Liao <askxuefeng@gmail.com>
Date: Tue Aug 20 14:53:12 2013 +0800
add distributed
commit cb926e7ea50ad11b8f9e909c05226233bf755030
Author: Michael Liao <askxuefeng@gmail.com>
Date: Mon Aug 19 17:51:55 2013 +0800
wrote a readme file
最新的那个版本append GPL已经看不到了!好比你从21世纪坐时光穿梭机来到了19世纪,想再回去已经回不去了,肿么办?
办法其实还是有的,只要上面的命令行窗口还没有被关掉,你就可以顺着往上找啊找啊,找到那个append GPL的commit id是3628164…,于是就可以指定回到未来的某个版本:
$ git reset --hard 3628164
HEAD is now at 3628164 append GPL
版本号没必要写全,前几位就可以了,Git会自动去找。当然也不能只写前一两位,因为Git可能会找到多个版本号,就无法确定是哪一个了。
再小心翼翼地看看readme.txt的内容:
$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
撤消修改
工作区和暂存区的概念
工作区就是存放我们编辑的代码的地方,暂存区,是我们的代码add之后存放的地方。commit之后存放在我们的本地版本库里!
修改尚在工作区的修改
$ git checkout -- readme.txt
命令git checkout -- readme.txt
意思就是,把readme.txt文件在工作区的修改全部撤销,这里有两种情况:
一种是readme.txt
自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;
一种是readme.txt
已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。
总之,就是让这个文件回到最近一次git commit
或git add
时的状态。
git checkout -- file
命令中的--
很重要,没有--
,就变成了“切换到另一个分支”的命令,我们在后面的分支管理中会再次遇到git checkout
命令。
修改已经add到暂存区的内容
Git同样告诉我们,用命令git reset HEAD file可以把暂存区的修改撤销掉(unstage),重新放回工作区:
$ git reset HEAD readme.txt
Unstaged changes after reset:
M readme.txt
git reset
命令既可以回退版本,也可以把暂存区的修改回退到工作区。当我们用HEAD
时,表示最新的版本。
再用git status
查看一下,现在暂存区是干净的,工作区有修改:
总结
场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- file
。
场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset HEAD file
,就回到了场景1,第二步按场景1操作。
场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,参考版本回退一节,不过前提是没有推送到远程库。
删除文件
在Git中,删除也是一个修改操作,我们实战一下,先添加一个新文件test.txt到Git并且提交:
$ git add test.txt
$ git commit -m "add test.txt"
[master 94cdc44] add test.txt
1 file changed, 1 insertion(+)
create mode 100644 test.txt
一般情况下,你通常直接在文件管理器中把没用的文件删了,或者用rm命令删了:
$ rm test.txt
这个时候,Git知道你删除了文件,因此,工作区和版本库就不一致了,git status
命令会立刻告诉你哪些文件被删除了:
$ git status
# On branch master
# Changes not staged for commit:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: test.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
现在你有两个选择,一是确实要从版本库中删除该文件,那就用命令git rm
删掉,并且git commit
:
$ git rm test.txt
rm 'test.txt'
$ git commit -m "remove test.txt"
[master d17efd8] remove test.txt
1 file changed, 1 deletion(-)
delete mode 100644 test.txt
现在,文件就从版本库中被删除了。
另一种情况是删错了,因为版本库里还有呢,所以可以很轻松地把误删的文件恢复到最新版本:
$ git checkout -- test.txt
git checkout
其实是用版本库里的版本替换工作区的版本,无论工作区是修改还是删除,都可以“一键还原”。
命令git rm
用于删除一个文件。如果一个文件已经被提交到版本库,那么你永远不用担心误删,但是要小心,你只能恢复文件到最新版本,你会丢失最近一次提交后你修改的内容。