#1.创建repository
repository,即仓库的意思,这里表示创建git的本地仓库。可以简单的理解为在Windows上某个盘符下的一个目录文件,该目录里面所有的文件都会被git管理,每个文件的修改/删除,git都会跟踪,因此在任何时刻都可以追踪历史,即还原成以前的版本。
我的仓库创建在h盘,名称:gitRepositories,创建过程如下
这个时候本机的h盘下面便多了个文件(gitRepositories)。
接着我在本地仓库里面新建一个子仓库,如下
如上的那些操作,只是简单的通过linux命令创建一些目录,和git没有任何关系,为了能让git管理这个目录(仓库),需要使用git init命令,如下
这个时候H:\gitRepositories\firstRepo目录下多了一个.git目录,这个目录就是git来跟踪/管理版本的。(注意:该.git目录位于firstRepo目录下,因此只有在该目录下的文件才会被git管理)
#2.将需要被git管理文件添加到仓库中
这里需要明白一点:所有的版本控制工具,只能跟踪到文本文件的改动,比如txt、网页、程序的源码等,git也不例外。对于图片、音视频这些二进制文件,git虽然也能管理,但是没有办法跟踪到二进制文件的具体变化,也就是说只能知道图片/音频/视频的大小从50kb变成了80kb,但是到底改动了什么,git也不知道。
我在firstRepo目录下新建一个记事本文件test.txt,内容:123456,可以手动的在h盘建立,也可以通过命令来建立。添加到版本库中的步骤如下
##2.1 使用git add test.txt将test.txt添加到暂存区,如下
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ git add test.txt
没有任何提示,则说明添加成功了。
##2.2 使用git commit将暂存区的文件提交到本地仓库
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ git commit -m "首次提交test.txt"
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'sand@sand_pc.(none)')
截图如下
很明显,我这里报错了,提示要先执行
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
提示:需要先配置user.email和user.name来设置我们的账号,如果加上--global的话,表示该账号全局可以使用,否则仅仅使用于该仓库。
##2.3 配置权限
这里我们就来设置全局的吧
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ git config --global user.name "lixianli"
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ git config --global user.email designer_back@163.com
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ git config --global -l
user.name=lixianli
user.email=designer_back@163.com
##2.4 继续提交
接着我们再来提交(有可能需要重新添加一次)
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ git add test.txt
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ git commit -m "首次提交test.txt"
[master (root-commit) 0d6dd3c] 首次提交test.txt
1 file changed, 1 insertion(+)
create mode 100644 test.txt
很明显,这次提交成功了。
##2.5 查看还有哪些修改的文件未提交
接着使用git status命令来查看暂存区是否还有文件未提交
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ git status
On branch master
nothing to commit, working directory clean
说明暂存区没有修改过的文件需要提交。
##2.6 修改文件内容
接着,我们来修改下test.txt里面的内容,修改成如下
123456
789012
这个时候git已经检测到了test.txt文件发生了改变,继续使用git status命令来查看是否存在已经修改过还未提交的文件。
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (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: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
如上告诉我们,test.txt文件已发生改变。
使用 "git add 文件名" 来更新/提交该文件
使用 "git checkout --文件名" 来放弃改变
使用 "git diff 文件名" 来查看该文件有哪些改变
这里,我们先查看下test.txt文件有哪些变化
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ git diff test.txt
diff --git a/test.txt b/test.txt
index 4632e06..3827e5e 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
-123456
\ No newline at end of file
+123456
+789012
\ No newline at end of file
截图如下
以上说明:修改前文件a/test.txt,修改后文件b/test.txt
修改前文件的内容
123456
修改后文件的内容
123456
789012
##2.7 提交修改后的文件
接着我们提交到本地仓库
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ git add test.txt
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ git commit -m "增加一行,内容为789012"
[master 87e8bc4] 增加一行,内容为789012
1 file changed, 2 insertions(+), 1 deletion(-)
#3.版本回退
##3.1 修改/提交text.txt文件
在test.txt文件再增加一行数据:345678。
将test.txt文件添加到暂存区。
将test.txt文件提交到本地仓库。
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ git add test.txt
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ git commit -m "增加一行数据:345678"
[master 3d5f005] 增加一行数据:345678
1 file changed, 2 insertions(+), 1 deletion(-)
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$
##3.2 查看历史记录
使用git log命令查看历史记录
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ git log
commit 3d5f0052c4ece6ce8bdc78a2634fdf64ecfc0567
Author: lixianli <designer_back@163.com>
Date: Wed Nov 2 16:27:05 2016 +0800
增加一行数据:345678
commit 87e8bc4d25b910f73bf167262aada5f0cf9da970
Author: lixianli <designer_back@163.com>
Date: Wed Nov 2 12:28:35 2016 +0800
增加一行,内容为789012
commit 0d6dd3cc7b055804db1378113a9ca9b24c7b2d53
Author: lixianli <designer_back@163.com>
Date: Wed Nov 2 12:24:00 2016 +0800
首次提交test.txt
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$
可以看出,历史记录是由近到远显示的。
提交版本号:3d5f0052c4ece6ce8bdc78a2634fdf64ecfc0567、87e8bc4d25b910f73bf167262aada5f0cf9da970
提交作者:lixianli designer_back@163.com
提交日期:Wed Nov 2 16:27:05 2016 +0800
提交信息:增加一行数据:345678
上面的信息很全,如果我们只关注提交信息呢,有时候需要根据提交信息来回退版本,这时候需要使用命令
$ git log --pretty=oneline
3d5f0052c4ece6ce8bdc78a2634fdf64ecfc0567 增加一行数据:345678
87e8bc4d25b910f73bf167262aada5f0cf9da970 增加一行,内容为789012
0d6dd3cc7b055804db1378113a9ca9b24c7b2d53 首次提交test.txt
这样看起来就精简多了。
3.3回退版本
回退到上一个版本命令:git reset --hard HEAD^
回退到上两个版本命令:git reset --hard HEAD^^
以此类推……
回退版本命令2:git reset --hard HEAD~n,这里n是正整数,表示向前推几个版本
我这里举例,向前推2个版本
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ git reset --hard HEAD~2
HEAD is now at 0d6dd3c 首次提交test.txt
我们再来查看下test.txt里面的内容
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ cat test.txt
123456
接着查看下提交历史记录
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ git log
commit 0d6dd3cc7b055804db1378113a9ca9b24c7b2d53
Author: lixianli <designer_back@163.com>
Date: Wed Nov 2 12:24:00 2016 +0800
首次提交test.txt
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$
很明显,提交记录只有一次,版本回退了。
##3.4 前进版本
有时候回退版本后,我们发现,最新的版本有些内容是我们需要的,即这个时候需要获取到当前版本之后版本的一些内容。那么,如何恢复到回退前的版本呢。
通过命令git reset --hard 版本号,可以到任何版本。但是这里,我们回退前没有记住最新版本的版本号,则我们可以使用如下命令获取版本号
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ git reflog
0d6dd3c HEAD@{0}: reset: moving to HEAD~2
3d5f005 HEAD@{1}: commit: 增加一行数据:345678
87e8bc4 HEAD@{2}: commit: 增加一行,内容为789012
0d6dd3c HEAD@{3}: commit (initial): 首次提交test.txt
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$
每次提交/回退操作,系统都会有一个操作号,这里我们拿到了所有的操作号。比如,我想回退到"增加一行,内容为789012"这个版本,则使用如下命令
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ git reset --hard 87e8bc4
HEAD is now at 87e8bc4 增加一行,内容为789012
接下来,查看这个版本的内容,取出我们所需要的
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ cat test.txt
123456
789012
#4.工作区与暂存区的区别
工作区:即我们电脑上常见的目录,例如H:\gitRepositories。
暂存区:这个目录H:\gitRepositories\firstRepo下有个隐藏目录.git,该隐藏目录就是版本库(repository),版本库里面存放了很多东西,最重要的是stage(暂存区),还有git会为我们自动创建第一个分支master。
总结:将文件提交仓库有两步
1.使用git add 将文件添加到暂存区
2.使用git commit 将暂存区的所有文件提交到当前分支上
如果我们修改了工作区中的文件,没有将该文件添加到暂存区,而直接提交该文件,是不能成功的。例如:我修改了test.txt文件内容,然后直接提交
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (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: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ git commit -m "增加内容:cccccc"
On branch master
Changes not staged for commit:
modified: test.txt
no changes added to commit
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$
#5.撤销修改
现在我在test.txt文件里添加了内容:cccccc,先查看
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ cat test.txt
123456
789012
cccccc
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$
这个时候还没提交,但是我发现添加的内容cccccc有错误,因此,这时候我需要恢复到之前的版本,有如下方法。
方法1:前提是我知道我应该删除那些内容,则我直接手动的删除那些内容,然后add到暂存区,接着commit就行。
方法2:使用git reset --hard HEAD~n命令回退到上一个版本。
方法3:假设我不想使用上述两种方法呢,想直接使用撤销命令,则应该如下
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ git checkout -- test.txt
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$
截图如下
使用命令 git checkout --文件名,就是将文件在工作区做的修改全部撤销,这里存在两种情况
1.test.txt修改后,还没放到暂存区。使用撤销命令就可以回退到与版本库一模一样的状态。
2.test.txt修改后,存放到了暂存区,接着又做了修改。这时候,撤销命令只能回退到添加到暂存区后的状态。剩下的,就只能采用方法1或者方法2来回退了。
#6.删除文件
现在我们在H:\gitRepositories\firstRepo目录下新建一个a.txt文件,然后提交
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ ls
a.txt test.txt
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ git add a.txt
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ git commit -m "新增a.txt文件"
[master da94b84] 新增a.txt文件
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a.txt
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$
假设我现在需要删除a.txt,使用如下命令
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ ls
a.txt test.txt
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ rm a.txt
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ ls
test.txt
这种删除只是将目录下的a.txt文件删除了,并没有将版本库中的文件彻底删除,要想彻底删除,还需要执行commit命令。如下
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master)
$ git commit -m "彻底删除a.txt文件"
On branch master
Changes not staged for commit:
deleted: a.txt
no changes added to commit