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 "[email protected]"

配置管理

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 [email protected]
      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 "[email protected]"
  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 "[email protected]"
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"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章