Git中的工作區(Working Directory)、暫存區(stage)和歷史記錄區(history)


今天和git搏斗了一下午,發現了修改的文件一直commit不了。網上查了一下才發現原來git的模型里還有工作區和暫存區的說法。

  • 工作區:在git管理下的正常目錄都算是工作區。我們平時的編輯工作都是在工作區完成。
  • 暫存區:可以理解為一個臨時區域。里面存放將要提交文件的快照。
  • 歷史區:commit后,記錄的歸檔。

三者的轉換關系如下圖:

需要注意的是:提交一個文件需要先git add <file>把它放到暫存區,然后才能用git commit真正提交。
這是一個和svn在使用上一個很大的區別。一直commit發現提交不上去,找了好久才發現,原來是沒有提交到暫存區。

實驗

下面來演示一下:

首先新建一個叫做learn_git的目錄,並初始化:

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查看現在的狀態:

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)

會發現現在什么都沒有。畢竟這個目錄里面我們還沒有放東西嘛。

接下來,新建一個叫做readme.md的文件,並在里面寫點內容。

然后git status來查看下狀態:

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來查看當前狀態。

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM