Git 使用入门

未完待续...

一、基本概念

二、常用命令

1. 常用命令速查表(英文)

clipboard.png

2. 常用命令速查表(中文)

clipboard.png

三、场景应用

1、配置Git账号

优先级:工程级 > 用户级 > 系统级
  • 系统级配置

    • --system 包含系统上没有给用户及其它们仓库的通用配置
    • 位于 /etc/gitconfig
  • 用户级配置

    • --global 只针对当前用户
    • 位于 ~/.gitconfig 或 ~/.config/git/config
  • 工程级配置

    • 只针对当前仓库
    • 位于当前仓库的 .git/config
  • 查看Git配置

    git config user.name
    git config user.email
  • 添加全局配置

    # Git global setup
    git config --global user.name "example"
    git config --global user.email "admin@example.com"

2、配置Git参数

  • 查看Git配置信息

    git config -l
  • 设置Git缓冲大小

    git config --global http.postBuffer 524288000
  • 启用压缩

    git config --global core.compression -1
  • 启用调试信息

    export GIT_TRACE_PACKET=1
    export GIT_TRACE=1
    export GIT_CURL_VERBOSE=1

3、创建初始仓库

情景:初始仓库内容为空,需添加文件内容至初始仓库。下面列举两种添加方式:
1、克隆空仓库,进入相应文件夹添加文件并上传文件。
2、已存在待上传的项目文件,进入文件夹后以此文件夹内内容作为仓库内容并上传。
  • 流程图
    clipboard.png
  • 操作步骤

    • 创建新的目录和文件并上传至仓库

      # Create a new repository
      git clone http://192.168.193.131/practice/project.git
      cd project
      touch README.md
      git add README.md
      git commit -m "add README"
      git push -u origin master
    • 已存在的目录和文件并上传至仓库

      # Existing folder or Git repository
      cd existing_folder
      git init
      git remote add origin http://192.168.193.131/practice/project.git
      git add .
      git commit
      git push -u origin master

4、获取指定tag的版本

获取 openwrt tag 为 v18.06.2 的分支版本
  • tag 版本
    clipboard.png
  • git 命令

    git clone --branch v18.06.2 https://git.openwrt.org/openwrt/openwrt.git

5、使用代理服务器

情景:当前主机无法连接git服务器或下载速度很慢,而其它主机可以快速访问git服务器
1、Windows 主机安装代理服务器(CCProxy)
2、Git 访问时使用代理参数
  • 设置全局代理

    # 设置代理
    git config --global https.proxy ip:port
    git config --global http.proxy ip:port
    
    # 取消代理
    git config --global --unset https.proxy
    git config --global --unset http.proxy
  • 设置临时代理

    # 仅当前命令生效
    git -c "http.proxy=ip:port" clone --branch v18.06.2 https://git.xxx.com/xxx.git

四、常见问题

1、A TLS packet with unexpected length was received.

# 网络不稳定或缓冲区过小
git config --global http.postBuffer 524288000

五、参考资料

Git 克隆错误

评论

暂无评论