温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

git命令行命令(1)

发布时间:2020-07-11 13:29:41 来源:网络 阅读:3851 作者:fengoz 栏目:软件技术

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

一. 创建本地仓库

git init可以在本地创建一个空的本地仓库。其常用命令行如下,
git init [-q | --quiet] [--bare] [directory]

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

二.配置本地仓库

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

  • --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]显示配置

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 的事了。

三.修改本地仓库

使用版本管理最常用的操作就是提交代码,不过对git来说,如果我们修改了文件内容提交的话必须先使用git add命令,然后才能使用git commit命令提交到本地仓库。

1. git add

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

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命令是提交暂存区中的修改。

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

向AI问一下细节
推荐阅读:
  1. Git常见命令
  2. git命令

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI