官术网_书友最值得收藏!

  • Git Version Control Cookbook
  • Aske Olsson Rasmus Voss
  • 359字
  • 2021-12-08 12:33:50

Querying the existing configuration

In this example, we will look at how we can query the existing configuration and set the configuration values.

Getting ready

We'll use jgit again by using the following command:

$ cd jgit

How to do it...

To view all the effective configurations for the current Git repository, run the following command:

$ git config --list
user.name=Aske Olsson
user.email=askeolsson@switch-gears.dk
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
remote.origin.url=https://git.eclipse.org/r/jgit/jgit
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master

The previous output will of course reflect the user running the command. Instead of Aske Olsson as the name and the e-mail, the output should reflect your settings.

If we are just interested in a single configuration item, we can just query it by its section.key or section.subsection.key:

$ git config user.name
Aske Olsson
$ git config remote.origin.url
https://git.eclipse.org/r/jgit/jgit

How it works...

Git's configuration is stored in plaintext files, and works like a key-value storage. You can set/query by key and get the value back. An example of the text-based configuration file is shown as follows (from the jgit repository):

$ cat .git/config
[core]
 repositoryformatversion = 0
 filemode = false
 bare = false
 logallrefupdates = true
[remote "origin"]
 url = https://git.eclipse.org/r/jgit/jgit
 fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
 remote = origin
 merge = refs/heads/master

There's more...

It is also easy to set configuration values. Just use the same syntax as you did when querying the configuration, except you need to add an argument to the value. To set a new e-mail address on the LOCAL layer, we can execute the following command line:

git config user.email askeolsson@example.com

The LOCAL layer is the default if nothing else is specified. If you require whitespaces in the value, you can enclose the string in quotation marks, as you would do when configuring your name:

git config user.name "Aske Olsson"

You can even set your own configuration, which does not have any effect on the core Git, but can be useful for scripting/builds and so on:

$ git config my.own.config "Whatever I need"

List the value

$ git config my.own.config 
Whatever I need

It is also very easy to delete/unset configuration entries:

$ git config --unset my.own.config

List the value

$ git config my.own.config 
主站蜘蛛池模板: 龙胜| 攀枝花市| 齐齐哈尔市| 新丰县| 厦门市| 曲麻莱县| 会宁县| 南木林县| 应城市| 土默特左旗| 汕尾市| 青铜峡市| 钦州市| 邯郸县| 武义县| 五莲县| 龙山县| 灵丘县| 安庆市| 宁陵县| 绿春县| 大竹县| 浪卡子县| 刚察县| 临城县| 晋宁县| 普定县| 平南县| 内乡县| 沂水县| 楚雄市| 武鸣县| 文成县| 太原市| 雷山县| 青神县| 莒南县| 和政县| 揭西县| 固安县| 保靖县|