首页  /  云原生  /  正文

Git 提交与版本回退

云原生 2026-10-02📖 13 分钟👁 —
🌿 云原生 · Git第 2 / 5 页12345📖 完整导航 →

一、先看状态 git status

git status 用来看工作区、暂存区的文件变更情况,是敲得最多的命令。刚 init 完的空仓库是这样:

圣果@shengguo MINGW64 /demo (master)
$ git status
On branch master # 当前本地库所在分支

No commits yet # 没有提交过东西

nothing to commit (create/copy files and use "git add" to track)
# 没有东西需要提交

往里放一个 hello.txt,再看一次:

# 查看本地库状态
圣果@shengguo MINGW64 /demo (master)
$ git status
On branch master

No commits yet

# 文件在工作区,git未追踪文件(git add)
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        hello.txt

nothing added to commit but untracked files present (use "git add" to track)
hello.txt 是红色的,被列在 Untracked files 里,说明 git 还没追踪它

红色就代表没被追踪、没进暂存区,看状态的时候先看颜色。

二、加到暂存区 git add

圣果@shengguo MINGW64 /demo (master)
$ git add hello.txt
# 警告:git默认转化了行末换行符(安装时配置的)
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it

圣果@shengguo MINGW64 /demo (master)
$ git status
On branch master

No commits yet

Changes to be committed:
# 删除暂存区的文件所用命令
  (use "git rm --cached <file>..." to unstage)
        new file:   hello.txt

红色变绿了,说明 git 追踪到了文件(已经在暂存区):

add 之后 hello.txt 变成绿色,跑到 Changes to be committed 下面

add 错了想撤回,用 git rm --cached,工作区的文件还在:

圣果@shengguo MINGW64 /demo (master)
$ git rm --cached hello.txt
rm 'hello.txt'

圣果@shengguo MINGW64 /demo (master)
$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        hello.txt

nothing added to commit but untracked files present (use "git add" to track)

三、提交到本地库 git commit

圣果@shengguo MINGW64 /demo (master)
$ git commit -m "first commit" hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
[master (root-commit) 4e1a936] first commit
# 一个文件被改变,17行内容插入
 1 file changed, 17 insertions(+)
 create mode 100644 hello.txt

圣果@shengguo MINGW64 /demo (master)
$ git status
On branch master
# 提交后没有新增和修改,工作树干净(没有东西需要再提交)
nothing to commit, working tree clean

-m 是 message 的缩写,后面跟日志信息。命令里写了文件名就只提交这一个文件,不写就是提交暂存区里的全部改动。

前面那条 warning 说的是换行符:

缩写全称通俗理解哪个系统用
LFLine Feed“换行”(光标往下移一行)Linux / Mac(新标准)
CRLFCarriage Return + Line Feed“回车 + 换行”(光标回到行首 + 往下移一行)Windows

在 Windows 上 git 会按安装时选的配置自动做这个转换,所以看到这条 warning 不用管。

四、看历史 git reflog 和 git log

# Reference Log 引用日志
圣果@shengguo MINGW64 /demo (master)
$ git reflog
# 4e1a936 是版本号 指针指向 master 分支 第一次提交的版本名(-m指定的)
4e1a936 (HEAD -> master) HEAD@{0}: commit (initial): first commit

# 查看详细日志
圣果@shengguo MINGW64 /demo (master)
$ git log
commit 4e1a936fb66a8825c53e8e7459b4d232f6f36272 (HEAD -> master)
# 提交者 用户名 <邮箱>
Author: shengguo <shengguo>
Date:   Tue Aug 11 19:17:03 2026 +0800

    first commit

git log 看的是“历史快照”,git reflog 看的是“操作日记”。 这个区别到第六节就特别管用。

五、改了文件再提交

改了文件不 add,git status 会告诉你是 modified,而且是红色:

$ git status
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.txt

no changes added to commit (use "git add" and/or "git commit -a")
改完没有 add,文件又变回红色,状态是 modified

add 完再 commit:

