Git版本控制管理学习笔记2--起步

首先确保系统中已经安装了git,这里使用的linux系统。

一、命令行初步使用:

1、git命令:

列出它的选项和最常用的子命令。标准命令格式中,COMMAND代表的就是下面列出的子命令。

[root@flower1 ~]# git
image

2、显示版本号:

[root@flower1 ~]# git --version
git version 1.7.1

3、裸双破折号--的作用:

    它用来分离一系列参数。比如下面这个:

[root@flower1 ~]# git diff -w master origin -- tools/Makerfile

这里对命令行的说明,仅是对git命令格式的简单说明。


二、Git使用快速入门:

这里将新建一个版本库,添加一些内容,然后管理一系列修订版本。

1、创建初始版本库:

    在~/public_html目录创建一个个人网站项目,并把它放到Git版本库里。

[root@flower1 ~]# mkdir public_html
[root@flower1 ~]# cd public_html/
[root@flower1 public_html]# echo 'My website is alive!' > index.html

执行git init,将~/public_html转化为Git版本库:

[root@flower1 public_html]# git init
Initialized empty Git repository in /root/public_html/.git/

git init命令会创建一个隐藏目录:至于该文件夹内部的数据代表什么意思,后续会有说明。

image

[root@flower1 public_html]# cd .git
[root@flower1 .git]# pwd
/root/public_html/.git
[root@flower1 .git]# ll
total 32
drwxr-xr-x 2 root root 4096 Nov 29 17:47 branches
-rw-r--r-- 1 root root   92 Nov 29 17:47 config
-rw-r--r-- 1 root root   73 Nov 29 17:47 description
-rw-r--r-- 1 root root   23 Nov 29 17:47 HEAD
drwxr-xr-x 2 root root 4096 Nov 29 17:47 hooks
drwxr-xr-x 2 root root 4096 Nov 29 17:47 info
drwxr-xr-x 4 root root 4096 Nov 29 17:47 objects
drwxr-xr-x 4 root root 4096 Nov 29 17:47 refs

除了在~/public_html目录下新增了一个.git隐藏文件夹,整个目录结构没有任何变化。隐藏在.git内的版本库将由Git维护。

2、将文件添加到版本库中:

最初,每个Git版本库都是空的。为了管理内容,必须明确的把文件加入到版本库中。

这里,将~/public_html文件夹下所有的文件都加入到版本库中:

[root@flower1 public_html]# pwd
/root/public_html
[root@flower1 public_html]# git add .

当然,也可以单个文件添加到版本库,比如这里只用一个index.html文件,我们将它加入到版本库:(其实上面的操作已经将index.html加入到版本库中了)

[root@flower1 public_html]# git add index.html

在使用了git add 命令后,Git知道index.html这个文件是要留在版本库中的,但是,现在它还只是暂存(staged)了这个文件,这是提交之前的中间步骤。Git有意将add和commit命令分开,是为了避免频繁变化。

运行git status命令,显示中间状态的index.html:

image

这个命令显示新文件index.html将在下一次提交(commit)的时候添加到版本库里。

由于Git在每次提交(commit)的时候会记录一下元数据,包括日志消息和作出此次变更的作者,所以一条完整的commit命令如下:

[root@flower1 public_html]# git commit -m "Initial contents of public_html" --author="nextflower <2230256@qq.com>"
[master (root-commit) c57b6cd] Initial contents of public_html
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 index.html

如果想在提交时通过编辑器输入日志消息,并且使用vim来编辑的话,可以导入下面的环境变量:

[root@flower1 public_html]# export GIT_EDITOR=vim

现在再查看一下git status:

image

这意味着工作目录中不包含任何与版本库中不同的未知或者更改过的文件。

3、配置提交作者:

虽然可以在每次提交时使用—author参数来让Git识别你的身份,但如果每次都输入难免让人感觉麻烦。

可以通过下述命令来修改提交作者的信息:

[root@flower1 public_html]# git config user.name "nextflower"
[root@flower1 public_html]# git config user.email "2230256@qq.com"

也可以使用环境变量GIT_AUTHOT_NAME和GIT_AUTHOR_EMAIL来告诉Git。

4、再次提交:

这里修改一下index.html文件,并重新提交。

[root@flower1 public_html]# cd ~/public_html/
[root@flower1 public_html]# vim index.html
[root@flower1 public_html]# cat index.html 
<html>
<body>
My web site is alive!
</body>
</html>
[root@flower1 public_html]# git commit index.html
[master e36e1e0] firsr change
 1 files changed, 5 insertions(+), 1 deletions(-)

这里或许让人感到疑惑,因为在commit之前,并没有使用add命令,这是为什么呢?

因为这个文件已经添加到版本库里了,所以没有必要再把这个文件告诉给索引。

事实上,当我们在修改index.html文件时,如果使用git status命令就会发现,Git已经自动捕捉了文件变更。

5、查看提交:

git log命令会产生版本库里一系列单独提交的历史。

image

可以使用git show + 提交码查看特定提交的详细信息:(若不添加提交码,则将只显示最近一次提交的详细信息

image

还可以使用show-branch提供当前开发分支简洁的单行摘要:

[root@flower1 public_html]# git show-branch --more=10
[master] firsr change
[master^] Initial contents of public_html

6、查看提交差异:

使用两个提交的全ID名并且运行git diff即可:

image

7、版本库内文件的删除和重命名:

     在删除之前,我们先按照上面的步骤再添加一个文件poem.html到版本库中。

image

    然后使用git rm命令将poem.html从版本库中删除。

[root@flower1 public_html]# git rm poem.html 
rm 'poem.html'
[root@flower1 public_html]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    deleted:    poem.html
#
[root@flower1 public_html]# git commit -m "Remove a poem"
[master 1811e1e] Remove a poem
 1 files changed, 0 insertions(+), 1 deletions(-)
 delete mode 100644 poem.html

和添加操作一样,删除操作也需要2步:git rm表示你想删除这个文件并暂存这个变更,接着使用git commit提交了这个变更。注意观察上面两个命令执行后文件系统发生的变化,当执行了git rm命令后,poem.html文件已经在文件系统中被删除了。

那么,如果想为版本库里的一个文件重命名,该如何操作呢?可以通过git rm和git add组合命令达到这个效果。

image

image

当然,也可以使用git mv实现相同的功能:

image

8、创建版本库副本:

    我们可以通过git clone命令创建一个完整的副本,或叫克隆。

    这里,我们在用户主目录里建立一个副本,并命名为my_website。

[root@flower1 ~]# cd ~
[root@flower1 ~]# git clone public_html/ my_website
Initialized empty Git repository in /root/my_website/.git/

    现在public_html和my_website两个版本库包含相同的对象、文件和目录,但是还有一些细微的差别,这些区别将在后续的章节进行说明。查看一下两者的区别:

image


三、配置文件:

和很多工具一样,Git支持不同层次的配置文件。按照优先级递减的顺序,它们如下所示。

.git/config:
    版本库特定的配置设置,可用--file进行修改,拥有最高的优先级。
    实际修改该文件的方式为 git config user.name “xxx”
~/.gitconfig:
    用户特定的配置设置,可用--global选项修改。
/etc/gitconfig:
    系统范围的配置设置,可用--system选项进行修改。这个文件可能在其他位置,或者完全不存在。

这里我们使用下面的命令修改一下用户的特定配置:这个命令其实修改的就是~/.gitconfig中的相关配置。

[root@flower1 my_website]# git config --global user.name "nextflowertest"
[root@flower1 my_website]# git config --global user.email "test@qq.com"

image

如果要修改版本库特定的名字和Email地址,只要执行下面的命令即可,就是将—global省略掉就行。它会修改~/.git/config文件。

[root@flower1 my_website]# git config user.name "nextflowertest"
[root@flower1 my_website]# git config user.email "test@qq.com"

如果要移除某个设置,执行下面的命令:

[root@flower1 public_html]# git config --unset user.email

或者

[root@flower1 public_html]# git config --global --unset user.email

上面两种方式的区别在于,前者移除的是版本库特定的设置,后者移除的是用户的特定设置。

在提交日志消息时,编辑器的选择按照以下步骤的顺序来确定:

  • GIT_EDITOR环境变量
  • core.editor配置选项
  • VISUAL环境变量
  • EDITOR环境变量
  • vi命令

1、配置别名:

   命令格式如下:

# git config --global alias-graph 
> 'log --graph --abbrev-commit --pretty=oneline'

四、结束语:

本节介绍了Git的基本使用方法,但是还有很多的疑问需要解答。

  • Git是如何存储文件的每一个版本的?
  • 一次提交是由什么组成的?
  • 那些有趣的提交数十从哪里来的?
  • 为什么叫master?
  • 。。。

下一节将介绍Git的一些概念。

原文地址:https://www.cnblogs.com/tq03/p/5005201.html