为了更好理解本文内容,请先阅读:Git文件版本控制
获取帮助
git 中有很多的命令,我们可以通过如下方式来获取帮助:
$ git [command] -h
这会显示一个简单的帮助提示,我们还能获取Git 命令的使用手册:
$ git help [command]
$ git [command] --help
状态显示
我们可以通过下面命令来查看文件处于什么状态:
$ git status
git status 命令的输出十分详细。 如果你使用 git status -s 命令或 git status –short 命令,你将得到一种更为紧凑的格式输出。 运行 git status -s ,状态报告输出如下:
$ git status -s
M README
MM Rakefile
A lib/git.rb
M lib/simplegit.rb
?? LICENSE.txt
- 新添加的未跟踪文件前面有 ?? 标记
- 新添加到暂存区中的文件前面有 A 标记
- 修改过的文件前面有 M标记,M 有两个可以出现的位置:
- 出现在右边的 M (红色)表示该文件被修改了但是还没放入暂存区
- 出现在靠左边的 M (绿色)表示该文件被修改了并放入了暂存区
- D表示删除的
- R表示重命名的
跟踪文件
结合 Git文件版本控制 跟踪状态这段的图片。
我们可以通过如下命令来将未跟踪文件转变为暂存状态:
$ git add readme.md
此时再运行 git status 命令,会看到文件已被跟踪,并处于暂存状态:
$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: readme.md
在 Changes to be committed 这行下面的是已暂存状态,但是未提交的文件。 如果此时提交,那么该文件此时此刻的版本将被留存在历史记录中。如果参数是目录的路径,该命令将递归地跟踪该目录下的所有文件。
我们也可以通过如下命令来将工作目录下所有文件添加到暂存区:
$ git add -A
通过命令来获取更多帮助
$ git add -h
提交文件
我们可以通过下面命令来提交暂存的文件:
$ git commit
执行这个命令,会跳出文本编辑器,我们需要在里面输入这此提交的描述(通SVN一样),我这边是 vim 也可以通过 git config –global core.editor [编辑器] 来设定。这个命令,会将所有添加到暂存区中的新增文件/文件修改内容提交,但是不会提交不在暂存区中的修改/文件,提交的是放在暂存区域的快照,任何还未暂存的仍然保持已修改状态。如果不想通过文本编辑器,我们也可以通过下面命令:
$ git commit -m 'readme'
如果,我们指定了文件,那么将会将所有修改提交(包括未暂存的修改),先看看知己 commit 的情况:
$ echo >readme.md 'test'
$ git commit
On branch master
Changes not staged for commit:
modified: readme.md
no changes added to commit
$ git status
On branch 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.md
no changes added to commit (use "git add" and/or "git commit -a")
可以看到,未进入暂存区的文件修改并没有被提交。
暂存已修改文件
通过上面 git add 命令,我们将一个新建的未跟踪文件加入暂存区并提交,这个时候我们再来看看状态:
$ git status
On branch master
nothing to commit, working directory clean
这个状态表示暂存区中没有任何文件需要提交,并且没有文件被修改过。现在,我们新建一个文件 1.txt 并将其添加到暂存区,同时修改 readme.md 中的内容,再通过状态命令来查看:
$ touch 1.txt
$ echo > readme.md '修改内容'
$ git add 1.txt
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: 1.txt
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
出现在“Changes not staged for commit”这行下面的文件,说明已其内容发生了变化,但还没有放到暂存区。 要暂存这次更新,需要运行 git add 命令,它有很多用途:
- 跟踪新文件
- 将已修改的跟踪文件放到暂存区
- 合并时把有冲突的文件标记为已解决状态
- 等等……
执行命令将 readme.md 添加到暂存区,并查看状态
$ git add readme.md
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: 1.txt
modified: readme.md
这样,在下次执行 commit 命令的时候,这些修改就会一并提交到仓库中保存起来。接下来,我们不要提交,再来看看:
$ echo >1.txt '111'
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: 1.txt
modified: readme.md
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: 1.txt
可以看到文件 1.txt 同时出现在暂存区和非暂存区。这个时候我们提交修改并查看状态:
$ git commit -m 'fix'
[master 4d0f0a2] fix
2 files changed, 3 insertions(+), 3 deletions(-)
$ git status
On branch 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: 1.txt
no changes added to commit (use "git add" and/or "git commit -a")
可以看到,未添加到暂存区的修改并没有进入到此次提交中。
跳过暂存区
有时候,我们可能只是需要将所有内容都提交,那么暂存区就显得累赘了。但是,我们也可以使用下面方式来跳过暂存区。在 commit 的时候增加文件名:
$ git commit readme.md -m 'is a test'
[master a7ff297] is a test
1 file changed, 1 insertion(+), 1 deletion(-)
如果是对所有文件,那么将文件名替换为 -a ,Git 就会自动把所有已经跟踪过的文件暂存起来一并提交,从而跳过 git add 步骤:
$ git commit -am 'describe'
查看修改内容
如果想知道具体修改的内容,可以通过 git diff 命令。
$ git diff
diff --git a/1.txt b/1.txt
index 87d8d80..9659f9a 100644
--- a/1.txt
+++ b/1.txt
@@ -2,7 +2,7 @@
sfjksdla
sdfsdkl
dskk
-///
+insert
sdgfjklsfsdfsdf
-号表示删除的内容,+表示增加的内容。如果我们这个时候通过 git add 将文件暂存,再运行此命令可以发现输出的内容是空的。此命令比较的是工作目录中当前文件和暂存区域快照之间的差异, 也就是修改之后还没有暂存起来的变化内容。
如果要查看已暂存的将要添加到下次提交里的内容,可以用 git diff –cached 命令或者 git diff –staged 。
$ git diff --cached
diff --git a/1.txt b/1.txt
index 87d8d80..9659f9a 100644
--- a/1.txt
+++ b/1.txt
@@ -2,7 +2,7 @@
sfjksdla
sdfsdkl
dskk
-///
+insert
sdgfjklsfsdfsdf
sddsds
kjslksjfk
我们也可以通过下面命令来用 Araxis ,emerge 或 vimdiff 等软件输出 diff 分析结果:
$ git difftool
$ $ git difftool --tool-help
(输出系统支持的 Git Diff 插件,等结果出来需要一点时间)
移除文件
如果我们希望某个文件不再继续进行版本控制,我们可以通过下面命令:
$ git rm --cached 1.txt
rm '1.txt'
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: 1.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
1.txt
可以看到,这个时候 1.txt 是未跟踪的状态了,如果直接运行 git rm 1.txt
,会将文件也删除,和下面命令效果相同:
$ rm 1.txt
$ git add 1.txt
提交遗漏信息
有的时候,我们可能会想对进行过的操作进行撤销,比如:提交完了才发现漏掉了几个文件没有添加,或者提交信息写错了等,但是我们希望在这次提交的时候进行修改,应该怎么做?我们可以通过下面步骤达到我们需要的效果:
# 首次提交(有遗漏信息)
$ git commit -m 'before amend'
# 添加遗漏到暂存区
$ git add 1.txt
# 提交遗漏信息
$ git commit --amend
文本编辑器启动后,可以看到之前的提交信息。 编辑后保存会覆盖原来的提交信息。而且最终只会有一个提交 - 第二次提交将代替第一次提交的结果。
撤消操作
1. 撤销暂存
有的时候,我们会希望将暂存区中的某个文件取消暂存,git status
已经告诉了我们怎么做:
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: 1.txt
modified: b.txt
这里指出了命令git reset HEAD <file>
,我们尝试一下:
$ git reset HEAD 1.txt
Unstaged changes after reset:
M 1.txt
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: b.txt
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: 1.txt
可以看到 1.txt 已经为未暂存的状态。
2. 撤销修改
像上面,我们将 1.txt 从暂存状态改为了未暂存的状态。但是,我们又需要将文件的所有修改都恢复为修改前的,通过上面的git status
命令结果,我们可以看到git已经给了我们操作的方法git checkout -- <file>
,再来尝试一下:
$ git checkout -- 1.txt
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: b.txt
可以看到, 1.txt 已经修改已经被还原。经过测试,这个命令需要文件不在暂存状态才有效。