本文通过一个简单的 C++ "Hello World" 程序,演示 Git 的基本工作流程,包括初始化仓库、添加文件、提交更改、查看状态与差异等核心操作。
1. 创建项目目录并初始化 Git 仓库
bash
mkdir learngit
cd learngit
git init
Initialized empty Git repository in /home/cyk/Desktop/learngit/.git/
此时,learngit 目录已变为一个 Git 仓库(包含隐藏的 .git 子目录),当然你也可以选择有文件的目录,将其初始化为Git仓库。
2. 编写源代码文件 hello.cpp
创建文件并查看文件内容:
c
#include <iostream>
using namespace std;
int main(void) {
cout << "Hello World!" << endl;
return 0;
}
验证内容:
bash
cat hello.cpp
输出:
c#include <iostream> using namespace std; int main(void) { cout << "Hello World!" << endl; return 0; }
3. 编译并运行程序
bash
g++ hello.cpp -o hello
./hello
输出:
Hello World!
程序编译成功并正常运行。
4. 查看 Git 状态(首次)
git status 是 Git 中最常用的基础命令之一,用于查看当前工作区、暂存区(索引)与本地仓库的状态差异,核心作用是告诉你:当前仓库的文件有哪些修改、哪些已暂存、哪些未跟踪,以及分支状态等关键信息。
4.1、核心功能:它能告诉你什么?
执行 git status 后,输出会围绕 4 类文件状态展开,帮你理清代码变更的 "归属":
- 未跟踪文件(Untracked files) :工作区中新增的、Git 之前从未管理过的文件(比如新建的
hello.cpp)。Git 不会自动跟踪这些文件,需要通过git add 文件名手动加入暂存区。 - 已修改但未暂存(Modified but not staged) :之前已被 Git 跟踪的文件(比如已提交过的
hello.cpp)被修改了,但修改内容还没添加到暂存区。需要git add 文件名将修改同步到暂存区,才能后续提交。 - 已暂存但未提交(Changes to be committed) :已通过
git add将修改(或新增文件)加入暂存区,等待通过git commit提交到本地仓库。这部分内容是 "待提交" 的,提交后仓库会记录这次变更。 - 分支与提交状态 :最后会显示当前所在分支(比如
On branch main),以及当前分支与远程分支的同步状态(比如Your branch is up to date with 'origin/main',或Your branch is ahead by 1 commit)。
lua
git status
输出:
vbnetOn branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) hello hello.cpp nothing added to commit but untracked files present
说明:当前有两个未被追踪的文件 ------ 源码 hello.cpp 和可执行文件 hello。
注意:通常不将编译生成的二进制文件(如
hello)纳入版本控制。但为展示目的,此处暂时一并提交。
5. 添加文件到暂存区并提交
sql
git add .
git commit -m "first commit: hello world source file"
输出:
sql[master (root-commit) e2a5eac] first commit: hello world source file 2 files changed, 8 insertions(+) create mode 100755 hello create mode 100644 hello.cpp
再次检查状态:
lua
git status
输出:
vbnetOn branch master nothing to commit, working tree clean
工作区干净,所有更改已提交。
6. 修改源代码
编辑 hello.cpp,新增一行输出:
c
#include <iostream>
using namespace std;
int main(void) {
cout << "Hello World!" << endl;
cout << "Hello Cpp!" << endl; // ← 新增行
return 0;
}
7. 查看修改状态
lua
git status
输出:
vbnetOn branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: hello.cpp no changes added to commit
Git 检测到 hello.cpp 已被修改,但尚未暂存。
8. 查看具体修改内容(使用 git diff)
如果你忘记了自己修改了什么内容,此时你可以通过如下命令进行查看对比。
git diff
输出:
diffdiff --git a/hello.cpp b/hello.cpp index 338506b..5997ef5 100644 --- a/hello.cpp +++ b/hello.cpp @@ -4,5 +4,6 @@ using namespace std; int main(void) { cout << "Hello World!" << endl; + cout << "Hello Cpp!" << endl; return 0; }
清晰显示新增了一行代码,此时你可以放心的提交了。
9. 提交修改
-
将修改加入暂存区:
csharpgit add hello.cpp -
提交更改:
sqlgit commit -m "add 'Hello Cpp!' output"
输出:
csharp[master 0e38978] add 'Hello Cpp!' output 1 file changed, 1 insertion(+)
-
再次检查状态:
luagit status
输出:
vbnetOn branch master nothing to commit, working tree clean
所有更改已成功提交,工作区干净。
总结:Git 基本工作流
| 步骤 | 命令 | 作用 |
|---|---|---|
| 初始化仓库 | git init |
创建新的 Git 仓库 |
| 查看状态 | git status |
显示工作区与暂存区状态 |
| 查看差异 | git diff |
显示未暂存的修改内容 |
| 添加文件 | git add <file> 或 git add . |
将更改加入暂存区 |
| 提交更改 | git commit -m "message" |
将暂存区内容提交到本地仓库 |