2. Git基础命令

前言

该文章只是记录了一些自己的见解,可能并不准确,只是为了学习时的一些记录,不喜勿喷,谢谢

这里先介绍一些基础命令

1. 初始化本地仓库

初始化本地仓库有两种

1.1 git init

使用git init 的方式创建初始仓库,如下图

[root@huangzb ~]# mkdir mygit
[root@huangzb ~]# cd mygit/
[root@huangzb mygit]# ls -al
total 8
drwxr-xr-x  2 root root 4096 Mar 15 20:38 .
dr-xr-x---. 8 root root 4096 Mar 15 20:38 ..
[root@huangzb mygit]#
[root@huangzb mygit]# git init
Initialized empty Git repository in /root/mygit/.git/
[root@huangzb mygit]#
[root@huangzb mygit]# ls -al
total 12
drwxr-xr-x  3 root root 4096 Mar 15 20:39 .
dr-xr-x---. 8 root root 4096 Mar 15 20:38 ..
drwxr-xr-x  7 root root 4096 Mar 15 20:39 .git
[root@huangzb mygit]#
 

可以看出,这里通过 git init命令后,在当前目录下 出现了一个 .git目录,该目录就是本地仓库

1.2 git clone

使用 git clone的方式从远程获取代码到本地仓库,该方式一般用于从远程代码库拷贝代码到本次, 比如我们去github上学习别人优质代码时,会使用该命令将代码下载到本地,还比如我们进公司时,将公司代码从代码库拉取下来等

2. 查看状态命令

2.1 git status

通过该命令,可以看出当前本次仓库的状态,例如距离上一次提交之后,如果目前有文件进行改动,则可以通过该命令查看当前相较于暂存区,相较于本地仓库副本等变化的地方

下面我们在上述已经初始化本地仓库的基础上,来观察状态,如下

[root@huangzb ~]# cd mygit/
[root@huangzb mygit]#
[root@huangzb mygit]#
[root@huangzb mygit]# git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
[root@huangzb mygit]#
[root@huangzb mygit]# echo 'hello world' > test.txt
[root@huangzb mygit]#
[root@huangzb mygit]# ls
test.txt
[root@huangzb mygit]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       test.txt
nothing added to commit but untracked files present (use "git add" to track)

可以看出,首先在创建一个新的文件前,我们使用 git status 命令查看了当前工作区的变化,如果我们新创建了文件,再次调用 git status命令后,就会发现会提示出 Untracked files 标识,表示该文件没有被git所跟踪,如果不是被git所跟踪的文件,git是不会有相对应的处理命令的,所以提示需要通过 git add 命令让git识别跟踪,且添加进暂存区。

3. 提交命令

3.1 git add [file]

使用该命令,可以将指定文件或者指定文件夹下的 未跟踪/新创建,已修改,已删除等文件的变化添加到暂存区

下面就演示一个场景,即在上述 git init之后,添加一个新文件(该文件处于未跟踪状态),然后使用 git add命令添加到暂存区,交由git跟踪管理

[root@huangzb mygit]#
[root@huangzb mygit]# git add test.txt
[root@huangzb mygit]#
[root@huangzb mygit]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   test.txt
#
 

从这个例子中,可以看出,通过 git add命令后,该文件就会纳入git跟踪,然后再次调用 git status时发现,不再提示 Untracked files 标识,而是 Changes to be committed,说明该文件已经被成功纳入了暂存区,此时后续有两种操作,要么让其本次 git add操作,要么将文件从暂存区保存到本地仓库中。

3.2 git commit [file] -m [message]

使用该命令,可以将暂存区的修改变动提交到本次仓库,这里的 file可传可不传,如果不传则将暂存区的所有变动文件都提交到版本库中

在上述的添加文件到暂存区后,通过git status 命令可以看出 提示我们需要 commit,现在我们来 commit一下看看效果

[root@huangzb mygit]#
[root@huangzb mygit]# git commit test.txt
 
*** Please tell me who you are.
 
Run
 
  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"
 
to set your account's default identity.
Omit --global to set the identity only in this repository.
 
fatal: unable to auto-detect email address (got 'root@huangzb.(none)')
[root@huangzb mygit]#
 

从上图中可以看出,当我们通过 git commit命令提交时,发现提示要求我们配置两个参数,分别是 user.email,user.name,这些配置的方式我们可以看第4个模块,简单配置下,如下图:

[root@huangzb .git]# git config --local user.name 'huangzb'
[root@huangzb .git]# git config --local user.email 'huangzb@huangzb.com'
[root@huangzb .git]#

现在我们已经配置好了这两个参数后,我们再次执行 git commit命令看看,如下图

Press ENTER or type command to continue
Aborting commit due to empty commit message.
 
      1
      2 # Please enter the commit message for your changes. Lines starting
      3 # with '#' will be ignored, and an empty message aborts the commit.
      4 # Explicit paths specified without -i nor -o; assuming --only paths...
      5 # On branch master
      6 #
      7 # Initial commit
      8 #
      9 # Changes to be committed:
     10 #   (use "git rm --cached <file>..." to unstage)
     11 #
     12 #   new file:   test.txt
     13 #
 

这是什么意思呢,意思就是我们是使用 git commit命令提交时,一定需要添加注释,即为什么提交该段代码等消息,需要添加 -m 参数,所以正确的完成的提交命令是 git commit -m '消息',我们再来试试,如下图

[root@huangzb mygit]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   test.txt
#
[root@huangzb mygit]#
[root@huangzb mygit]# git commit test.txt -m 'first commit'
[master (root-commit) 3858ced] first commit
1 file changed, 1 insertion(+)
create mode 100644 test.txt
[root@huangzb mygit]#
[root@huangzb mygit]# git status
# On branch master
nothing to commit, working directory clean
[root@huangzb mygit]#
 

由上图我们可以看到,通过命令提交后,再次使用 git status查看状态,发现提示 nothing to commit,即当前工作已经处理干净的状态了。

4. 修改配置命令

4.1 git config

配置git的配置文件,例如配置的提交者姓名,邮箱等等信息

我们通过 git config 命令看下有哪些参数

[root@huangzb mygit]# git config
usage: git config [options]
 
Config file location
    --global              use global config file
    --system              use system config file
    --local               use repository config file
    -f, --file <file>     use given config file
    --blob <blob-id>      read config from given blob object
 
Action
    --get                 get value: name [value-regex]
    --get-all             get all values: key [value-regex]
    --get-regexp          get values for regexp: name-regex [value-regex]
    --replace-all         replace all matching variables: name value [value_regex]
    --add                 add a new variable: name value
    --unset               remove a variable: name [value-regex]
    --unset-all           remove all matches: name [value-regex]
    --rename-section      rename section: old-name new-name
    --remove-section      remove a section: name
    -l, --list            list all
    -e, --edit            open an editor
    --get-color <slot>    find the color configured: [default]
    --get-colorbool <slot>
                          find the color setting: [stdout-is-tty]
 

从上图中可以看出,config配置有3个维度,分别是全用户(system),当前用户全局配置(global),当前本地仓库(local)等,从优先级来看是 local>global>system,即使这三个维度都配置了,还是依赖优先原则,哪个最近先使用哪个,什么意思呢,就是如果我们配置了global,也配置了system,那么在当前仓库中,优先使用 local的。

下面我们来看看如果添加配置,删除配置和查看配置等操作

1. 查看当前配置列表

使用命令 git config --local --list 来查看本地仓库的config配置信息

[root@huangzb mygit]# git config --local --list
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
[root@huangzb mygit]#
 

从上图中可以看出当前本地仓库的配置信息,那么该信息是保存在哪里的呢,我们可以在当前本地仓库的 .git/目录中的 config文件中,我们进入该目录,且查看该文件

[root@huangzb mygit]# cd .git
[root@huangzb .git]#
[root@huangzb .git]#
[root@huangzb .git]#
[root@huangzb .git]# ll -a
total 44
drwxr-xr-x 7 root root 4096 Mar 16 10:04 .
drwxr-xr-x 3 root root 4096 Mar 15 21:17 ..
drwxr-xr-x 2 root root 4096 Mar 15 20:39 branches
-rw-r--r-- 1 root root   92 Mar 15 20:39 config
-rw-r--r-- 1 root root   73 Mar 15 20:39 description
-rw-r--r-- 1 root root   23 Mar 15 20:39 HEAD
drwxr-xr-x 2 root root 4096 Mar 15 20:39 hooks
-rw-r--r-- 1 root root  104 Mar 15 21:17 index
drwxr-xr-x 2 root root 4096 Mar 15 20:39 info
drwxr-xr-x 5 root root 4096 Mar 15 21:17 objects
drwxr-xr-x 4 root root 4096 Mar 15 20:39 refs
[root@huangzb .git]# cat config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true

我们可以看到该目录下的config文件里面的内容确实是我们的命令返回的内容,但是不建议大家通过直接修改该文件来操作配置,可以通过命令来操作。

2. 添加/设置 key value

使用命令 git config --local [key value]

下面我们就来添加 commit时需要的两个配置,如下图

[root@huangzb .git]#
[root@huangzb .git]# git config --local user.name 'huangzb'
[root@huangzb .git]# git config --local user.email 'huangzb@huangzb.com'
[root@huangzb .git]#
[root@huangzb .git]# git config --local --list
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
user.name=huangzb
user.email=huangzb@huangzb.com
[root@huangzb .git]#
 

从上图可以看出,我们使用命令 git config --local key value的方式添加了两个配置,也从 git config --local --list中看到了结果。如果我们配错了配置,想要修改怎么办,同样的使用该命令重新设置key value,将原有的配置进行覆盖即可,试试

[root@huangzb .git]# git config --local --list
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
user.name=huangzb
user.email=huangzb@huangzb.com
[root@huangzb .git]#
[root@huangzb .git]# git config --local user.name 'huangzb1'
[root@huangzb .git]#
[root@huangzb .git]# git config --local --list
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
user.name=huangzb1
user.email=huangzb@huangzb.com
[root@huangzb .git]#
 

从上图可知,我们依然通过该命令,修改了 user.name的值

3. 查看指定key对应的配置值

使用命令 git config --local --get key

上面我们通过 --list方式,可以获取所有的config配置,如果我们想要其中某一个配置的值的话,可以使用该命令来单独获取,如下图

[root@huangzb .git]# git config --local --get user.name
huangzb1
[root@huangzb .git]#
 

从上图可以看出,使用了该命令成功获取到了我们配置的 user.name的值

4. 删除指定配置

使用命令 git config --local --unset [key]

如果我们想要删除我们刚才配置的 user.name,可以使用该命令来删除,如下图

[root@huangzb .git]# git config --local --list
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
user.name=huangzb1
user.email=huangzb@huangzb.com
[root@huangzb .git]#
[root@huangzb .git]#
[root@huangzb .git]# git config --local --unset user.name
[root@huangzb .git]#
[root@huangzb .git]# git config --local --list
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
user.email=huangzb@huangzb.com
[root@huangzb .git]#
 

由上图可知,我们通过 unset指令成功删除了 user.email配置

5. 查看日志命令

使用命令 git log 查看我们的提交记录

我们在上述的过程中,完成了提交的流程,一旦我们commit后,会在当前的分支上添加一个新的节点,标识本次提交,那么我们如何查看以前的提交记录呢。可以使用命令 git log,我们来试试看

[root@huangzb mygit]# git log
commit 3858ced83ce0a2396415b5ac3681e032a576eb61
Author: huangzb <huangzb@huangzb.com>
Date:   Mon Mar 16 10:51:30 2020 +0800
 
    first commit
[root@huangzb mygit]#
 

由于我们只提交了一次,所以只有一条记录,那么分别来介绍下该条记录中各个参数的意义。

  1. commit----commitId,即commit的时候,通过sha-1盐值算法计算出来的唯一id,可以用来回滚
  2. Author---提交人的信息,由我们配置的 user.name user.email 组合而成
  3. Date---提交的时间
  4. first commit---提交时的注释信息

我们也可以通过命令 git log -n 来查看前n条提交记录

6. 查看暂存区数据命令

使用命令 git ls-files 查看暂存区数据

参考:https://www.cnblogs.com/panbingwen/p/10736915.html

下图中使用的命令如下:

  1. git ls-files-------列出当前暂存区的文件列表,只有文件名
  2. git ls-files -s --------列出当前暂存区的文件列表,包括了文件id
  3. git cat-file -p [file_id]--------查看指定文件id对应的暂存区文件内容

注意点:

  1. 使用 git cat-file 文件id 命令之前,需要先使用 git-files -s 来查看待查看的文件对应的文件id
  2. 多次多同一个文件修改,然后执行 git add操作,在没有commit之前,后面的 git add操作会将之前的暂存区相同文件覆盖,即丢失之前的暂存区内容
  3. 执行 git commit之后,会清空暂存区内容

现在,我们先添加一个文件,然后添加到暂存区,看看暂存区的数据,如下图

[root@huangzb testgit]# echo 'hello world' > a.txt
[root@huangzb testgit]#
[root@huangzb testgit]# cat a.txt
hello world
[root@huangzb testgit]#
[root@huangzb testgit]# git add a.txt
[root@huangzb testgit]#
[root@huangzb testgit]# git ls-files
a.txt
[root@huangzb testgit]#
[root@huangzb testgit]# git ls-files -s
100644 3b18e512dba79e4c8300dd08aeb37f8e728b8dad 0       a.txt
[root@huangzb testgit]#
[root@huangzb testgit]# git cat-file -p 3b18e5
hello world
[root@huangzb testgit]#
 
 

从上图可以看出,我们添加了一个 a.txt文件,然后使用命令 git add 添加到了暂存区,然后使用命令:**git ls-files ** 查看当前暂存区的所有文件列表,但是这里只有一个文件名,没有其余信息,所以我们添加了一个参数 -s,就显示了其暂存区的文件的唯一id,例如这里就是 3b18e512xxxx等,但是如果我们想要看到暂存区文件里面的内容的时候,就得使用命令 git cat-flie -p [fileId] 来查看其内容

其实我们可以看出来,如果我们多次 git add的话,同一份文件也只是保存一份,那么会将之前添加的内容所覆盖。什么意思呢,我们下面就来演示看看

[root@huangzb testgit]# git commit -m 'first commmit'
[master (root-commit) 1867d84] first commmit
1 file changed, 1 insertion(+)
create mode 100644 a.txt
[root@huangzb testgit]# git ls-files -s
100644 3b18e512dba79e4c8300dd08aeb37f8e728b8dad 0       a.txt
[root@huangzb testgit]# git cat-file -p 3b18
hello world
[root@huangzb testgit]#
[root@huangzb testgit]# echo 'hello 1' > a.txt
[root@huangzb testgit]# git add a.txt
[root@huangzb testgit]#
[root@huangzb testgit]# echo 'hello 2' > a.txt
[root@huangzb testgit]# git add a.txt
[root@huangzb testgit]#
[root@huangzb testgit]# git ls-files -s
100644 c9835dfd7d3c3d547df9ed94479f556fcaf5615d 0       a.txt
[root@huangzb testgit]#
[root@huangzb testgit]# git cat-file -p c9835
hello 2
[root@huangzb testgit]#
 

由上图可以看出,我们有多个操作,分别如下

  1. 先将a.txt 提及到本地仓库
  2. commit了之后再来看暂存区还保留着 a.txt,并不会删除
  3. 随后修改了a.txt两次,且都分别添加到了暂存区
  4. 使用git list-files 可以看到只会有一条记录,且后续的操作会覆盖之前的操作。
原文地址:https://www.cnblogs.com/duguxiaobiao/p/12598532.html