Git管理項目執行個體說明-記錄和跟蹤項目

來源:互聯網
上載者:User

標籤:dex   條目   world   bio   modified   分類樹   initial   rect   director   

 

假設一個HTML項目,使用Git來記錄和跟蹤這個項目,包括以下內容:
1)建立版本庫。
2)添加與修改檔案。
3)建立新分支。
4)打標籤並整理版本庫。
5)複製版本庫。

1.建立版本庫 Creating a Repository
在Git中,版本庫(.git目錄)是與工作目錄樹並排放在同一個目錄中的。
本例中,要建立一個HTML頁面,給這個項目取名為mysite。
首先建立一個同名目錄“mysite”,並進入到這個目錄,然後輸入命令git init。
[[email protected] ~]# mkdir mysite
[[email protected] ~]# cd mysite
[[email protected] mysite]# git init
Initialized empty Git repository in /root/mysite/.git/
建立完成。

2.代碼修改
往空版本庫裡面添加檔案:
建立一個名為index.html的檔案,並添加文本:
[[email protected] mysite]# vim index.html

<html><body><h1>Hello World</h1><p>My first paragraph.</p></body></html>

建立了一個簡單的HTML檔案後(把它放在mysite路徑下),就可以開始跟蹤版本了。

要想讓Git跟蹤這個檔案,須先讓它知道這個檔案,要分兩步走:
1)首先使用git add命令把該檔案添加到版本庫的索引(index);
2)然後使用git commit命令提交。

檔案或檔案清單可以作為git add命令的參數。
[[email protected] mysite]# git add index.html

git commit命令建立一個提交記錄。
提交記錄是儲存在版本中的記錄,每提交一次建立一個記錄,並標記出代碼的演化。
Git把提交者的姓名和郵件地址,以及提交留言,都添加到提交記錄中。
參數-m,告訴Git本次提交的留言為"add in hello world HTML"。
commit提交前要進行global全域設定,設定郵箱和使用者名稱地址,不然提交會失敗
[[email protected] mysite]# git config --global user.email "[email protected]"
[[email protected] mysite]# git config --global user.name "wangshibo"
[[email protected] mysite]# git commit -m "add in hello world HTML"

運行命令git log可以看到這個提交相關的資訊:
輸出的第一行顯示提交名稱,是Git自動產生的SHA-1碼。Git通過它來跟蹤提交,使用該雜湊碼可以保證每個提交的名稱都是獨一無二的。
[[email protected] mysite]# git log
commit 6881e1772267debce7bc29ee90cc5acf448ab24a
Author: wangshibo <[email protected]>
Date: Tue Dec 20 17:16:20 2016 +0800

add in hello world HTML

3.項目後續開發中,修改HTML檔案如下:
[[email protected] mysite]# vim index.html

<html><head><title>Hello World in Git</title></head><body><h1>Hello World</h1><p>My first paragraph.</p></body></html>

修改完畢,Git可以檢測到檔案被修改。
命令git status會顯示工作目錄樹的狀態,即當前的檢視狀態。
[[email protected] mysite]# 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: index.html
#
no changes added to commit (use "git add" and/or "git commit -a")

上面的結果表明Git監測到了修改,但還不知道如何處理它們。
如果要提交,需要暫存(stage)修改,以準備把修改提交到版本庫。

Git有三個地方可以存放代碼:
1)第一個地方是工作目錄樹,編輯檔案時可以直接在這裡操作;
2)第二個是索引(index),也就是暫存區(staging area)。暫存區是工作目錄樹和版本庫之間的緩衝區。
3)第三個,也就是最終的一個,是版本庫。

命令git add,可以暫存對檔案剛做的修改。它跟前面添加一個新檔案時使用的是同一個命令,只不過,這次它告訴Git要跟蹤的是一個新的修改而非新的檔案。
[[email protected] mysite]# git add index.html
[[email protected] mysite]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: index.html
#

暫存修改過的index.html之後,執行命令git status可以看到,資訊變為了Changes to be commited,index.html這行由紅色變為了綠色。
使用命令git commit時,不要忘記使用帶-m的參數,並在參數後面加上提交留言,以解釋修改的原因,如下:
git log可以快速探索提交留言:
[[email protected] mysite]# git log
commit 6881e1772267debce7bc29ee90cc5acf448ab24a
Author: wangshibo <[email protected]>
Date: Tue Dec 20 17:16:20 2016 +0800

add in hello world HTML
[[email protected] mysite]# git log -1
commit 6881e1772267debce7bc29ee90cc5acf448ab24a
Author: wangshibo <[email protected]>
Date: Tue Dec 20 17:16:20 2016 +0800

add in hello world HTML
命令中加入參數:-1可以限制命令輸出的提交條目的個數。

4.分支理解和使用
比如mysite項目的代碼現在幾乎發行就緒了,但是還需要進行測試等工作,直到確認它達到了預期的功能和品質,而與此同時,藉助分支,可以開始下一個版本的新功能的開發了。
建立分支的命令是git branch,該命令需要兩個參數:新分支名稱和父分支名稱。
新建立的分支基於已經存在的父分支。
[[email protected] mysite]# git branch RB_1.0 master
該命令從主分支(master branch)上建立一個叫RB_1.0的分支。
主分支master是Git的預設分支。分支名稱中的RB代表發布分支(release branch)。該首碼可以讓人快速分辨出哪些分支是發布分支。

現在來做一些新的改動。這些改動不影響準備發布的代碼。
在</body>之前增加如下代碼:
[[email protected] mysite]# vim index.html

<html><head><title>Hello World in Git</title></head><body><h1>Hello World</h1><p>My first paragraph.</p><ul><li><a href="bio.html">Biography</a></li></ul></body></html>

用如下命令提交這些修改:
[[email protected] mysite]# git commit -a
Aborting commit due to empty commit message.
參數-a告訴Git提交全部修改過的檔案。
上面提交後,會屆時彈出了一個文字檔,輸入的資訊是提交留言。將彈出的文字檔正常退出即可。

現在主分支上有最新的修改,而發布分支上還是原來的代碼。
請切換到發布分支,做發布前的最後修改。切換分支的命令是git checkout。
[[email protected] mysite]# git checkout RB_1.0
M index.html
Switched to branch ‘RB_1.0‘

轉換分支後,所使用的開啟檔案的編輯器會提醒檔案已經被修改,重新載入檔案,會發現剛才在主分支上做過的修改消失了。
可以用git status命令來查看自己在哪一個分支上:
[[email protected] mysite]# git status
# On branch RB_1.0
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: index.html
#
# 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: index.html
#

做發布前的最後修改:在<head>標記塊中添加一些描述性的元標籤:
[[email protected] mysite]# vim index.html

<html><head><title>Hello World in Git</title><meta name="description" content="hello world in Git"/></head><body><h1>Hello World</h1><p>My first paragraph.</p><ul><li><a href="bio.html">Biography</a></li></ul></body></html>

儲存並修改該提交:
[[email protected] mysite]# git commit -a
Aborting commit due to empty commit message.

 

Git管理項目執行個體說明-記錄和跟蹤項目

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.