Git 初学者指南:基础与必备命令
I’m happy to help translate the article, but I’ll need the actual text you’d like translated. Could you please paste the content (or the portion you want translated) here? I’ll keep the source link at the top and preserve all formatting, markdown, and code blocks as you requested.
什么是 Git?
Git 是一个开源的分布式版本控制系统(DVCS)。它于 2005 年由 Linus Torvalds(Linux 的创建者)创建,旨在以高速和高效处理大型项目。
通俗来说,Git 是你项目的“时间机器”。开发者使用它来:
- 跟踪更改 – 精确查看哪些内容被更改、何时更改、由谁更改。
- 协作 – 与多个人同时在同一代码库上工作。
- 管理历史 – 如果出现问题,可以回退到之前的版本。
无论是单独工作还是在大团队中,Git 都能确保你的项目历史安全有序。
核心概念
- 工作目录 – 你本地编辑文件的文件夹。
- 暂存区(Index) – “候诊室”。文件准备好后会放到这里等待保存。
- 本地仓库 – 机器上隐藏的
.git文件夹,存放项目的历史记录。 - 远程仓库 – 托管在网上的项目版本(例如 GitHub),用于共享和备份。
- 提交(Commit) – 项目的“一次快照”。每个提交都有唯一的 ID 和说明更改的消息。
- 分支(Branch) – 并行工作区。它让你在不破坏主代码的情况下开发新功能。
- 合并(Merge) – 将一个分支的更改合并到另一个分支(例如将“feature”分支合并到“main”)。
- HEAD – 指向项目历史中当前所在位置的指针。可以把它想象成“你在这里”的标识。
Source: …
基本 Git 命令
配置与仓库初始化
# Set your name for all your commits
git config --global user.name "Your Name"
# Set your email address
git config --global user.email "yourname@example.com"
# Turn a local folder into a Git repository
git init
# Download an existing project from a URL (like GitHub)
git clone [url]
处理更改
# Show modified files in your working directory and staged files for your next commit
git status
# Add a file to the staging area for the next commit
git add [file]
# Add all untracked files to the staging area
git add .
# Remove a file from the staging area while retaining changes in the working area
git reset [file]
# Show the difference of what is changed but not staged
git diff
# Show the difference of what is staged but not yet committed
git diff --staged
# Commit your staged content as a new snapshot
git commit -m "[descripted message]"
# Shortcut: stage and commit tracked files
git commit -am "[descripted message]"
分支管理
# List all local branches
git branch
# Create a new branch
git branch [branch-name]
# Switch to a specific branch
git checkout [branch-name]
# Combine another branch's history into your current one
git merge [branch-name]
查看历史
# Show the commit history of the current branch
git log
# Show the commits on branchA that are not on branchB
git log branchB..branchA
# Show the commits that change a file, even across renaming
git log --follow [file]
# Show the difference of what is in branchA that is not in branchB
git diff branchB...branchA
# Show any object in Git in human‑readable format
git show [SHA]
远程操作
# Add a remote repository (alias)
git remote add [alias] [url]
# Fetch all branches from the remote
git fetch [alias]
# Merge the remote branch into your current branch
git merge [alias]/[branch]
# Push local commits to the remote repository
git push [alias] [branch]
# Fetch and merge any commit from the tracking remote branch
git pull
实践示例
-
创建一个新项目文件夹(工作目录)
mkdir my-first-git-project cd my-first-git-project git init # Initialize Git (run once per project) -
创建一个文件
echo "Hello Git!" > hello.txt -
检查状态
git status你会看到
hello.txt被列为“未跟踪”。 -
暂存并提交
git add hello.txt git commit -m "Initial commit: Create hello.txt" -
进行进一步修改
echo "Learning Git is fun!" >> hello.txt git diff # See exactly what changed before saving -
暂存并提交更新
git add . git commit -m "Update hello.txt with a new sentence" -
查看项目历史
git log --oneline
日志中的每一行都代表一个时间点,如果出现问题,你可以回到该点。你已经成功地跟踪了你的第一个项目!
版本控制不仅仅是保存代码;它还意味着可以自由实验,而不必担心会破坏项目。