git命令行命令(1)

咱们知道git是分布式的版本库,也就是本地仓库里面包含了开发的所用内容,每一个人都是本地版本库的主人,包括历史记录、文件内容。即便没有和远程代码库交换依旧能够提交内容到本地仓库,而后git push到远程仓库。
可使用git $commit --help查看每一个命令的html帮助文档,例如git init --helphtml

一. 建立本地仓库

git init能够在本地建立一个空的本地仓库。其经常使用命令行以下,
git init [-q | --quiet] [--bare] [directory]java

  • -q| --quiet :建立过程当中只输出警告或者错误级别的信息
  • --bare是表示建立的版本库是裸版本库,也就是不包含工做空间,只有.git目录下面内容的仓库,通常用做服务器上的远程仓库。
  • directory为本地仓库的目录

二.配置本地仓库

当咱们有了本地仓库之后,须要对这个仓库配置,须要配置用户名和用户的email和其余的配置。
git config命令提供了三种级别的配置。分别是:git

  • --global:用户级别。其修改的配置文件的内容在~/.gitconfig文件中,若是是windows系统就在C:\Users\$用户名 的目录下
  • --system:适用于所用的用户,其修改的配置文件在etc/gitconfig文件中,若是是windows系统,通常修改在git的安装目录下,例如$GIT_INSTALL_DIR\mingw64\etc\gitconfig
  • --local:适用范围为本版本库,其修改的配置文件在本地仓库的.git/config文件中,也是git config修改的默认范围。
    在使用git命令配置的时候,其配置文件的优先级为local>global>system,可使用git config --list --show-origin查看每一个配置项所在的文件

    1.设置配置

git config [--add] name value                ---添加或者修改配置项,默认的使用范围为本地仓库,可使用--global、--system来指定范围,
                                                                        例如 git config user.name fenglxh、git config user.email fenglxh@126.com
git config --unset name                           --取消该配置项,一样可使用--global、--system来指定范围,

2.显示配置

使用git config [-l|--list]显示配置windows

3.配置命令别名

git存在大量命令,能够对咱们常常使用命令,并且命令比较长的命令设置一个别名,也就是一个简写。
别名的配置也须要使用config命令,好比给 git status 设置别名 st:缓存

git config alias.st status                -----以alias.开头
git config --global alias.lg "log --color --graph --oneline --abbrev-commit"

这样咱们之后使用的时候,直接用 git st 就能够作 git status 的事了。bash

三.修改本地仓库

使用版本管理最经常使用的操做就是提交代码,不过对git来讲,若是咱们修改了文件内容提交的话必须先使用git add命令,而后才能使用git commit命令提交到本地仓库。服务器

1. git add

git add命令是把修改提交到暂存区中。dom

git add -A                   -----------懒人模式,把工做目录下全部的更改提交到,包括删除、添加、修改文件
git add gameoflife-acceptance-tests/\*.java -----------------------------把某个目录下的全部java后缀的文件提交
git add *.java                              ------------------------------提交全部的java后缀的文件

2.git rm

git rm命令是把暂存区中的添加删除,命令基本和git add相反,都是修改的暂存区分布式

