Git 系列:简介安装以及配置管理

简介安装

简介

https://git-scm.com/

Git是一个开源的分布式版本控制系统,用于有效、高速地处理从小到大的项目版本管理。它最初由Linux之父Linus Torvalds设计开发,旨在帮助管理Linux内核开发。Git采用了分布式版本库的方式,不需要服务器端软件支持,每个开发人员都可以在自己的计算机上拥有完整的项目副本,独立进行开发、测试和修改。

Git的主要特点包括:

  1. 速度:Git在版本控制方面表现出色,能够高效地处理大规模项目。
  2. 简单的设计:Git具有直观易用的设计,使得开发人员能够轻松上手。
  3. 对非线性开发模式的强力支持:Git允许成千上万个并行开发的分支,为团队协作提供了极大的灵活性。
  4. 完全分布式:Git的分布式特性使得代码管理和版本控制更加灵活和可靠。

Git的基本概念包括仓库(Repository)、提交(Commit)和分支(Branch)。仓库是Git中存储项目代码和历史记录的地方;提交代表着一次代码修改的记录;分支则是基于主线(通常是master分支)的一个代码分支。

Git的主要功能包括从服务器上克隆完整的Git仓库到单机上,创建和合并分支,提交代码,生成补丁等。此外,Git还可以记录软件添加或更改源代码的过程,回滚到特定阶段,恢复误删除的文件等。

Centos安装

#!/bin/bash
#移除以前的git
yum remove git
#添加依赖包
yum install -y curl-devel expat-devel openssl-devel zlib-devel gcc wget
#创建git源码路径
mkdir -p /server/download/git
#进入git源码路径
cd /server/download/git
#下载git源码
wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.30.0.tar.gz
#解压git源码
tar -zxvf git-2.30.0.tar.gz
#进入源码路径
cd git-2.30.0
#配置
./configure --prefix=/server/application/git
#编译和编译安装
make && make install
#配置环境变量
echo '#git config.'>>/etc/profile
echo 'export PATH=$PATH:/server/application/git/bin'>>/etc/profile
#立即生效
source /etc/profile
#查看git版本
git --version
#生成密钥
ssh-keygen -t rsa -C "code9342@gmail.com"

配置管理

git help

显示关于 Git 的帮助信息

https://git-scm.com/docs/git-help

概要

git help [-a|--all [--[no-]verbose]]
	   [[-i|--info] [-m|--man] [-w|--web]] [COMMAND|GUIDE]
git help [-g|--guides]
git help [-c|--config]

选项

  • -a-all

    • 在标准输出上打印所有可用的命令。此选项会覆盖任何给定的命令或指南名称

    • git help -a
      
    • image

  • -c,–config

    • 列出所有可用的配置变量。这是 git-confi 中列表的简短摘要

    • git help -c
      git help -c | grep format
      
    • image

  • -g,–guides

    • 在标准输出上打印 Git 概念指南的列表。

    • git help -g
      
    • image

  • -i,–info

    • 以信息格式显示命令的手册页。

    • git help -i
      
  • -m,–man

    • 以 man 格式显示命令的手册页。此选项可用于覆盖help.format配置变量中设置的 值。

      默认情况下,man 程序将用于显示手册页,但man.viewer配置变量可用于选择其他显示程序

  • -w,–web

    • 以 Web(HTML) 格式显示命令的手册页。为此将使用网络浏览器。
    • 可以使用配置变量指定 Web 浏览器 help.browser,或者web.browser如果未设置前者。如果没有设置这些配置变量,git web–browse帮助脚本(由 git help 调用)将选择一个合适的默认值。有关这方面的更多信息,请参阅 [git-web–browse1]

示例

