$ git status On branch master Changes not staged for commit: (use"git add ..."toupdate what will be committed) (use"git checkout -- ..."to discard changes in working directory) modified: readme.txt no changes added tocommit (use"git add"and/or"git commit -a")
git status 命令可以让我们时刻掌握仓库当前的状态, 上面命令的输出信息告诉我们,readme.txt被修改了,但是还没有提交修改
git diff 查看修改的内容
1 2 3 4 5 6 7 8 9
$ git diff readme.txt diff --git a/readme.txt b/readme.txt index b0354bd..5367626100644 --- a/readme.txt +++ b/readme.txt @@ -1,2 +1,2 @@ -Git is a version control system. +Git is a distributed version control system. Git is a free software.
修改完成之后,再次进行提交
第一步:
1
git add readme.txt
第二步:
在执行git commit 之前 我们可以执行git status查看当前仓库的状态
1 2 3 4 5
$ git status On branch master Changes to be committed: (use"git reset HEAD ..."to unstage) modified: readme.txt
`$ git status On branch master Changes not staged for commit: (use"git add/rm ..."toupdate what will be committed) (use"git checkout -- ..."to discard changes in working directory) deleted: LICENSE.txt modified: readme.txt Untracked files: (use"git add ..."toincludein what will be committed) LICENSE no changes added tocommit (use"git add"and/or"git commit -a") `
$ git status On branch master Changes to be committed: (use"git reset HEAD <file>..."to unstage)
newfile: LICENSE modified: readme.txt
两个文件都被添加到了暂存区
执行 git commit 就可以一次性把暂存区的所有修改提交到分支
1 2 3 4
`$ git commit -m 'git new ' [master0694ef3] git new 3 files changed, 3 insertions(+), 1 deletion(-) createmode100644 LICENSE
提交之后,不对工作区做任何修改, 工作区就是干净的
1 2 3
$ git status On branch master nothingtocommit, working tree clean
版本库再次发生变化
管理修改
git跟踪并管理的是修改,而非文件
实践出真知:
首先,对readme.txt做一个修改 加一行内容
1 2 3 4 5 6
`$ cat readme.txt Git isa distributed version control system. Git isa free software distributed under the GPL. Git hasa mutable index called stage. Git hasa nbnb nbn nb. Git new connect. //新加的内容
git add 添加 `
1 2 3 4 5 6
$ git add readme.txt $ git status On branch master Changes to be committed: (use "git reset HEAD ..."to unstage) modified: readme.txt
再次修改readme.txt
1 2 3 4 5 6 7
$ cat readme.txt Git isa distributed version control system. Git isa free software distributed under the GPL. Git hasa mutable index called stage. Git hasa nbnb nbn nb. Git new connect. Git newnew connect //再次添加
直接提交 git commit
1 2 3
`$ git commit -m "git new new connect" [master cf3406c] git newnewconnect 1filechanged, 2 insertions(+), 1 deletion(-)
查看状态 git status `
1 2 3 4 5 6 7 8
$ git status On branch master Changes not staged for commit: (use"git add/rm ..."toupdate what will be committed) (use"git checkout -- ..."to discard changes in working directory) deleted: LICENSE modified: readme.txt no changes added tocommit (use"git add"and/or"git commit -a") `
可以看到 第二次修改没有被提交
因为:第一次修改->git add ->第二次修改 -> git commit
第一次的修改放入到了暂存区,第二次的修改没有放入到暂存区,所以,使用
git commit 只提交了暂存区的修改,因此,第一次修改被提交,第二次的修改不会被提交
使用 git diff HEAD -- readme.txt 查看工作区和版本库里面的区别
1 2 3 4 5 6 7 8 9 10
`$ git diff HEAD -- readme.txt diff --git a/readme.txt b/readme.txt index e1b5b50..6558bb3 100644 --- a/readme.txt +++ b/readme.txt @@ -3,3 +3,4 @@ Git is a free software distributed under the GPL. Git has a mutable indexcalled stage. Git has a nbnb nbn nb. Git newconnect. +Git newnewconnect
$ git status On branch master Changes not staged for commit: (use"git add/rm ..."to update what will be committed) (use"git checkout -- ..."to discard changes in working directory) deleted: LICENSE no changes added to commit (use"git add"and/or"git commit -a")
$ cat readme.txt Git is a distributed version control system. Git is a free software distributed under the GPL. Git has a mutable indexcalled stage. Git has a nbnb nbn nb. Git newconnect. Git newnewconnect. Git nen newnewconnect. Git newnewnewnewconnect. Git nbn nbn nbn //添加数据
不执行git add 的情况:
1 2 3 4 5 6 7 8 9 10
$ git checkout readme.txt $ cat readme.txt Git is a distributed version control system. Git is a free software distributed under the GPL. Git has a mutable indexcalled stage. Git has a nbnb nbn nb. Git newconnect. Git newnewconnect. Git nen newnewconnect. Git newnewnewnewconnect. //新数据已被撤回
$ cat readme.txt Git is a distributed version control system. Git is a free software distributed under the GPL. Git has a mutable indexcalled stage. Git has a nbnb nbn nb. Git newconnect. Git newnewconnect. Git nen newnewconnect. Git newnewnewnewconnect. Git nbn. //新增数据
撤回
1 2 3 4 5 6 7 8 9 10 11
$ git checkout -- readme.txt $ cat readme.txt Git is a distributed version control system. Git is a free software distributed under the GPL. Git has a mutable indexcalled stage. Git has a nbnb nbn nb. Git newconnect. Git newnewconnect. Git nen newnewconnect. Git newnewnewnewconnect.
`$ cat readme.txt Git is a distributed version control system. Git is a free software distributed under the GPL. Git has a mutable indexcalled stage. Git has a nbnb nbn nb. Git newconnect. Git newnewconnect. Git nen newnewconnect. Git newnewnewnewconnect. git my nbme //新增数据
git add readme.txt //添加到暂存区
1 2 3 4
`$ git resetHEAD readme.txt Unstaged changes afterreset: D LICENSE M readme.txt ` `
git reset 命令既可以回退版本,也可以把暂存区的修改回退到工作区。当我们使用 HEAD 时,表示最新的版本
git status 查看一下
1
$ git status On branch master Changes not staged for commit: (use"git add ..."toupdate what will be committed) (use"git checkout -- ..."to discard changes in working directory) modified: readme.txt no changes added tocommit (use"git add"and/or"git commit -a") `
现在暂存区干净了,工作区有修改
丢弃工作区的修改
1 2 3 4 5
$ git checkout -- readme.txt
$ git status On branch master nothingtocommit, working tree clean
λ git status On branch master Changes not staged for commit: (use"git add/rm <file>..."toupdate what will be committed) (use"git checkout -- <file>..."to discard changes in working directory) deleted: test.txt
no changes added tocommit (use"git add"and/or"git commit -a")