Git 与 GitHub 入门指南
发布: (2026年1月18日 GMT+8 18:10)
3 min read
原文: Dev.to
Source: Dev.to
介绍
在软件开发的世界里,版本控制至关重要。Git 是一种版本控制工具,帮助你跟踪代码的更改,而 GitHub 则提供协作平台并展示你的项目。本指南将演示如何设置 Git Bash、将其连接到你的 GitHub 账户,以及推送和拉取代码的基础操作。
什么是 Git Bash?
Git Bash 在 Windows 上提供类 Unix 的终端,让你可以使用 Git 命令和常用的 Bash 实用工具。
什么是 GitHub?
GitHub 是一个基于云的 Git 仓库托管平台,支持协作、问题追踪以及项目展示。
为什么使用 Git 和 GitHub?
- 版本控制 – 随时间跟踪代码的更改。
- 协作 – 与他人无缝合作。
- 作品集 – 向潜在雇主展示你的项目。
安装 Git Bash
- 在官方 Git 网站上选择你的操作系统。
- 下载并运行安装程序。
验证安装
git --version
配置姓名和电子邮件
# Set your name
git config --global user.name "YourName"
# Set your email
git config --global user.email "YourEmail"
# Verify the configuration
git config --list
使用 SSH 密钥将 GitHub 连接到 Git Bash
生成 SSH 密钥
# List existing keys (optional)
ls ~/.ssh
# Generate a new Ed25519 key
ssh-keygen -t ed25519 -C "YourEmail"
# Start the ssh-agent
eval "$(ssh-agent -s)"
# Add the private key to the agent
ssh-add ~/.ssh/id_ed25519
将 SSH 密钥添加到 GitHub
# Copy the public key to the clipboard (Windows)
clip < ~/.ssh/id_ed25519.pub
- 前往 GitHub → Settings → SSH and GPG keys → New SSH key。
- 粘贴复制的公钥,填写标题(例如 “GitHub Key”),然后点击 Add SSH key。
测试连接
ssh -T git@github.com
你应该会看到一条成功信息,确认身份验证已通过。
基本 Git 命令
# Create a new directory and initialize a repository
mkdir kenya
cd kenya
git init
# Create files
touch nairobi
touch student.py
touch README.md
# Stage all changes
git add .
# (Optional) Return to home directory
cd ~
结论
掌握 Git 和 Git Bash 对于在软件开发中实现高效的版本控制和协作至关重要。通过了解这些基本命令并配置 SSH 认证,你可以更从容地管理自己的项目,并在 GitHub 等平台上为他人的工作做出贡献。