git help config
git config --help
git --help
用法:git [--version] [--help] [-C <路径>] [-c <名称>=<取值>]
           [--exec-path[=<路径>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<路径>] [--work-tree=<路径>] [--namespace=<名称>]
           <命令> [<参数>]

这些是各种场合常见的 Git 命令:

开始一个工作区(参见:git help tutorial)
   clone             克隆仓库到一个新目录
   init              创建一个空的 Git 仓库或重新初始化一个已存在的仓库

在当前变更上工作(参见:git help everyday)
   add               添加文件内容至索引
   mv                移动或重命名一个文件、目录或符号链接
   restore           恢复工作区文件
   rm                从工作区和索引中删除文件
   sparse-checkout   初始化及修改稀疏检出

检查历史和状态(参见:git help revisions)
   bisect            通过二分查找定位引入 bug 的提交
   diff              显示提交之间、提交和工作区之间等的差异
   grep              输出和模式匹配的行
   log               显示提交日志
   show              显示各种类型的对象
   status            显示工作区状态

扩展、标记和调校您的历史记录
   branch            列出、创建或删除分支
   commit            记录变更到仓库
   merge             合并两个或更多开发历史
   rebase            在另一个分支上重新应用提交
   reset             重置当前 HEAD 到指定状态
   switch            切换分支
   tag               创建、列出、删除或校验一个 GPG 签名的标签对象

协同(参见:git help workflows)
   fetch             从另外一个仓库下载对象和引用
   pull              获取并整合另外的仓库或一个本地分支
   push              更新远程引用和相关的对象

命令 'git help -a''git help -g' 显示可用的子命令和一些概念帮助。
查看 'git help <命令>''git help <概念>' 以获取给定子命令或概念的
帮助。
有关系统的概述,查看 'git help git'

git-doc

#git添加doc文件维护
mkdir -p /server/application/git/share/doc
cd /server/application/git/share/doc
git clone git://git.kernel.org/pub/scm/git/git-htmldocs.git git-doc
git config --global help.htmlpath /server/application/git/share/doc/git-doc
git config --global help.format html
#安装命令行浏览器w3m或links或lynx
yum -y install w3m  #Shift+Q退出

git config

获取和设置仓库或者全局选项

https://git-scm.com/docs/git-config

概要

git config [<file-option>] [--type=<type>] [--show-origin] [-z|--null] name [value [value_regex]]
git config [<file-option>] [--type=<type>] --add name value
git config [<file-option>] [--type=<type>] --replace-all name value [value_regex]
git config [<file-option>] [--type=<type>] [--show-origin] [-z|--null] --get name [value_regex]
git config [<file-option>] [--type=<type>] [--show-origin] [-z|--null] --get-all name [value_regex]
git config [<file-option>] [--type=<type>] [--show-origin] [-z|--null] [--name-only] --get-regexp name_regex [value_regex]
git config [<file-option>] [--type=<type>] [-z|--null] --get-urlmatch name URL
git config [<file-option>] --unset name [value_regex]
git config [<file-option>] --unset-all name [value_regex]
git config [<file-option>] --rename-section old_name new_name
git config [<file-option>] --remove-section name
git config [<file-option>] [--show-origin] [-z|--null] [--name-only] -l | --list
git config [<file-option>] --get-color name [default]
git config [<file-option>] --get-colorbool name [stdout-is-tty]
git config [<file-option>] -e | --edit

选项

  • –add

    • 向选项添加新行而不更改任何现有值

    • [robin->mcode-server->~]$ mkdir git-test
      [robin->mcode-server->~]$ cd git-test/
      [robin->mcode-server->~/git-test]$ git init 
      Initialized empty Git repository in /Users/robin/git-test/.git/
      [robin->mcode-server->~/git-test]$ git config user.name
      robin
      [robin->mcode-server->~/git-test]$ git config --add user.name luobin 
      [robin->mcode-server->~/git-test]$ git config user.name
      luobin
      
    • image

  • –get

    • 获取给定键的值

    • [robin->mcode-server->~/git-test]$ git config --get user.name
      luobin1
      [robin->mcode-server->~/git-test]$ git config  user.name
      luobin1
      

    -image

  • –get-all

    • 与 get 类似,但返回多值键的所有值

    • [robin->mcode-server->~/git-test]$ git config --get-all user.name
      robin
      luobin
      luobin1
      
    • image

  • –get-regexp

    • 与 --get-all 类似,但将名称解释为正则表达式并写出键名称

    • [robin->mcode-server->~/git-test]$ git config --get-regexp user
      user.name robin
      user.email code9342@gmail.com
      user.name luobin
      user.name luobin1
      
    • image

  • –global

    • 对于写入选项:写入全局~/.gitconfig文件而不是存储库

    • 对于读取选项:仅从全局文件~/.gitconfig

    • [robin->mcode-server->~/git-test]$ git config --global user.name 
      robin
      [robin->mcode-server->~/git-test]$ git config --global user.name robin1
      [robin->mcode-server->~/git-test]$ git config --global user.name 
      robin1
      
    • image

  • –system

    • 对于写入选项:写入系统范围 $(prefix)/etc/gitconfig而不是存储库 .git/config
    • 对于读取选项:仅从系统范围读取,$(prefix)/etc/gitconfig 而不是从所有可用文件读取
  • –local

    • 对于写入选项:写入存储库.git/config文件。这是默认行为
    • 对于读取选项:仅从存储库读取,.git/config而不是从所有可用文件读取
  • -f config-file,–file config-file

    • 对于写入选项:写入指定文件而不是存储库.git/config
  • -l,–list

    • 列出配置文件中设置的所有变量及其值

    • git config -l
      
    • image

  • –remove-section

    • 从配置文件中删除给定的部分

    • [robin->mcode-server->~/git-test]$ git config --local -l
      core.repositoryformatversion=0
      core.filemode=true
      core.bare=false
      core.logallrefupdates=true
      core.ignorecase=true
      core.precomposeunicode=true
      user.name=luobin
      [robin->mcode-server->~/git-test]$ git config --remove-section user
      [robin->mcode-server->~/git-test]$ git config --local -l
      core.repositoryformatversion=0
      core.filemode=true
      core.bare=false
      core.logallrefupdates=true
      core.ignorecase=true
      core.precomposeunicode=true
      
    • image

  • –rename-section

    • 将给定部分重命名为新名称

    • [robin->mcode-server->~/git-test]$ git config --rename-section user user.email
      [robin->mcode-server->~/git-test]$ git config --local -l
      core.repositoryformatversion=0
      core.filemode=true
      core.bare=false
      core.logallrefupdates=true
      core.ignorecase=true
      core.precomposeunicode=true
      user.email.name=robin
      
    • image

  • –unset

    • 从配置文件中删除与密钥匹配的行

    • [robin->mcode-server->~/git-test]$ git config --local -l                       
      core.repositoryformatversion=0
      core.filemode=true
      core.bare=false
      core.logallrefupdates=true
      core.ignorecase=true
      core.precomposeunicode=true
      user.name=robin
      [robin->mcode-server->~/git-test]$ git config --unset user.name 
      [robin->mcode-server->~/git-test]$ git config --local -l 
      core.repositoryformatversion=0
      core.filemode=true
      core.bare=false
      core.logallrefupdates=true
      core.ignorecase=true
      core.precomposeunicode=true
      
    • image

  • –unset-all

    • 从配置文件中删除与密钥匹配的行
  • -e,–edit

    • 打开编辑器来修改指定的配置文件;--local(默认)、--system--global

    • git config -e 
      

变量

https://git-scm.com/docs/git-config#_variables

Git 中常用的变量主要涉及到配置选项,这些配置选项决定了 Git 在不同场景下的行为。以下是一些常用的 Git 配置变量及其说明:

  1. user.name和 user.email
    • 这两个变量用于设置提交代码时使用的用户名和邮箱地址。它们通常设置为全局变量,以便在所有仓库中使用相同的身份信息进行提交。
    • 设置方法:git config --global user.name "Your Name"git config --global user.email "your.email@example.com"
  2. core.editor
    • 这个变量用于设置 Git 命令使用的默认文本编辑器,例如当 Git 需要你输入提交信息时。
    • 设置方法:git config --global core.editor "vim"(这里以 vim 为例)
  3. color.ui
    • 这个变量用于控制 Git 命令输出中的颜色显示。设置为 auto 可以让 Git 根据终端是否支持颜色来自动决定是否显示颜色。
    • 设置方法:git config --global color.ui auto
  4. alias
    • 这个变量用于设置 Git 命令的别名,以便用更简短的命令执行常用操作。
    • 设置方法:git config --global alias.st status(这里将 status 命令缩写为 st
  5. credential.helper
    • 这个变量用于配置 Git 的凭据助手,以便在需要时自动提供认证信息,如用户名和密码。
    • 设置方法可能因操作系统和所使用的凭据管理器而异。
  6. remote.origin.url
    • 这个变量用于设置或查看当前仓库远程仓库的 URL。当你克隆一个仓库时,Git 会自动设置这个变量。
    • 查看方法:git config --get remote.origin.url
    • 设置方法通常通过 git remote set-url origin <new_url> 来实现。

这些变量通常存放在三个不同的位置:系统级别的配置文件(如 /etc/gitconfig),用户级别的配置文件(如 ~/.gitconfig),以及当前仓库级别的配置文件(即 .git/config 文件位于仓库根目录下)。不同级别的配置会按照特定的优先级进行覆盖,通常仓库级别的配置会覆盖用户级别和系统级别的同名配置。

要查看或修改这些配置变量,你可以使用 git config 命令,并结合 --system--global--local(默认)选项来指定配置的作用范围。例如,git config --global user.name 用于查看或设置全局的用户名。

示例

初始化配置

git config --global user.name "robin"
git config --global user.email "code9342@gmail.com"
git config --global core.editor "vim"
git config --global alias.co "checkout"
git config --global alias.ci "commit"
git config --global alias.cm "commit -m"
git config --global alias.ps "push"
git config --global alias.pl "pull"
git config --global alias.st "status"
git config --global alias.last "log -1 HEAD"
git config --global alias.unstage "reset HEAD --"
git config --global alias.df "diff"
git config --global alias.dfw "diff --color-words"
git config --global alias.dfc "diff --cached"
git config --global alias.dfl "diff HEAD~"
git config --global alias.br "branch"
git config --global alias.hist "log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short"
git config --global alias.ad "add"
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)[%an]%Creset' --abbrev-commit --date=relative"
git config --global alias.type "cat-file -t"
git config --global alias.dump "cat-file -p"
git config --global gui.encoding "utf-8"
git config --global il8n.commitEncoding "utf-8"
git config --global il8n.logOutputEncoding "utf-8"
git config --global alias.cl "clone"
  • 25
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值