# 再次检查当前文件状态,新加的文件处于 未跟踪 状态 $ git status On branch master Your branch is up-to-date with 'origin/master'. Untracked files: (use "git add <file>..." to include in what will be committed)
README
nothing added to commit but untracked files present (use "git add" to track)
跟踪新文件
把新添加的文件加到 Git 跟踪,使用 git add 命令:
1 2 3 4 5 6 7 8 9 10
git add README
# 这个时候文件会处于 被跟踪(Changes to be committed) 状态,并且处于暂存状态 $ git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage)
# 比如修改文件 README.md,这个文件之前处于已跟踪状态 # 修改后,这个文件处于 未暂存(Changes not staged for commit) 状态 $ git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage)
new file: README
Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory)
modified: README.md
暂存已经修改的被跟踪的文件,还是运行 git add 命令:
1 2 3 4 5 6 7 8 9 10 11
git add README.md
# 再次查看状态,此时文件 README.md 处于 已暂存(Changes to be commited) 状态 $ git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage)
new file: README modified: README.md
Git 状态预览
上面用的 git status 命令会显示比较详细的信息,加 -s 或 --short 选项可以显示简短的文件状态:
简短格式显示状态时,有6种情况
① ?? 开头:新添加的,但未跟踪的文件;或者是移出暂存区变成未跟踪状态的文件 ② A 开头:新添加的,且已加到暂存区中的文件,即新添加且已跟踪的文件 ③ M_:左侧的 M,已被跟踪的文件,修改过,且已加入到暂存区 ④ _M:右侧的 M,已被跟踪的文件,修改过,但还未加到暂存区 ⑤ MM:已被跟踪的文件,修改过并且已加到暂存区,但之后又修改了 ⑥ AM:新添加的文件,已加到暂存区,但之后又修改了
正常格式 vs 简短格式:
正常格式显示状态时,有 3 大类情况
① Changes to be committed:已暂存正等待提交的(即已跟踪,已暂存,等待提交的) ② Changes not staged for commit:已跟踪,但未暂存的(这种情况不会提交) ③ Untracked files:未跟踪的文件(可以是新建的文件,或把已跟踪的文件设为未跟踪,这种情况不会提交)
# 格式化输出 $ git log --pretty=format:"%h - %an, %ar : %s" ca82a6d - Scott Chacon, 11 years ago : changed the verison number 085bb3b - Scott Chacon, 11 years ago : removed unnecessary test code a11bef0 - Scott Chacon, 11 years ago : first commit
# 查看状态中已经有提示如何移除暂存状态了 $ git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage)
modified: README
# 移出暂存状态 $ git reset HEAD README Unstaged changes after reset: M README
# 再次查看状态 # 状态中给出了如何添加文件到暂存,以及如何撤销对文件的修改 $ git status On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory)
modified: README
no changes added to commit (use "git add" and/or "git commit -a")
# 文件状态为已暂存 $ git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage)
new file: NEWFILE
# 修改文件 $ echo'NEW LINE' >> NEWFILE
# 修改后的文件内容 $ cat NEWFILE New File NEW LINE
# 修改后的文件状态 $ git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage)
new file: NEWFILE
Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory)
modified: NEWFILE
# 撤销对已暂存文件的修改 $ git checkout -- NEWFILE
# 撤销后文件内容恢复原样 $ cat NEWFILE New File
# 撤销后文件状态恢复原样 $ git status On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage)
# 这次的标签详情只是显示了提交信息 $ git show v1.4-lw commit ca82a6dff817ec66f44342007202690a93763949 Author: Scott Chacon <schacon@gmail.com> Date: Mon Mar 17 21:52:11 2008 -0700
changed the verison number
diff --git a/Rakefile b/Rakefile index a874b73..8f94139 100644 --- a/Rakefile +++ b/Rakefile @@ -5,7 +5,7 @@ require 'rake/gempackagetask' spec = Gem::Specification.new do |s| s.platform = Gem::Platform::RUBY s.name = "simplegit" - s.version = "0.1.0" + s.version = "0.1.1" s.author = "Scott Chacon" s.email = "schacon@gmail.com" s.summary = "A simple gem for using Git in Ruby code."
后期打标签
运行命令 git tag -a <tag-name> <commit-hash>。其中,<commit-hash> 表示提交的校验和(或部分校验和)。
也就是给指定的提交打标签。比如,之前的一个提交忘记打标签了,就属于这种情况。
1 2 3 4 5 6 7 8
# 查看提交 $ git log --pretty=oneline ca82a6dff817ec66f44342007202690a93763949 changed the verison number 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 removed unnecessary test code a11bef06a3f659402fe7563abf99ad00de2209e6 first commit
# 给第二个提交打标签 $ git tag -a v1.2 085bb3b -m "my version 1.2"