1

『现学现忘』Git基础 — 12、Git用户签名的其他操作 - 繁华似锦Fighting

 2 years ago
source link: https://www.cnblogs.com/liuyuelinfighting/p/16169058.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

1、修改用户签名

其实很简单,就是重新执行git config命令,换个用户名和邮箱地址就可以了,新配置的内容会覆盖之前配置的内容,达到了用户签名的修改。

以系统用户为例:

bash
# 查看系统级别的用户配置
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git config --list --system
diff.astextplain.textconv=astextplain
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
http.sslbackend=openssl
http.sslcainfo=F:/DevInstall/Git/GitforWindows/mingw64/ssl/certs/ca-bundle.crt
core.autocrlf=true
core.fscache=true
core.symlinks=false
credential.helper=manager
user.name=tang_s  # 这里是tang_s
[email protected]

# 修改系统用户配置
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git config --system user.name 'zhu_bj'

# 查看系统级别的用户配置
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git config --list --system
diff.astextplain.textconv=astextplain
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
http.sslbackend=openssl
http.sslcainfo=F:/DevInstall/Git/GitforWindows/mingw64/ssl/certs/ca-bundle.crt
core.autocrlf=true
core.fscache=true
core.symlinks=false
credential.helper=manager
user.name=zhu_bj	# 这里是zhu_bj
[email protected]
  • 系统级别的配置对应的是所有操作系统的用户,全局配置对应的是单个系统用户对所有Git仓库的配置,本地配置是对单个Git仓库的配置。
  • 所以综上所述,在一台计算机中,系统用户只能有一个,全局用户是每一个用户可以设置一个,本地用户可以设置多个(一个本地版本库对应一个)。

2、取消用户签名

语法:

brainfuck
git config --配置文件范围 --unset user.name
git config --配置文件范围 --unset user.email

示例:取消系统用户的用户名和邮箱设置:

bash
# 取消系统用户的用户名和邮箱设置
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git config --system --unset user.name

L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git config --system --unset user.email

# 查看系统级别的用户配置,可以看到没有系统用户的配置了。
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git config --list --system
diff.astextplain.textconv=astextplain
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
http.sslbackend=openssl
http.sslcainfo=F:/DevInstall/Git/GitforWindows/mingw64/ssl/certs/ca-bundle.crt
core.autocrlf=true
core.fscache=true
core.symlinks=false
credential.helper=manager

提示:全局用户同理,本地用户需要到仓库中执行命令,也是同理的。

3、用户签名的优先级

之前我们讲过执行git config命令有三个作用域,分别是local、global、system

接下来,要探讨的是,这三个配置作用域的优先级。

(1)先查看本机Git用户签名配置

shell
# 查看系统用户签名
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git config --list --system
diff.astextplain.textconv=astextplain
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
http.sslbackend=openssl
http.sslcainfo=F:/DevInstall/Git/GitforWindows/mingw64/ssl/certs/ca-bundle.crt
core.autocrlf=true
core.fscache=true
core.symlinks=false
credential.helper=manager
user.name=tang_s
[email protected]

# 查看全局用户签名
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git config --list --global
user.name=sun_wk
[email protected]

# 查看本地用户签名
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git config --list --local
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
user.name=sha_hs
[email protected]

可以看到:

  • 系统用户签名:tang_s
  • 全局用户签名:sun_wk
  • 本地用户签名:sha_hs

(2)创建一个文件,提交到本地版本库中。

1)在仓库中创建一个文本。

bash
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ touch test.java

L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ ll
total 0
-rw-r--r-- 1 L 197121 0  4月  3 10:54 test.java

2)提交这个文本到本地Git仓库中。

bash
# 1.查看工作目录和暂存区中文件的状态,
# 发现工作区中有一个test.java未被git管理
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        test.java

nothing added to commit but untracked files present (use "git add" to track)

# 2.把test.java添加到暂存区,被git纳入管理
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git add test.java

# 3.再次查看工作目录和暂存区中文件的状态,
# 可以看到test.java文件被git管理
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   test.java

# 4.把test.java提交到本地版本库中
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git commit -m 'add test.java'
[master (root-commit) e85b3b2] add test.java
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.java

说明上边的一些列操作:这里简单解释一下,之后会详细说明。
使用Git的好处之一就是,如果出错了,它的提示信息是非常详细的,认真的耐心的看一看,就能够解决。

  • 执行$ git status命令:用于显示工作目录和暂存区中文件的状态。

    文件是红色的,意思是test.java文件还没有被Git管控,这个时候直接使用git commit命令是不能生效的。
  • 将文件加入Git的暂存区。
    这时就要对新加入的文件执行git add + 文件名,让Git对该文件进行管控,在看一下Git的状态。

    绿色表示该文件已经被Git管理了,该文件已经在Git的暂存区当中了(stage)。
  • 此时便可以对test.java文件生成一个正式的commit了。
    执行$ git commit -m 'add Test_text'
    -m :就是说你这此变更的理由或者原因是什么。

    提示master分支根部的commit已经创建出来了。

(3)查看Git日志。

执行git log命令,查看Git日志。

bash
L@DESKTOP-T2AI2SU MINGW64 /j/git-repository/learngit (master)
$ git log
commit e85b3b21a4b296acacf34bf9caeeaa75544a2f2b (HEAD -> master)
Author: sha_hs <[email protected]>
Date:   Sat Apr 3 10:55:55 2021 +0800

    add test.java

我们可以看到,在systemgloballocal这三个用户签名都存在的情况下,是通过本地用户进行的提交,所以说明本地用户sha_hs的优先级最高。

  • 黄颜色的一串数字是本次commitid号。
  • Author表示完成这一次提交的作者。

(4)配置文件级别的优先级总结。

  1. 就近原则:
    项目(本地)级别优先于用户(全局)级别,用户级别优先于系统级别。
    三者都有时采用项目级别的签名。
  2. 如果只有用户级别的签名,就以用户级别的签名为准。
  3. 三者都没有签名,不允许,无法提交变更到本地版本库。

4、总结本文用到的Git命令

序号 Git命令 说明

1 $ git status 查看当前工作区和暂存区文件的状态

2 $ git add 可以将跟踪到的更新放到暂存区(更新包括新增、修改、删除等操作)

3 $ git commit -m 'add Test_text' 提交更新

4 $ git log 查看提交历史

执行git status命令后,出现“Changed but not updated” t提示,说明已跟踪文件的内容发生了变化,但还没有放到暂存区。

__EOF__


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK