经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 软件/图像 » Git » 查看文章
Git 系列:简介安装以及配置管理
来源:cnblogs  作者:Code技术分享  时间:2024/3/13 9:16:57  对本文有异议

简介安装

简介

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安装

  1. #!/bin/bash
  2. #移除以前的git
  3. yum remove git
  4. #添加依赖包
  5. yum install -y curl-devel expat-devel openssl-devel zlib-devel gcc wget
  6. #创建git源码路径
  7. mkdir -p /server/download/git
  8. #进入git源码路径
  9. cd /server/download/git
  10. #下载git源码
  11. wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.30.0.tar.gz
  12. #解压git源码
  13. tar -zxvf git-2.30.0.tar.gz
  14. #进入源码路径
  15. cd git-2.30.0
  16. #配置
  17. ./configure --prefix=/server/application/git
  18. #编译和编译安装
  19. make && make install
  20. #配置环境变量
  21. echo '#git config.'>>/etc/profile
  22. echo 'export PATH=$PATH:/server/application/git/bin'>>/etc/profile
  23. #立即生效
  24. source /etc/profile
  25. #查看git版本
  26. git --version
  27. #生成密钥
  28. ssh-keygen -t rsa -C "code9342@gmail.com"

配置管理

git help

显示关于 Git 的帮助信息

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

概要

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

选项

  • -a-all

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

      1. git help -a
    • image

  • -c,--config

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

      1. git help -c
      2. git help -c | grep format
    • image

  • -g,--guides

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

      1. git help -g
    • image

  • -i,--info

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

      1. 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]

