今天和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