跳到主要内容

检查状态和日志

通过 status 命令查看状态

当仓库中内容有增、删、改等变更操作时,可以通过 git status 命令查看相应的状态提示,并且 Git 提供了引导式命令说明,提示接下来可以做哪些事情。

创建文件

# 创建文件和编辑内容保存后,可以先查看一下当前仓库的状态
git status

# 提示

On branch main # 提示当前在哪个分支上,这里在 main 分支 (Git 中内容都在分支空间中,先不急于理解分支概念,在分支章节会说明)

No commits yet # 提示当前没有要提交的内容

Untracked files: # 提示有 Git 未跟踪的文件,并附带了相关的操作命令,这里提示将新的文件纳入 Git
(use "git add <file>..." to include in what will be committed) # (未跟踪区/工作区 → 暂存区)
README.txt

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

编辑文件内容

# 变更内容保存后,先查看一下当前仓库的状态
git status

# 提示:

On branch main # 当前分支
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: README.txt # modified → 已修改的文件

no changes added to commit (use "git add" and/or "git commit -a") # 提示将变更内容添加到‘暂存区’,或者直接提交到‘仓库区’

添加文件内容

# 添加文件内容
git add . # (未跟踪区/工作区 → 暂存区)

# 查看一下当前仓库的状态
git status

# 提示:

On branch main
# 已追踪的文件
Changes to be committed:
(use "git restore --staged <file>..." to unstage) # 附带的是用于“撤销”/“回滚”的操作命令 (本地仓库 → 暂存区)
modified: README.txt # modified → 已修改的文件

# 新的文件
Changes to be committed:
(use "git rm --cached <file>..." to unstage) # 附带的是用于“撤销”的操作命令 (暂存区 → 未跟踪区)
new file: README.txt # new file → 新的文件

提交文件内容

# 提交
git commit -m "一些提交信息" # (暂存区 → 本地仓库)

# 查看当前仓库的状态
git status

# 1、本地仓库与远程仓库无关联,提示:

On branch main # 当前分支
nothing to commit, working tree clean # 提示没有变更需要提交,工作树很干净

# 2、本地仓库与远程仓库有关联,提示:
On branch main # 当前分支
Your branch is ahead of 'origin/main' by 1 commit. # 当前分支比远程仓库 'origin/main' 分支领先 1 次提交
(use "git push" to publish your local commits) # 可以执行 git push 操作将本地内容提交到远程仓库

删除文件

# 不是通过 Git 途径删除文件 (操作系统命令删除,或手动删除)
rm README.txt

# 习惯性的查看当前仓库的状态
git status

# 提示

On branch main # 当前分支
Your branch is up to date with 'origin/main'. # 本地仓库内容和远程仓库一样

Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed) # 要么提交变更操作
(use "git restore <file>..." to discard changes in working directory) # 要么放弃变更操作 (暂存区 → 工作区)
deleted: README.txt # deleted → 已删除的文件

no changes added to commit (use "git add" and/or "git commit -a")
# 通过 Git 途径删除文件
git rm README.txt

git status

# 提示

On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed: # 变更内容已提交
(use "git restore --staged <file>..." to unstage) # 撤销变更操作 (本地仓库 → 暂存区)
deleted: README.txt

远程仓库有更新

# 将远程仓库的最新内容拉到本地
git fetch

# 查看状态
git status

# 提示

On branch main # 当前分支
# 当前分支比远程仓库 'origin/main' 分支落后 1 次提交
Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded.
# 可以执行 git pull 操作将远程仓库内容拉取到本地
(use "git pull" to update your local branch)

nothing to commit, working tree clean

远程仓库无更新

git status

On branch main
Your branch is up to date with 'origin/main'. # 和远程仓库内容一致

nothing to commit, working tree clean # 没有要提交的内容,工作树很干净

通过 log 命令查看历史日志

可以通过 git log 查看提交历史,历史记录较多时,按空格键翻页,按 q 退出 (Quit)。

可指定最近次数查看 git log -3

git log -3
commit 4654f4ff1c275815fa7fc32c62022553f3181920 (HEAD -> main)
Author: xianghongai <[email protected]>
Date: Wed Dec 06 00:35:12 2018 +0800

一些提交信息

commit 6971fd3212a241a815aaf0b5afe2222de03730d5
Author: xianghongai <[email protected]>
Date: Wed Dec 06 00:32:42 2018 +0800

第二次进行 Git 提交

commit f6d87bfea86b111c0d9380e0fe4f3c7cdff4a4a1
Author: xianghongai <[email protected]>
Date: Wed Dec 06 00:31:33 2018 +0800

第一次进行 Git 提交

可以开启 -p/--patch 选项,显示每次提交所引入的差异;

可以通过 --pretty=format 选项,定制记录的显示格式,如查看本周的提交:

git log --color --since=1.weeks --date=format:'%Y-%m-%d %H:%M:%S' --pretty=format:'%Cred%h%Creset %Cgreen%cd %C(yellow)%d%Creset %C(bold blue)%an%Creset %s'

这类太长的命令,可以配置别名来简化指令。

Resources