今天和git搏斗了一下午,发现了修改的文件一直commit不了。网上查了一下才发现原来git的模型里还有工做区和暂存区的说法。git
三者的转换关系以下图:svn
须要注意的是:提交一个文件须要先git add <file>
把它放到暂存区,而后才能用git commit
真正提交。
这是一个和svn在使用上一个很大的区别。一直commit发现提交不上去,找了很久才发现,原来是没有提交到暂存区。code
下面来演示一下:blog
首先新建一个叫作learn_git
的目录,并初始化:ci
phantom01@phantom01-VirtualBox:~/work$ mkdir learn_git phantom01@phantom01-VirtualBox:~/work$ cd learn_git/ phantom01@phantom01-VirtualBox:~/work/learn_git$ git init # Initialized empty Git repository in /home/phantom01/work/learn_git/.git/
而后git status
查看如今的状态:get
phantom01@phantom01-VirtualBox:~/work/learn_git$ git status # On branch master # # Initial commit # # nothing to commit (create/copy files and use "git add" to track)
会发现如今什么都没有。毕竟这个目录里面咱们尚未放东西嘛。it
接下来,新建一个叫作readme.md
的文件,并在里面写点内容。io
而后git status
来查看下状态:ast
phantom01@phantom01-VirtualBox:~/work/learn_git$ git status # On branch master # # Initial commit # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # readme.md # # nothing added to commit but untracked files present (use "git add" to track)
发现如今readme.md
是"Untracked files",说明git如今尚未开始追踪这个文件,这时须要咱们用git add
来把这个文件添加进git 的管理。而后git status
来查看当前状态。class
phantom01@phantom01-VirtualBox:~/work/learn_git$ git add readme.md phantom01@phantom01-VirtualBox:~/work/learn_git$ 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"中的"new file"。
此时,咱们刚才修改的部分已经被提交至暂存区。
咱们修改一下文件中的内容,而后在查看一下状态:
phantom01@phantom01-VirtualBox:~/work/learn_git$ git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: 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: 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: readme.md
其中"Changes not staged for commit"是说没有被提交到暂存区。
接下来咱们用git commit
提交一下:
phantom01@phantom01-VirtualBox:~/work/learn_git$ git commit -m "ci1" # [master (root-commit) 41adae7] ci1 # 1 file changed, 1 insertion(+) # create mode 100644 readme.md phantom01@phantom01-VirtualBox:~/work/learn_git$ 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") #
会发现"be committed"那一段不见了,而"not staged"还在。这说明一段内容被提交了,然后一段内容没有被提交。
接下来,咱们只须要在git add
和commit一次就行了。
http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013745374151782eb658c5a5ca454eaa451661275886c6000
http://selfcontroller.iteye.com/blog/1786644
知乎上关于这个的讨论:https://www.zhihu.com/question/19946553