环境配置与仓库初始化
#git
#配置
#克隆
#初始化
环境配置与仓库初始化#
克隆仓库#
使用 git clone 命令将远程仓库复制到本地。
# 克隆远程仓库到当前目录
# <仓库地址> 可以是 HTTPS 或 SSH 格式,如 https://github.com/user/repo.git
git clone <仓库地址>
# 克隆远程仓库到指定目录
# <directory> 是本地目标目录名,会自动创建
git clone <仓库地址> <directory>
配置网络代理#
如果无法正常克隆仓库,需要设置网络代理。查看你科学上网软件的本地端口(一般在设置里,比如 7890),然后执行:
# ==================== HTTP 代理 ====================
# 设置 HTTP 协议的代理地址
# --global 表示全局配置,对所有仓库生效
git config --global http.proxy http://127.0.0.1:7890
# 设置 HTTPS 协议的代理地址
git config --global https.proxy http://127.0.0.1:7890
# ==================== SOCKS5 代理 ====================
# 根据你代理软件的协议选择 HTTP 或 SOCKS5
# SOCKS5 代理支持更多协议,但需要代理软件支持
git config --global http.proxy socks5://127.0.0.1:7890
git config --global https.proxy socks5://127.0.0.1:7890
# ==================== 取消代理 ====================
# 如果不再需要代理,可以取消设置
git config --global --unset http.proxy
git config --global --unset https.proxy
💡 提示
代理配置说明
127.0.0.1是本机回环地址,表示代理运行在本地7890是常见的代理端口,请根据你的代理软件实际端口修改--global表示全局配置,如果只想为某个仓库设置代理,去掉--global即可
设置用户信息#
Git 需要知道你是谁,才能记录每次提交的作者信息。
# 设置提交代码时的用户名
# --global 表示全局配置,对所有仓库生效
git config --global user.name "your-name"
# 设置提交代码时的用户邮箱
# 建议使用与 GitHub/GitLab 账号关联的邮箱
git config --global user.email your-email@example.com
⚠️ 注意
重要提示
用户信息必须在首次提交前设置,否则提交记录中会使用系统默认用户名,可能导致提交无法关联到你的账号。
Git 配置层级#
Git 的配置分三个层级,优先级从高到低为:仓库级 > 全局级 > 系统级
| 级别 | 命令参数 | 配置文件位置 | 作用范围 |
|---|---|---|---|
| 系统级 | --system | C:\Program Files\Git\etc\gitconfig | 所有用户 |
| 全局级 | --global | C:\Users\你的用户名\.gitconfig | 当前用户的所有仓库 |
| 仓库级 | 无参数 | 项目目录 .git/config | 仅当前仓库 |
# 查看所有配置(合并结果)
git config --list
# 查看某个层级的配置
git config --global --list # 查看全局配置
git config --local --list # 查看当前仓库配置
# 查看某个具体配置项的值
git config user.name
创建仓库#
方式一:本地初始化#
# 进入项目目录
cd my-project
# 使用当前目录作为 Git 仓库
# 执行后会在当前目录生成一个 .git 隐藏目录
git init
# 或者在指定目录创建 Git 仓库
# <目录名> 不存在时会自动创建
git init <目录名>
方式二:克隆远程仓库#
# 从远程仓库克隆,自动初始化本地仓库
git clone https://github.com/user/repo.git
✨ 技巧
两种方式的区别
git init:从零开始创建新仓库,适合新项目git clone:从已有远程仓库复制,适合参与已有项目
常用配置命令速查#
# ==================== 查看配置 ====================
git config --list # 查看所有配置
git config --global --list # 查看全局配置
git config user.name # 查看用户名
git config user.email # 查看邮箱
# ==================== 修改配置 ====================
git config --global user.name "new-name" # 修改用户名
git config --global user.email new@email.com # 修改邮箱
# ==================== 删除配置 ====================
git config --global --unset user.name # 删除某个配置项
# ==================== 设置别名 ====================
# 为常用命令设置简短别名,提高效率
git config --global alias.st status # git st = git status
git config --global alias.co checkout # git co = git checkout
git config --global alias.br branch # git br = git branch
git config --global alias.ci commit # git ci = git commit
相关笔记: