Git学习笔记(1)

安装Git

在linux下安装:yum install git

其他系统安装在这里略去~~~

安装完成后,需要设置一下,在命令行输入以下命令:

[root@xwq ~]# git config --global user.name "your name"

[root@xwq ~]# git config --global user.email "your email"

注意git config命令的--global参数,用了这个参数,表示你这台机器上所有的Git仓库都会使用这个配置,当然也可以对某个仓库指定不同的用户名和Email地址。

创建版本库

创建版本库很简单,找到一个合适的地方,创建一个空目录:

[root@xwq ~]# mkdir testgit

[root@xwq ~]# cd testgit/

[root@xwq testgit]# pwd

/root/testgit

如果你是在window下运行git,请确保文件路径没有中文!

通过git init命令把这个目录变成Git可以管理的仓库:

[root@xwq testgit]# git init

Initialized empty Git repository in /root/testgit/.git/

接着我们在仓库目录下创建一个txt文件:test_01.txt

[root@xwq testgit]# vim test_01.txt

在里面输入以下内容:

Git is a version control system.

Git is free software.

用命令git add告诉Git,把文件添加到仓库:
[root@xwq testgit]# git add test_01.txt
执行上面的命令,没有任何显示,这就对了,Unix的哲学是“没有消息就是好消息”,说明添加成功。

用命令git commit告诉Git,把文件提交到仓库:

[root@xwq testgit]# git commit -m "wrote a readme file"

[master (root-commit) 7347c20] wrote a readme file

1 file changed, 2 insertions(+)

create mode 100644 test_01.txt

简单解释一下git commit命令,-m后面输入的是本次提交的说明,可以输入任意内容,当然最好是有意义的,这样你就能从历史记录里方便地找到改动记录。
嫌麻烦不想输入-m "xxx"行不行?确实有办法可以这么干,但是强烈不建议你这么干,因为输入说明对自己对别人阅读都很重要。
git commit命令执行成功后会告诉你,1个文件被改动(我们新添加的test_01.txt文件),插入了两行内容(test_01.txt有两行内容)

撤销命令

我们将test_01.txt文件的内容改为如下内容:

Git is a distributed version control system.

Git is free software.

运行git status命令查看结果:

[root@xwq testgit]# git status

#On branch master

#Changes not staged for commit:

#(use "git add ..." to update what will be committed)

#(use "git checkout -- ..." to discard changes in working directory)

  modified:   test_01.txt

#no changes added to commit (use "git add" and/or "git commit -a")

git status命令可以让我们时刻掌握仓库当前的状态,上面的命令告诉我们,text_01.txt被修改过了,但还没有准备提交的修改。
虽然Git告诉我们test_01.txt被修改了,如果想查看被修改了什么内容可以用git diff命令查看:

[root@xwq testgit]# git diff test_01.txt

diff --git a/test_01.txt b/test_01.txt

index 46d49bf..9247db6 100644

--- a/test_01.txt

+++ b/test_01.txt

@@ -1,2 +1,2 @@

-Git is a version control system.

+Git is a distributed version control system.

Git is free software.

我们知道被修改的内容后就可以放心的文件提交到仓库了,接下来我们用git add :

[root@xwq testgit]# git add test_01.txt

同样没有返回任何信息就是好结果

我们在用git status查看当前仓库的状态:

[root@xwq testgit]# git status

# On branch master

# Changes to be committed:

#   (use "git reset HEAD ..." to unstage)

#

#    modified:   test_01.txt

#

git status 告诉我们将要被提交的修改包括test_01.txt,下一步,就可以放心地提交了:

[root@xwq testgit]# git commit -m "add distributed"

[master 6c90823] add distributed

1 file changed, 1 insertion(+), 1 deletion(-)

版本回退

我们再次对test_01.txt文件进行修改,修改内容如下:

Git is a distributed version control system.

Git is free software distributed under the GPL.

然后提交:

[root@xwq testgit]# git add test_01.txt

[root@xwq testgit]# git commit -m "append GPL"

[master ee985a6] append GPL

1 file changed, 1 insertion(+), 1 deletion(-)

Git每当你觉得文件修改到一定程度的时候,就可以“保存一个快照”,这个快照在Git中被称为commit。一旦你把文件改乱了,或者误删了文件,还可以从最近的一个commit恢复,然后继续工作,而不是把几个月的工作成果全部丢失。
现在,我们回顾一下test_01.txt文件一共有几个版本被提交到Git仓库里了:
版本1:wrote a readme file

