跳到主要内容

Git

信息

Git 是一个免费的开源分布式版本控制系统,旨在以快速和高效的方式处理从小型到超大型项目的所有内容。

Git 易于学习,占用空间很小,性能快如闪电。 它超越了像 Subversion、CVS、Perforce 和 ClearCase 这样的配置管理工具,具有廉价的本地分支、方便的暂存区和多个工作流等特性。

官方网站 | 《Pro Git 中文版》 | 菜鸟教程 | 在 BiliBili 上搜索视频教程

安装

Windows

点击下载 Windows 最新版的 Git 安装包 | 也可以使用 Chocolatey 安装 | 也可以使用 winget 安装

TortoiseGit

TortoiseGit 提供了显示文件状态的叠加图标、强大的 Git 上下文菜单以及更多! 官方网站

配置

初次使用配置

配置用户名、邮箱

桌面右键点击 [Git Bash Here],输入以下指令:

# "username" 为 GitHub 账号,用户名
git config --global user.name "username"

# xxxxxxxxxx@mail.com 为 GitHub 账号,注册时的用的邮箱
git config --global user.email xxxxxxxxxx@mail.com

生成自己的 SSH Key

Git Bash 终端中,输入 ssh-keygen

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Yue_p/.ssh/id_rsa): # 直接回车
Enter passphrase (empty for no passphrase): # 直接回车
Enter same passphrase again: # 直接回车
Your identification has been saved in yes
Your public key has been saved in yes.pub
The key fingerprint is:
SHA256:xuvPaGeJuufX6k4oiufALDHD7PCfZxkfXQxjCBElkCA Yue_p@DESKTOP-SQLFD1T
The key's randomart image is:
+---[RSA 3072]----+
|E ...o==.. |
| . . .. + |
| . + |
|o . o |
|=o S. . |
|oB ...o. |
|..= .+oo.o |
| . +.o+o=+= . |
| .o=oo*+B*. |
+----[SHA256]-----+

现在,~/.ssh/ 中应该有 id_rsa(私钥)与 id_rsa.pub(公钥)两个文件。

GitBash 配置代理

## 注意端口号---------------------------------------
# http ↓↓↓↓
git config --global http.proxy http://127.0.0.1:1080
git config --global https.proxy https://127.0.0.1:1080

# socks5
git config --global http.proxy 'socks5://127.0.0.1:1080'
git config --global https.proxy 'socks5://127.0.0.1:1080'

# 重置代理
git config --global --unset http.proxy
git config --global --unset https.proxy

配置 SSH 访问 GitHub 走 443 端口

~/.ssh/config
Host github.com
Hostname ssh.github.com
Port 443
User git

基本操作

参考:Git 基本操作 - 菜鸟教程

# 初始化仓库
git init

# 克隆项目
git clone

# 添加至暂存区
git add

# 查看仓库状态
git status

# 比较暂存区和工作区
git diff

# 提交暂存区到本地仓库
git commit

# 回退版本
git reset

# 删除文件
git rm

# 移动或重命名文件
git mv

# 分支切换
git checkout

# 更清晰地切换分支 git 2.23后
git switch

# 恢复或撤销文件 git 2.23后
git restore

# 查看历史提交记录
git log

# 列表形式查看指定文件修改记录
git blame <文件路径>

# 远程仓库操作
git remote

# 从远程获取代码库
git fetch

# 下载远程代码并合并
git pull

# 上传远程代码并合并
git push