git rm --cached hello-word/README                   ---------------把 hello-word/README从暂存区移除
git rm -f hello-word/README                              ---------------把hello-word/README从暂存区移除,同时删除工做目录下的该文件
git rm --cached Documentation/*.txt                   ---------------把Documentation下的全部的txt文件从暂存区移除

3.git commit

git commit命令是提交暂存区中的修改。ide

git commit -m "commit message"          --------------带有提交注释的提交
git commit --allow-empty -m "This is a empty commit"             ----------当暂存区没有变化的时候,是提交失败的,能够加上 --allow-empty运行空提交,此时这两个提交的tree对象指向同一个。

4.git stash

当咱们修改了工做区的内容,可是还不能提交,此时须要更新代码的时候,能够把本地的修改存储,使用git stash命令。这样就会把工做区的修改(不包括新增)保存,并把工做区的内容切换到HEAD指向的提交中,这样又是一个干净的工做区了。

git stash ---------------存储
git stash pop -------------弹出存储
git stash list  --------显示全部的存储

实现原理:
当咱们使用git stash命令的时候,会生成.git/refs/stash文件,其内容为stash的 sha1信息。能够经过git cat-file查看这个SHA1的信息,会发现这个sha1是以当前的SHA1和工做区提交(建立的一个提交对象)为父提交。
git命令行命令(1)
注:SHA-Stash为.git/refs/stash文件中保存的sha1。sha-temp为工做区的提交

当咱们屡次运行git stash的时候,.git/refs/stash文件中的sha永远执行最近执行的stash对应的sha。在.git\logs\refs/stash文件中按顺序保存全部的stash命令对应的sha

四.查看仓库

1.git status

显示工做目录下的状态。

当咱们不存在工做目录修改的时候执行输出以下信息:
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean
随便对其中的某个文件修改,可是不提交暂存区:
$ git status
On branch master
Your branch is up-to-date with 'origin/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:   pom.xml

no changes added to commit (use "git add" and/or "git commit -a")
此时咱们把修改提交到缓存区,再查看状态,会发现暂存区发生了变化。
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   pom.xml
此时咱们再修改该文件,可是不执行git add命令。
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   pom.xml

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:   pom.xml
会发现提示pom.xml文件修改了出现两个地方,一个是暂存区的修改,一个是工做目录的修改。并且其提示颜色并不相同

咱们可使用git status -s命令查看统计的信息(工做区的修改使用红色字体,绿×××字体是暂存区的修改)。
git命令行命令(1)

2.git diff

git diff命令显示工做区、提交、暂存区的差别

$ git diff                                         ------显示工做空间和暂存区的差别
diff --git a/pom.xml b/pom.xml
index 0ec4374..3f64500 100644
--- a/pom.xml
+++ b/pom.xml
@@ -10,7 +10,7 @@                                     ----文件的第十行开始的起航
     <properties>
         <build.number>SNAPSHOT</build.number>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
-        <easyb.version>1.4</easyb.version>
+        <easyb.version>1.5</easyb.version>
         <cobertura.version>2.6</cobertura.version>
         <!-- A workaround for a bug in PMD -->
         <sourceJdk>1.7</sourceJdk>
@@ -178,6 +178,12 @@
                 <version>${easyb.version}</version>
                 <scope>test</scope>
             </dependency>
+                        <dependency>
+                <groupId>dom4j</groupId>
+                <artifactId>dom4j</artifactId>
+                <version>1.6.2</version>
+                <scope>test</scope>
+            </dependency>
         </dependencies>
     </dependencyManagement>
     <dependencies>

$git add pom.xml  ------------------------提交到暂存区
$git diff --cached      ----------------------比较暂存区和提交的差别

3.git log

git log命令能够查看提交的信息。

git log -1              ----------------能够查看最近的一次提交,-N表示最近的N次提交
git log --oneline -N     --------------提交以一行信息显示,等于--pretty=oneline
git log --graph               -----------以图形的方式展现提交树
git log --stat                  -----------------展现提交的汇总信息

git log的精细化输出,--pretty选项。使用--pretty选项能够精细化输出commit的全部信息。

git log --oneline         --------------等价于git log --pretty=oneline输出内容为<sha1> <title line>
git log --pretty=short            ------------输出内容为<sha1>
                                                                            <author>
                                                                            <title line>
git log --pretty=medium/full/fuller/email
git log --pretty=raw        暂时提交的树、父提交等比较全的信息

git --pretty=format:<string> 其中string是能够格式化的,支持占位符。经常使用的占位符以下:

  • %H: commit hash -----提交的SHA
  • %h: abbreviated commit hash ----提交的SHA简写形式
  • %P: parent hashes ----------父提交的SHA
  • %p: abbreviated parent hashes ----------父提交的SHA简写形式
  • %an: author name -----做者名字
  • %ae: author email
  • %ar: author date, relative
  • %n: newline
  • %%: a raw %-----%自身
  • %s: subject ---提交注释
    例如:
    git log --pretty=format:"The author of %h was %an, %ar%nThe title was >>%s<<%n" -------输出大概以下:
    The author of fe6e0ee was Junio C Hamano, 23 hours ago
    The title was >>t4119: test autocomputing -p<n> for traditional diff input.<<

    4.git grep

    git grep命令能够根据模式按行查找内容。支持的pattern类型以下:--basic-regexp, --extended-regexp, --fixed-strings, or --perl-regexp。默认为basic-regexp

    git grep 'time_t' -- '*.[ch]'
    git grep -E 'public (class|interface) \w+[0-9]+' --------查找全部的java的类名包含数字的类
    git grep -F 'fixed string'

    5.git blame

    git blame能够查找文件每一行的提交信息,追溯文件的内容。git blame xxx.txt

相关文章
相关标签/搜索