Git is a version control system.

Git is free software.

版本2:add distributed

Git is a distributed version control system.

Git is free software.

版本3:append GPL

Git is a distributed version control system.

Git is free software distributed under the GPL.

在实际工作中,我们脑子里怎么可能记得一个几千行的文件每次都改了什么内容,不然要版本控制系统干什么。版本控制系统肯定有某个命令可以告诉我们历史记录,在Git中,我们用git log命令查看:

[root@xwq testgit]# git log

commit ee985a69fecf94482280769949290b240b7a0329

Author: wenqiuan <1303460512@qq.com>

Date:   Thu Apr 13 11:45:14 2017 -0400

  append GPL

commit 6c9082366df03ee9a47f2bcd18be0105fa1fae86

Author: wenqiuan <1303460512@qq.com>

Date:   Thu Apr 13 11:33:34 2017 -0400

   add distributed

commit 7347c2023929935a58f147168a50ce9bf51a2478

Author: wenqiuan <1303460512@qq.com>

Date:   Thu Apr 13 09:48:56 2017 -0400

   wrote a readme file

git log命令显示从最近到最远的提交日志,我们可以看到3次提交,最近的一次是append GPL,上一次是add distributed,最早的一次是wrote a readme file。

如果嫌输出信息太多,看得眼花缭乱的,可以试试加上--pretty=oneline参数:

[root@xwq testgit]# git log --pretty=oneline

ee985a69fecf94482280769949290b240b7a0329 append GPL

6c9082366df03ee9a47f2bcd18be0105fa1fae86 add distributed

7347c2023929935a58f147168a50ce9bf51a2478 wrote a readme file

注:类似这些数据ee985a69fecf94482280769949290b240b7a0329的是commit id(版本号)
那么问题来了我们要如何回退版本呢?其实很简单,首先,Git必须知道当前版本是哪个版本,在Git中,用HEAD表示当前版本,也就是最新的提交ee985a69fecf94482280769949290b240b7a0329,上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100。

现在,我们要把当前版本“append GPL”回退到上一个版本“add distributed”,就可以使用git reset命令:

[root@xwq testgit]# git reset --hard HEAD^

HEAD is now at 6c90823 add distributed

现在我们查看一下test_01.txt的内容是不是add distributed:

[root@xwq testgit]# cat test_01.txt

Git is a distributed version control system.

Git is free software.

[root@xwq testgit]# git log

commit 6c9082366df03ee9a47f2bcd18be0105fa1fae86

Author: wenqiuan <1303460512@qq.com>

Date:   Thu Apr 13 11:33:34 2017 -0400

 add distributed

commit 7347c2023929935a58f147168a50ce9bf51a2478

Author: wenqiuan <1303460512@qq.com>

Date:   Thu Apr 13 09:48:56 2017 -0400

 wrote a readme file

很显然我们已经回退到上一个版本了

现在我又想回到最新版本的append GPL了怎么办?这个我们可以用git reflog命令就可以轻松的办到,命令git reflog用来记录你的每一次命令:

[root@xwq testgit]# git reflog

6c90823 HEAD@{0}: reset: moving to HEAD^

ee985a6 HEAD@{1}: commit: append GPL

6c90823 HEAD@{2}: commit: add distributed

7347c20 HEAD@{3}: commit (initial): wrote a readme file

接下来我们只用输入命令 git reset --hard ee985a6(append GPL的id)就可以回到最新的版本了

[root@xwq testgit]# git reset --hard ee985a6

HEAD is now at ee985a6 append GPL

[root@xwq testgit]# cat test_01.txt

Git is a distributed version control system.

Git is free software distributed under the GPL.
|| 版权声明
作者:废权
链接:https://blog.yjscloud.com/archives/13
声明:如无特别声明本文即为原创文章仅代表个人观点,版权归《废权的博客》所有,欢迎转载,转载请保留原文链接。
THE END
分享
二维码
Git学习笔记(1)
安装Git 在linux下安装:yum install git 其他系统安装在这里略去~~~ 安装完成后,需要设置一下,在命令行输入以下命令: [root@xwq ~]# git config --global ……
<<上一篇
下一篇>>
文章目录
关闭
目 录