您的位置:首页 > 其它

Git学习笔记(2)--Git基本操作

2012-12-27 11:47 459 查看

Git学习笔记(2)--Git基本操作

Gitgit

首先

介绍一本Git的书,<<Pro Git>>,此书的网络版是可以免费获取的,是一本非常好的介绍Git的书籍。

然后,介绍一下我的运行环境,是ubuntu10.10,Git版本是1.7.1。

现在,进入正题介绍一下基本的Git命令,通过这些命令,你可以搭建一个单机版的Git库,开始你的工作,并且,在你是用Git管理你的软件时,你绝大部分时间使用的命令就是这几个。

这些命令有:

git init 初始化Git库

git add 向Git库提交文件修改(包括创建文件)

git commit 基于此分支提交一个更改

git reset 去除目标提交之后的一切提交记录(世界清净级大招)

git log 查看当前分支下的提交记录

git status 查看当前状态

git checkout 切换分支或回到某次提交

git branch 创建分支,查看分支等

git merge 合并目标分支到当前分支

从现在开始,我们一一实践这些命令

首先我们创建文件夹GitTest

#mkdir GitTest

#cd GitTest

然后创建文件readme,test并在test输入字符串“1”

#touch readme

#echo "1">test

接下来,执行一下三个操作

#git init 创建git仓库

#git config user.name yym 配置作者名

#git config user.email yym@**.com 配置email

#git add . 向git仓库中添加文件(.表示当前目录及其子目录下所有文件)

#git commit -m "initial" 进行第一次提交

现在,分别运行三个命令

#ls -a

. .. .git readme test

如果你看到.git恭喜你,你已经有了一个Git仓库,如果你没看到,抱歉,请重新安装Git。

#git status

# On branch master

nothing to commit (working directory clean)

翻译一下,你在分支master上,没什么可以提交的(工作文件夹很干净)

这里有两个概念需要解释,都很重要:分支,干净

1.branch分支是Git的一个重要概念,可以说Git是以这个概念为核心设计的。你可以把分支理解为你当前的工作发展方向,你的程序所在的位置,你的未来。一般来说,会有如下分支稳定版本bug修复分支,程序发展分支1~n,特性分支,发展分支,主分支等等。分支的管理和权限分配构成里项目的组织结构,这就像数据结构之于算法。

关于分支的妙用,这里推荐一个篇文章

《Git分支管理是一门艺术》

2.干净,这是一个美妙的词。干净代表你的程序没有什么需要提交的修改,意味着你完成了这个阶段性成果,当然这是在你的程序是正确的情况下。

#git log

commit 5b1ac4cff1bc7d91622ba6d5f733db1d2ba2af0f

Author: yym <yym@**.com>

Date: Wed Sep 28 17:56:06 2011 +0800

initial

commit代表提交,后面的hash值是这次提交的唯一标志,接下来是作者与时间,然后就是这次提交的名称。简洁直观

接下来,我们修改readme文件及test文件

#echo I" create this dir for learning Git">>readme

#echo “test”>test

注意>>和>的区别

创建文件notrack

#touch notrack

然后我们输入

#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

# modified: test

#

# Untracked files:

# (use "git add <file>..." to include in what will be committed)

#

# notrack

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

很好,我们发现,我们还在master分支上(当然!),然后,我们发现readme与test处于修改未更新状态,notrack处于未追踪状态。这是什么意思呢?

要回答这个问题,就需要介绍一个很要的概念。在Git的世界中,文件被分为3类:

1.未追踪,也就是说在此文件夹下,但是未被Git库追踪。

2.未修改,这代表这个文件未被改变

3.修改未更新,这就是上面的状态了

4.已暂存,处于这种状态,那就是等着提交(commit)了

现在我们需要用到add命令,将修改提交打Git库中。

#git add readme

# On branch master

# Changes to be committed:

# (use "git reset HEAD <file>..." to unstage)

#

# modified: readme

#

# 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: test

#

# Untracked files:

# (use "git add <file>..." to include in what will be committed)

#

# notrack

此时,你就可以将修改提交到Git库中,当然,你也可以将test文件的修改也暂存一起提交。这里我只提交单个文件的修改

#git commit -m "2nd commit"

#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: test

#

# Untracked files:

# (use "git add <file>..." to include in what will be committed)

#

# notrack

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

可以看到,刚才的readme文件的修改已经被提交了

#git log

commit ae79a590fa13673a584bc5cea08eaa260a7dd473

Author: yym <yym@**.com>

Date: Wed Sep 28 18:10:48 2011 +0800

2nd commit

commit 52a2d3bb92cc54a69f43a6118ca3ee1d1b520156

Author: yym <yym@**.com>

Date: Wed Sep 28 18:01:29 2011 +0800

initial

这是提交记录,就不多说了。现在,我们把test文件也提交了。

#git add test

#git commit -m "3rd commit"

#git log

commit b8d7f73a84944bc38a36990512ab5d9ceda77fef

Author: yym <yym@**.com>

Date: Thu Sep 29 09:34:51 2011 +0800

3rd commit

commit ae79a590fa13673a584bc5cea08eaa260a7dd473

Author: yym <yym@**.com>

Date: Wed Sep 28 18:10:48 2011 +0800

2nd commit

commit 52a2d3bb92cc54a69f43a6118ca3ee1d1b520156

Author: yym <yym@**.com>

Date: Wed Sep 28 18:01:29 2011 +0800

initial

突然,你想回退到2nd commit,想看看当时test文件中的内容。

你可以使用命令

#git checkout ae79a590fa13673a584bc5cea08eaa260a7dd473

#cat 1

1

看完test之前的内容后,你觉得还是第三次提交的内容比较合适,又想回到第三次提交,同样的,你可以

#git checkout b8d7

#cat test

test

你可以不必打出所有位数,只要你保证他是独一无二的,但是你至少需要打出四位。

好吧,不要觉得我是个反复无常的人,现在我有觉得3rd commit完全是多余,我想回到2nd,彻底丢弃之后的内容。只需输入如下命令

#git reset --hard ae79

#git log

commit ae79a590fa13673a584bc5cea08eaa260a7dd473

Author: yym <yym@**.com>

Date: Wed Sep 28 18:10:48 2011 +0800

2nd commit

commit 52a2d3bb92cc54a69f43a6118ca3ee1d1b520156

Author: yym <yym@**.com>

Date: Wed Sep 28 18:01:29 2011 +0800

initial

最后,觉得每次都要add所有修改文件太麻烦了?

ok

git commit -a -m "CommitName"

你可以使用如上命令跳过暂存区域,或者你也可以把它看作是自动add已修改文件

关于Git的一部分基本操作就介绍到这里,下文会介绍Git的另一部分常用操作。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: