第三課 Git 工作流程 與遠程倉庫同步,修改,載入(stage)和提交項目 現在你已經有一個git本地倉庫,一切都配置完畢。然後該怎麼辦?
一般來說,跟其他的源碼控制系統的工作流程沒什麼兩樣,唯一一個區別就是載入(stage)的過程。整個工作流程大致是這樣(流程1):
與遠程倉庫同步
修改檔案
查看變更
載入變更
提交載入的變更
重複
上傳這是最複雜的情況,如果你不與別人合作開發的話,就不需要上傳到倉庫中去(流程2):* 修改檔案
* 提交變更
* 重複簡單吧。要記得,git是分布式的,所以如果不是合作項目的話,實際上不需要提交到一個公用的共用伺服器上--你可以像使用RCS一樣,只用來追蹤本地檔案變更。下面,讓我們先來看個簡單的樣本,緊接著再來看用git協作開發的執行個體。簡單樣本如果你想跟著做這個例子,請複製這個項目:$ git clone git://github.com/schacon/simplegit 例子開始,按照流程2,我們首先要修改README檔案,將自己添加到項目作者中去。所以我們修改這個檔案。然後我們希望提交這個變更,所以我們運行'git commit -a' 命令。 -a 的意思是告訴git先將變更了的檔案先載入(stage),然後提交-我們後面會通過'staging area'命令實現,但是現在運行 'git commit -a' 命令,效果跟在SVN中使用'commit'命令一樣。$ git commit -a執行完之後,一個提交資訊的提示會出現在編輯器中(這裡$EDITOR環境變數或'core.editor'這兩個git組態變數的預設值都是vim)類似下面這樣的內容:_
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch main
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: README
#
~
~
".git/COMMIT_EDITMSG" 9L, 253C輸入一些提交的資訊,譬如"added myself to the README as an author"然後退出。vim操作提示:
按下I,o,a進入編輯模式,編輯完畢按Esc,輸入:wq儲存退出。然後會看到這樣的提示:
[master]: created 5896d4d: "added myself to the README as an author"
1 files changed, 2 insertions(+), 1 deletions(-) 顯示我們剛剛輸入的提交資訊,並且有一組關於這次提交項目中檔案變更的統計數字。同時還給我們一個提交的校正和,'5896d4d',這個校正和可以用來日後確切的查看這次提交的細節。這就是簡單用例。修改檔案,'git commit -a',重複協作開發樣本現在,我們來介紹一個複雜點的執行個體,這次我們使用遠程倉庫,將項目上傳上去,從而跟其他的開發人員一起協同工作。同時,我們會介紹staging area。如果你會從遠程倉庫中複製項目,那麼與遠程倉庫項目同步也是相當簡單的--只需要執行'git pull'.如果是遠程倉庫項目沒有變化,也就是說其他的開發人員沒有對項目進行變更,會顯示這樣的資訊:
Already up-to-date.相反,執行這條命令之後會將你上次同步之後遠程倉庫中的變更同步到本地項目中,並且git會合并這些新的變更:$ git pull
Updating c264051..b04dc3d
Fast forward
lib/simplegit.rb | 4 ++++
1 files changed, 4 insertions(+), 0 deletions(-)例子開始,重新複製遠程倉庫中的項目,修改README檔案和lib/simplegit.rb檔案(不要執行git commit -a)。現在你可以使用'git status'命令來查看工作目錄發生了什麼變更:$ git status
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: README
# modified: lib/simplegit.rb
#
no changes added to commit (use "git add" and/or "git commit -a")我們看到,有兩個檔案是在"changed but not updated"段落中出現 ,這意味著這兩個檔案還沒有載入(unstaged). 如果現在我們提交,什麼也不會發生。也就是說檔案必須先載入(stage),然後才能提交。所以,我們先來載入(stage) 檔案,git中使用'git add'命令不僅可以開始追逐檔案而且可以對他們載入stage變更。所以讓我們載入(stage) README檔案的變更,然後再來查看一下狀態。$ git add README
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: README
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: lib/simplegit.rb現在'lib/simplegit.rb'檔案還是未載入(unstaged), 但是README檔案現在已經到了'changes to be committed'段落中-它幾經載入(stage)了。現在如果我們運行提交命令(不要-a,這個會自動stage所有的東西),只有這個檔案會被提交-而simplegit.rb依然是unstaged。這時,我們使用-m選項來執行'git commit',這樣後面跟上字串表示這次提交的資訊。$ git commit -m 'updated the README'
[master]: created 14bb3c6: "updated the README"
1 files changed, 1 insertions(+), 2 deletions(-)如果現在再執行'git status',我們會看到stage之後的檔案現在已經提交了,只剩下了unstaged的'simplegit.rb'檔案。$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: lib/simplegit.rb
#現在我們可以stage並且提交這個檔案:$ git commit -a -m 'added a staging command to the library'
[master]: created bbaee85: "added a staging command to the library"
1 files changed, 4 insertions(+), 0 deletions(-)
$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
#
nothing to commit (working directory clean)現在我們已經將兩次提交的變更都搞定了,而且加了提交資訊,可以讓我們的夥伴很容易理解我們做的變更。經過最後一次提交,我們看到'git status'執行之後顯示我們的工作目錄clean了(同時提示我們現在的分枝上有兩個提交還沒有上傳)所以,現在我們將這些變更上傳到伺服器端與我們的夥伴分享,前提是我們有上傳的許可權,(如果沒有上傳的許可權,我們可以在網路上建立一個自己的git倉庫,將其上傳),然後讓朋友下載。運行'git push'會將我們的變更上傳到伺服器。$ git push
Counting objects: 11, done.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (7/7), 744 bytes, done.
Total 7 (delta 3), reused 0 (delta 0)
To git@github.com:schacon/simplegit.git
b04dc3d..bbaee85 master -> master
到目前為止,我們看到了新資料都已經上傳完畢,伺服器上的主分支也已經更新了。現在我們可以複習一下這整個的過程,讓我們可以更加熟練的將git應用到我們的項目中去。
轉自http://fsjoy.blog.51cto.com/318484/244826