$ git commit -m "second commit" hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
[master 5972f9e] second commit
# 一行修改,一行删去 insertion 是新增
 1 file changed, 1 deletion(-)

$ git status
On branch master
nothing to commit, working tree clean

$ git reflog
# 指针指向第二个版本
5972f9e (HEAD -> master) HEAD@{0}: commit: second commit
4e1a936 HEAD@{1}: commit (initial): first commit

再改第三次:

$ git commit -m "third commit" hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
[master ed2db57] third commit
# 一个修改 一个新增 一个删除
 1 file changed, 1 insertion(+), 1 deletion(-)


$ git reflog
ed2db57 (HEAD -> master) HEAD@{0}: commit: third commit
5972f9e HEAD@{1}: commit: second commit
4e1a936 HEAD@{2}: commit (initial): first commit


$ git log
commit ed2db576665c771c22c2c6d68118370a5a6378ee (HEAD -> master)
Author: shengguo <shengguo>
Date:   Tue Aug 11 21:11:49 2026 +0800

    third commit

commit 5972f9e1e8db8ad9d9ddfcac902c05df18d5f855
Author: shengguo <shengguo>
Date:   Tue Aug 11 21:08:47 2026 +0800

    second commit

commit 4e1a936fb66a8825c53e8e7459b4d232f6f36272
Author: shengguo <shengguo>
Date:   Tue Aug 11 19:17:03 2026 +0800

    first commit

六、版本回退 git reset --hard

回退或者前进到指定版本,配合 git reflog 用:

# 版本穿梭(回退或前进到指定版本,配合 git reflog 使用)
git reset --hard 版本号

比如从第三个版本回到第二个:

$ git reflog
ed2db57 (HEAD -> master) HEAD@{0}: commit: third commit
5972f9e HEAD@{1}: commit: second commit
4e1a936 HEAD@{2}: commit (initial): first commit

$ git reset --hard 5972f9e
HEAD is now at 5972f9e second commit

# 查看历史版本,发现指针在第二个版本,本地库内容也变
$ git reflog
版本穿越日志
5972f9e (HEAD -> master) HEAD@{0}: reset: moving to 5972f9e
ed2db57 HEAD@{1}: commit: third commit
5972f9e (HEAD -> master) HEAD@{2}: commit: second commit
4e1a936 HEAD@{3}: commit (initial): first commit

回退之后 .git 目录里的 HEAD 文件会变成:

ref: refs/heads/master

指针指向了 master,说明当前在 master 分支。分支具体指向哪个版本号,看 .git\refs\heads\master 文件的内容:

5972f9e1e8db8ad9d9ddfcac902c05df18d5f855

每次提交,这个文件里的版本号都会自动变。两个指针记住就行:

git reflog 能看到“被删掉的版本”,git log 看不到 —— 因为 git reset --hard 回退之后,git log 里就没有后面的版本了,但 git reflog 还留着记录,可以照着版本号找回来。

git reset --hard 会丢弃工作区和暂存区的所有未提交修改。执行之前确认没有没保存的代码,不然就真丢了。

七、删文件和 .gitignore

在工作区删掉文件,git status 会显示 deleted,提交之后这次删除就记进历史了。只想把文件从暂存区拿掉、工作区保留,还是用前面说过的 git rm --cached 文件名。

不想让某些文件进 git 的视野(编译产物、日志、IDE 配置这些),在项目根目录放一个 .gitignore:

# 忽略所有 .log 文件
*.log

# 忽略 target 目录下的全部内容
target/

# 忽略 IDE 的配置目录
.idea/
.vscode/

八、看改了什么 git diff

add 之前想知道具体改了哪几行,用 git diff;已经 add 到暂存区的,用 git diff --cached:

# 工作区和暂存区的差异
git diff

# 暂存区和最后一次提交的差异
git diff --cached 文件名

改坏了想丢掉工作区的修改,老写法是 git checkout -- 文件名,新版 git 推荐 git restore 文件名,两个都能用。