2、记录文件更新
1)检查当前文件状态
要查看哪些文件处于什么状态,可以用 git status 命令。
[root@node1 git-test]# git status
On branch master
Initial commit
nothing to commit (create/copy files and use "git add" to track)
我们可以看到这是个刚初始化的目录,没有未提交的文件。后面还提示我们,可以创建或者复制文件,然后使用git add命令对文件进行跟踪。
2)跟踪新文件
我们首先在当前目录下创建一个新文件README,然后再查看当前文件的状态
[root@node1 git-test]# ll
total 4
-rw-r--r-- 1 root root 26 Nov 28 08:13 README
[root@node1 git-test]# git status
On branch master
Initial commit
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)
[root@node1 git-test]#
在状态报告中可以看到新建的 README 文件出现在 Untracked files 下面。 未跟踪的文件意味着 Git 在之前的快照(提交)中没有这些文件;Git 不会自动将之纳入跟踪范围,而且后面的提示我们可以使用git add命令去跟踪新文件
使用命令 git add 开始跟踪一个文件。
[root@node1 git-test]# git add README
再运行 git status 命令,会看到 README 文件已被跟踪,并处于暂存状态:
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage) git add 的逆操作,具体后面讲解
new file: README
只要在 Changes to be committed 这行下面的,就说明是已暂存状态。 如果此时commit,那么该文件此时此刻的版本将被留存在历史记录中。 git add 命令使用文件或目录的路径作为参数;如果参数是目录的路径,该命令将递归地跟踪该目录下的所有文件。我们也可以使用git add *提交当前目录下的所有文件
3)提交更新
现在我们已经把创建的文件提交到了暂存区,接下来我们就需要把暂存区的文件提交到本地版本库进行管理。注意:在每交提交前最好先使用git status查看一下,是不是所有更改的文件都已经暂存,然后再使用git commit命令提交。
[master (root-commit) 5e874cc] first submit
1 file changed, 1 insertion(+)
create mode 100644 README
[root@node1 git-test]# git status
On branch master
nothing to commit, working tree clean
请记住,提交时记录的是放在暂存区域的快照。 Git的每一次提交操作,都是对你项目作一次快照,以后可以回到这个状态,或者进行比较。
4)暂存己修改的文件
我们首先再新建一个Hello.txt的文件并提交:
[root@node1 git-test]# echo "Hello world">Hello.txt
[root@node1 git-test]# git status
On branch master
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)
[root@node1 git-test]# git add Hello.txt
[root@node1 git-test]# git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: Hello.txt
接着我们修改刚才已经提交过的README文件,然后查看状态:
[root@node1 git-test]# echo "first modify" >>README
[root@node1 git-test]# git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: Hello.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
文件 README 出现在 Changes not staged for commit 这行下面,说明已跟踪文件的内容发生了变化,但还没有放到暂存区。 要暂存这次更新,需要运行 git add 命令。 这是个多功能命令:可以用它开始跟踪新文件,或者把已跟踪的文件放到暂存区,还能用于合并时把有冲突的文件标记为已解决状态等。 将这个命令理解为“添加内容到下一次提交中”更加合适。 现在让我们运行 git add 将"README"放到暂存区,然后再看看 git status 的输出:
You have new mail in /var/spool/mail/root
[root@node1 git-test]# git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: Hello.txt
modified: README
现在两个文件都已暂存,下次提交时就会一并记录到仓库。 假设此时,你想要在 README 里再加一条内容, 重新编辑存盘后,再运行 git status 看看:
[root@node1 git-test]# echo "second modify" >>README
[root@node1 git-test]# git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: Hello.txt
modified: 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
你看到了什么? 现在 README 文件同时出现在暂存区和非暂存区。 这是为什么? 好吧,实际上 Git 只不过暂存了你运行 git add 命令时的版本, 如果你现在提交,README的版本是你最后一次运行 git add 命令时的那个版本,而不是你运行 git commit 时,在工作目录中的当前版本。 所以,运行了 git add 之后又作了修订的文件,需要重新运行 git add 把最新版本重新暂存起来:
[root@node1 git-test]# git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: Hello.txt
modified: README
[root@node1 git-test]# git commit -m "second submit"
[master a306d94] second submit
2 files changed, 3 insertions(+)
create mode 100644 Hello.txt
[root@node1 git-test]# git status
On branch master
nothing to commit, working tree clean
5)四个区域和文件的四种状态
四个区域:
image.png
文件的四种状态:
image.png
6)查看已暂存和未暂存的修改
我们再次修改 README 文件后暂存,然后编辑 Hello.txt文件后先不暂存, 运行 status 命令将会看到:
[root@node1 git-test]# echo "first modify" >>Hello.txt
[root@node1 git-test]# git add README
[root@node1 git-test]# git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: 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: Hello.txt
要查看尚未暂存的文件更新了哪些部分,不加参数直接输入 git diff:
[root@node1 git-test]# git diff
diff --git a/Hello.txt b/Hello.txt
index 802992c..6b599f0 100644
--- a/Hello.txt
+++ b/Hello.txt
@@ -1 +1,2 @@
Hello world
+first modify
此命令比较的是工作目录中当前文件和暂存区域快照之间的差异, 也就是修改之后还没有暂存起来的变化内容。
要查看已暂存的将要添加到下次提交里的内容,可以用 git diff --cached 命令或git diff --staged,效果是相同的。
[root@node1 git-test]# git diff --cached
diff --git a/README b/README
index 771c673..5f9ac91 100644
--- a/README
+++ b/README
@@ -1,3 +1,4 @@
My first git test project
first modify
second modify
+three modify
[root@node1 git-test]# git diff --staged
diff --git a/README b/README
index 771c673..5f9ac91 100644
--- a/README
+++ b/README
@@ -1,3 +1,4 @@
My first git test project
first modify
second modify
+three modify
现在我们将Hello.txt文件暂存,然后再进行编辑后,查看状态:
[root@node1 git-test]# git add Hello.txt
[root@node1 git-test]# git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: Hello.txt
modified: README
[root@node1 git-test]# git diff
[root@node1 git-test]# echo "second modify" >>Hello.txt
[root@node1 git-test]# git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: Hello.txt
modified: 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: Hello.txt
[root@node1 git-test]# git diff
diff --git a/Hello.txt b/Hello.txt
index 6b599f0..bfd0f21 100644
--- a/Hello.txt
+++ b/Hello.txt
@@ -1,2 +1,3 @@
Hello world
first modify
+second modify
[root@node1 git-test]# git diff --staged
diff --git a/Hello.txt b/Hello.txt
index 802992c..6b599f0 100644
--- a/Hello.txt
+++ b/Hello.txt
@@ -1 +1,2 @@
Hello world
+first modify
diff --git a/README b/README
index 771c673..5f9ac91 100644
--- a/README
+++ b/README
@@ -1,3 +1,4 @@
My first git test project
first modify
second modify
+three modify
请注意,git diff 本身只显示尚未暂存的改动,而不是自上次提交以来所做的所有改动。 所以有时候你一下子暂存了所有更新过的文件后,运行 git diff 后却什么也没有,就是这个原因
7)跳过使用暂存区
git commit 加上 -a 选项,Git 就会自动把所有已经跟踪过的文件暂存起来一并提交,从而跳过 git add 步骤:
[root@node1 git-test]# 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: Hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
[root@node1 git-test]# git commit -a -m "jump git add submit"
[master 4794019] jump git add submit
1 file changed, 2 insertions(+)
[root@node1 git-test]# git status
On branch master
nothing to commit, working tree clean
8、移除文件
我们先创建一个用于删除测试的文件a.txt,并提交
[root@node1 git-test]# git add a.txt
[root@node1 git-test]# git commit -m "rm test"
[master a80c6a9] rm test
1 file changed, 1 insertion(+)
create mode 100644 a.txt
现在我们在工作目录删除a.txt文件,然后查看状态,在 “Changes not staged for commit” 部分(也就是 未暂存清单)看到
[root@node1 git-test]# git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: a.txt
no changes added to commit (use "git add" and/or "git commit -a")
然后再运行 git rm 从暂存区删除文件:
rm 'a.txt'
[root@node1 git-test]# git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: a.txt
[root@node1 git-test]# git commit -m "rm test finish"
[master 3a2db18] rm test finish
1 file changed, 1 deletion(-)
delete mode 100644 a.txt
[root@node1 git-test]# git status
On branch master
nothing to commit, working tree clean
另外一种情况是,我们想把文件从 Git 仓库中删除(亦即从暂存区域移除),但仍然希望保留在当前工作目录中。 换句话说,你想让文件保留在磁盘,但是并不想让 Git 继续跟踪。使用 --cached 选项
[root@node1 git-test]# git commit -m "delete a.txt from response"
9、移动文件
使用git mv命令对文件进行改名操作:
使用mv命令改名:
[root@node1 git-test]# git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: Hello.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
Helloword.txt
no changes added to commit (use "git add" and/or "git commit -a")
[root@node1 git-test]# git add Helloword.txt
[root@node1 git-test]# git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: Helloword.txt
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: Hello.txt
[root@node1 git-test]# git commit -m "mv test"
[master 9ccca93] mv test
1 file changed, 4 insertions(+)
create mode 100644 Helloword.txt
[root@node1 git-test]# git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: Hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
[root@node1 git-test]# git rm Hello.txt
rm 'Hello.txt'
[root@node1 git-test]# git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: Hello.txt
[root@node1 git-test]# git commit -m "delete Hello.txt"
[master 2b1583e] delete Hello.txt
1 file changed, 4 deletions(-)
delete mode 100644 Hello.txt
You have new mail in /var/spool/mail/root
[root@node1 git-test]# git status
On branch master
nothing to commit, working tree clean
[root@node1 git-test]# ll
total 8
-rw-r--r-- 1 root root 52 Nov 28 11:17 Helloword.txt
-rw-r--r-- 1 root root 66 Nov 28 10:42 README
使用git mv 命令改名
[root@node1 git-test]# git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: Helloword.txt -> Hello.txt
[root@node1 git-test]# git commit -m "git mv test"
[master 6597abf] git mv test
1 file changed, 0 insertions(+), 0 deletions(-)
rename Helloword.txt => Hello.txt (100%)
[root@node1 git-test]# git status
On branch master
nothing to commit, working tree clean
不管何种方式结果都一样。 两者唯一的区别是,mv 是一条命令而另一种方式需要三条命令,直接用 git mv 轻便得多
10、忽略文件
一般我们总会有些文件无需纳入 Git 的管理,也不希望它们总出现在未跟踪文件列表。 通常都是些自动生成的文件,比如日志文件,或者编译过程中创建的临时文件等。 在这种情况下,我们可以创建一个名为 .gitignore 的文件,列出要忽略的文件模式。
我们再看一个 .gitignore 文件的例子:
*.a
# but do track lib.a, even though you're ignoring .a files above
!lib.a
# only ignore the TODO file in the current directory, not subdir/TODO
/TODO
# ignore all files in the build/ directory
build/
# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt
# ignore all .pdf files in the doc/ directory
doc/**/*.pdf
网友评论