示例

  1. git help config
  2. git config --help
  3. git --help
  4. 用法:git [--version] [--help] [-C <路径>] [-c <名称>=<取值>]
  5. [--exec-path[=<路径>]] [--html-path] [--man-path] [--info-path]
  6. [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
  7. [--git-dir=<路径>] [--work-tree=<路径>] [--namespace=<名称>]
  8. <命令> [<参数>]
  9. 这些是各种场合常见的 Git 命令:
  10. 开始一个工作区(参见:git help tutorial
  11. clone 克隆仓库到一个新目录
  12. init 创建一个空的 Git 仓库或重新初始化一个已存在的仓库
  13. 在当前变更上工作(参见:git help everyday
  14. add 添加文件内容至索引
  15. mv 移动或重命名一个文件、目录或符号链接
  16. restore 恢复工作区文件
  17. rm 从工作区和索引中删除文件
  18. sparse-checkout 初始化及修改稀疏检出
  19. 检查历史和状态(参见:git help revisions
  20. bisect 通过二分查找定位引入 bug 的提交
  21. diff 显示提交之间、提交和工作区之间等的差异
  22. grep 输出和模式匹配的行
  23. log 显示提交日志
  24. show 显示各种类型的对象
  25. status 显示工作区状态
  26. 扩展、标记和调校您的历史记录
  27. branch 列出、创建或删除分支
  28. commit 记录变更到仓库
  29. merge 合并两个或更多开发历史
  30. rebase 在另一个分支上重新应用提交
  31. reset 重置当前 HEAD 到指定状态
  32. switch 切换分支
  33. tag 创建、列出、删除或校验一个 GPG 签名的标签对象
  34. 协同(参见:git help workflows
  35. fetch 从另外一个仓库下载对象和引用
  36. pull 获取并整合另外的仓库或一个本地分支
  37. push 更新远程引用和相关的对象
  38. 命令 'git help -a' 'git help -g' 显示可用的子命令和一些概念帮助。
  39. 查看 'git help <命令>' 'git help <概念>' 以获取给定子命令或概念的
  40. 帮助。
  41. 有关系统的概述,查看 'git help git'

git-doc

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

git config

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

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

概要

  1. git config [<file-option>] [--type=<type>] [--show-origin] [-z|--null] name [value [value_regex]]
  2. git config [<file-option>] [--type=<type>] --add name value
  3. git config [<file-option>] [--type=<type>] --replace-all name value [value_regex]
  4. git config [<file-option>] [--type=<type>] [--show-origin] [-z|--null] --get name [value_regex]
  5. git config [<file-option>] [--type=<type>] [--show-origin] [-z|--null] --get-all name [value_regex]
  6. git config [<file-option>] [--type=<type>] [--show-origin] [-z|--null] [--name-only] --get-regexp name_regex [value_regex]
  7. git config [<file-option>] [--type=<type>] [-z|--null] --get-urlmatch name URL
  8. git config [<file-option>] --unset name [value_regex]
  9. git config [<file-option>] --unset-all name [value_regex]
  10. git config [<file-option>] --rename-section old_name new_name
  11. git config [<file-option>] --remove-section name
  12. git config [<file-option>] [--show-origin] [-z|--null] [--name-only] -l | --list
  13. git config [<file-option>] --get-color name [default]
  14. git config [<file-option>] --get-colorbool name [stdout-is-tty]
  15. git config [<file-option>] -e | --edit

选项

  • --add

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

      1. [robin->mcode-server->~]$ mkdir git-test
      2. [robin->mcode-server->~]$ cd git-test/
      3. [robin->mcode-server->~/git-test]$ git init
      4. Initialized empty Git repository in /Users/robin/git-test/.git/
      5. [robin->mcode-server->~/git-test]$ git config user.name
      6. robin
      7. [robin->mcode-server->~/git-test]$ git config --add user.name luobin
      8. [robin->mcode-server->~/git-test]$ git config user.name
      9. luobin
    • image

  • --get

    • 获取给定键的值

      1. [robin->mcode-server->~/git-test]$ git config --get user.name
      2. luobin1
      3. [robin->mcode-server->~/git-test]$ git config user.name
      4. luobin1

    -image

  • --get-all

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

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

  • --get-regexp

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

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

  • --global

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

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

      1. [robin->mcode-server->~/git-test]$ git config --global user.name
      2. robin
      3. [robin->mcode-server->~/git-test]$ git config --global user.name robin1
      4. [robin->mcode-server->~/git-test]$ git config --global user.name
      5. 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

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

      1. git config -l
    • image

  • --remove-section

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

      1. [robin->mcode-server->~/git-test]$ git config --local -l
      2. core.repositoryformatversion=0
      3. core.filemode=true
      4. core.bare=false
      5. core.logallrefupdates=true
      6. core.ignorecase=true
      7. core.precomposeunicode=true
      8. user.name=luobin
      9. [robin->mcode-server->~/git-test]$ git config --remove-section user
      10. [robin->mcode-server->~/git-test]$ git config --local -l
      11. core.repositoryformatversion=0
      12. core.filemode=true
      13. core.bare=false
      14. core.logallrefupdates=true
      15. core.ignorecase=true
      16. core.precomposeunicode=true
    • image

  • --rename-section

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

      1. [robin->mcode-server->~/git-test]$ git config --rename-section user user.email
      2. [robin->mcode-server->~/git-test]$ git config --local -l
      3. core.repositoryformatversion=0
      4. core.filemode=true
      5. core.bare=false
      6. core.logallrefupdates=true
      7. core.ignorecase=true
      8. core.precomposeunicode=true
      9. user.email.name=robin
    • image

  • --unset

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

      1. [robin->mcode-server->~/git-test]$ git config --local -l
      2. core.repositoryformatversion=0
      3. core.filemode=true
      4. core.bare=false
      5. core.logallrefupdates=true
      6. core.ignorecase=true
      7. core.precomposeunicode=true
      8. user.name=robin
      9. [robin->mcode-server->~/git-test]$ git config --unset user.name
      10. [robin->mcode-server->~/git-test]$ git config --local -l
      11. core.repositoryformatversion=0
      12. core.filemode=true
      13. core.bare=false
      14. core.logallrefupdates=true
      15. core.ignorecase=true
      16. core.precomposeunicode=true
    • image

  • --unset-all

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

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

      1. 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 用于查看或设置全局的用户名。

示例

初始化配置

  1. git config --global user.name "robin"
  2. git config --global user.email "code9342@gmail.com"
  3. git config --global core.editor "vim"
  4. git config --global alias.co "checkout"
  5. git config --global alias.ci "commit"
  6. git config --global alias.cm "commit -m"
  7. git config --global alias.ps "push"
  8. git config --global alias.pl "pull"
  9. git config --global alias.st "status"
  10. git config --global alias.last "log -1 HEAD"
  11. git config --global alias.unstage "reset HEAD --"
  12. git config --global alias.df "diff"
  13. git config --global alias.dfw "diff --color-words"
  14. git config --global alias.dfc "diff --cached"
  15. git config --global alias.dfl "diff HEAD~"
  16. git config --global alias.br "branch"
  17. git config --global alias.hist "log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short"
  18. git config --global alias.ad "add"
  19. 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"
  20. git config --global alias.type "cat-file -t"
  21. git config --global alias.dump "cat-file -p"
  22. git config --global gui.encoding "utf-8"
  23. git config --global il8n.commitEncoding "utf-8"
  24. git config --global il8n.logOutputEncoding "utf-8"
  25. git config --global alias.cl "clone"

原文链接:https://www.cnblogs.com/vic-tory/p/18067304

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号