Git 简单使用
· 阅读需 2 分钟
Git 配置
查看 Git 的配置所在文件:git config --list --show-origin.
Git 的配置信息存储在四个地方:
- 系统级:
<Git 安装路径>/etc/gitconfig - 用户级:
~/.gitconfig - 仓库级:仓库根目录下的
.git/config - 工作树级:仓库根目录下的
.git/config.worktree
配置优先级:工作树级 > 仓库级 > 用户级 > 系统级。
使用 git config 命令读取和写入配置的时候加上 --system, --global, --local, --worktree 选项可实现不同级别的配置。
git config --list
git config --system --list
git config --global --list
Git 常用命令
生成密钥对:
ssh-keygen -t ed25519 -C "your_email@example.com"
ssh -i ~/.ssh/id_ed25519 -T git@github.com
初始化配置:
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
在命令行中使用代理:
git -c http.proxy=socks5h://127.0.0.1:7897 fetch
git -c core.sshCommand="ssh -i ~\\.ssh\\id_ed25519" clone git@github.com:xxx/xxx.git
代理配置写入配置文件:
git config http.proxy socks5h://127.0.0.1:7897
git config https.proxy socks5h://127.0.0.1:7897
git config core.sshCommand 'ssh -i ~/.ssh/id_ed25519 -o IdentitiesOnly=yes'
查看 commit 树形列表
git log --oneline --decorate --graph --all
Github 配置
在 ~/.ssh/config 中配置:
- Windows
- macOS
Host github.com
HostName ssh.github.com
User git
Port 443
IdentityFile ~/.ssh/id_ed25519
ProxyCommand 'C:\Program Files\Git\mingw64\bin\connect.exe' -S 127.0.0.1:7890 %h %p
Host github.com
HostName ssh.github.com
User git
Port 443
IdentityFile ~/.ssh/id_ed25519
ProxyCommand nc -v -x 127.0.0.1:7890 %h %p