首先确保系统中已经安装了git,这里使用的linux系统。html
列出它的选项和最经常使用的子命令。标准命令格式中,COMMAND表明的就是下面列出的子命令。linux
[root@flower1 ~]# git
[root@flower1 ~]# git --version git version 1.7.1
它用来分离一系列参数。好比下面这个:git
[root@flower1 ~]# git diff -w master origin -- tools/Makerfile
这里对命令行的说明,仅是对git命令格式的简单说明。web
这里将新建一个版本库,添加一些内容,而后管理一系列修订版本。vim
在~/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命令会建立一个隐藏目录:至于该文件夹内部的数据表明什么意思,后续会有说明。网站
[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维护。spa
最初,每一个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:
这个命令显示新文件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:
这意味着工做目录中不包含任何与版本库中不一样的未知或者更改过的文件。
虽然能够在每次提交时使用—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。
这里修改一下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已经自动捕捉了文件变动。
git log命令会产生版本库里一系列单独提交的历史。
可使用git show + 提交码查看特定提交的详细信息:(若不添加提交码,则将只显示最近一次提交的详细信息)
还可使用show-branch提供当前开发分支简洁的单行摘要:
[root@flower1 public_html]# git show-branch --more=10 [master] firsr change [master^] Initial contents of public_html
使用两个提交的全ID名而且运行git diff便可:
在删除以前,咱们先按照上面的步骤再添加一个文件poem.html到版本库中。
而后使用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组合命令达到这个效果。
固然,也可使用git mv实现相同的功能:
咱们能够经过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两个版本库包含相同的对象、文件和目录,可是还有一些细微的差异,这些区别将在后续的章节进行说明。查看一下二者的区别:
和不少工具同样,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"
若是要修改版本库特定的名字和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 config --global alias-graph \ > 'log --graph --abbrev-commit --pretty=oneline'
本节介绍了Git的基本使用方法,可是还有不少的疑问须要解答。
下一节将介绍Git的一些概念。