Rinne's Blog

Back

Git 的安装与配置#

安装#

Linux 平台#

Debian/Ubuntu Git 安装最新稳定版本命令为:

sudo apt-get install git
bash

或在参考官网中其他安装方式

Windows 平台#

前往官网直接下载安装包

Git 配置#

Git 提供了一个叫做 git config 的命令,用来配置或读取相应的工作环境变量。

配置用户信息#

配置个人的用户名称和电子邮件地址,这是为了在每次提交代码时记录提交者的信息:

git config --global user.name "Your Name"
git config --global user.email "Your Email"
bash

配置代理#

Git 默认情况下是不使用代理的,如果需要使用代理,可以按照如下步骤进行配置:

# 配置Git使用HTTP代理
git config --global http.proxy http://127.0.0.1:7897
# 配置Git使用SOCKS5代理
git config --global http.proxy 'socks5://127.0.0.1:7897'
bash

验证安装与配置#

在终端或命令行中运行以下命令,确保 Git 已正确安装并配置:

git --version
git config --list
bash

Git 的基本操作#

创建仓库#

在需要创建仓库的文件夹中,运行以下命令:

git init
bash

或者从远程仓库中拷贝一个仓库:

git clone <remote_repo_url>
bash

提交与修改#

添加文件到暂存区:

# 添加一个或多个文件到暂存区
git add <file_name>
# 添加所有文件到暂存区
git add .
# 添加指定目录下的所有文件到暂存区
git add [dir]
# 添加指定目录下的所有文件到暂存区,并忽略指定文件
git add [dir] --ignore-file [file]
bash

提交暂存区中的文件:

# 提交暂存区中的文件
git commit -m "commit message"
# 提交指定文件
git commit [file1] [file2] ... -m [message]
bash

查看 Git 仓库当前状态:

git status
# 显示 Git 仓库当前状态的简略信息
git status -s
bash

回退版本

# 回退到上一个版本
git reset HEAD^
# --hard 参数撤销工作区中所有未提交的修改内容,将暂存区与工作区都回到上一次版本,并删除之前的所有信息提交 
git reset --hard HEAD~1
# HEAD表示当前版本,HEAD~1, HEAD^ 表示上一次提交
bash

远程操作#

Git 其他配置#

git clone使用代理url#

# 设置代理前缀自动替换
git config --global url."https://ghproxy.net/https://github.com/".insteadOf "https://github.com/"
bash

使用示例

# 直接运行原始命令,Git 会自动替换 URL
git clone https://github.com/mlcommons/storage.git
# 实际等效于:
git clone https://ghproxy.net/https://github.com/mlcommons/storage.git
bash

取消代理

git config --global --unset url."https://ghproxy.net/https://github.com/".insteadOf
bash

参考资料#

  1. Git 官方文档
  2. Git 教程
  3. git clone使用代理url
©
Git 使用笔记
https://astro-pure.js.org/blog/technology/git-notes
Author Rinne
Published at 2025年10月10日
Comment seems to stuck. Try to refresh?✨