Git 入门教程:初始化、修改与提交

本文通过一个简单的 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 类文件状态展开,帮你理清代码变更的 "归属":

  1. 未跟踪文件(Untracked files) :工作区中新增的、Git 之前从未管理过的文件(比如新建的 hello.cpp)。Git 不会自动跟踪这些文件,需要通过 git add 文件名 手动加入暂存区。
  2. 已修改但未暂存(Modified but not staged) :之前已被 Git 跟踪的文件(比如已提交过的 hello.cpp)被修改了,但修改内容还没添加到暂存区。需要 git add 文件名 将修改同步到暂存区,才能后续提交。
  3. 已暂存但未提交(Changes to be committed) :已通过 git add 将修改(或新增文件)加入暂存区,等待通过 git commit 提交到本地仓库。这部分内容是 "待提交" 的,提交后仓库会记录这次变更。
  4. 分支与提交状态 :最后会显示当前所在分支(比如 On branch main),以及当前分支与远程分支的同步状态(比如 Your branch is up to date with 'origin/main',或 Your branch is ahead by 1 commit)。
lua 复制代码
git status

输出:

vbnet 复制代码
On 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

输出:

vbnet 复制代码
On 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

输出:

vbnet 复制代码
On 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

输出:

diff 复制代码
diff --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. 提交修改

  1. 将修改加入暂存区:

    csharp 复制代码
    git add hello.cpp
  2. 提交更改:

    sql 复制代码
    git commit -m "add 'Hello Cpp!' output"

输出:

csharp 复制代码
[master 0e38978] add 'Hello Cpp!' output
 1 file changed, 1 insertion(+)
  1. 再次检查状态:

    lua 复制代码
    git status

输出:

vbnet 复制代码
On 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" 将暂存区内容提交到本地仓库
相关推荐
fengyehongWorld1 天前
Git 认证凭据管理
git
暮云星影1 天前
git版本发布
git·gitee·github·gitea
乌夷1 天前
Git Flow 分支模型
git
独隅1 天前
VS Code Git 工作树多分支并行开发实战评测
git
暮云星影2 天前
git仓库分支管理
git·gitee·github·gitea
互联网中的一颗神经元2 天前
ZZ — Git 速查表
大数据·git·elasticsearch
早安试言3 天前
git使用
git
燐妤3 天前
梦绘片场 ProjectDream故事总纲场景剧本篇:从项目到成片的 AI 漫剧工作台
人工智能·git·ai·项目开发
JackieDYH3 天前
Git 与 GitHub 新电脑配置指南(从零开始)
git·github·工具·使用教程
Jeanyia3 天前
GitLab 配套 Git 常用命令
git·gitlab