星期三, 4月 09, 2014

[GIT] 輕鬆了解Git 分支的概念與基本操作手記




這張圖讓人可以很快速了解整個Git Branch的精要(分支的種類與用途)

主要分支

 * master 主程式(除非重大 bug,則會分出 hotfix 分支)
 * develop 開發分支(用來在另外分支出 Release, feature)

次要分支

 * Hotfixes(由 master 直接分支,馬上修正 bug)
 * Feature(由 develop 直接分支,開發新功能)
 * Release(由 develop 直接分支,開發下一版 Release)

小惡魔神人在2011年的文章有中文版的操作步驟:
 Git 版本控制 branch model 分支模組基本介紹
建議可以好好閱讀與演練:D

以下是自已操作的指令,不定時更新

建立本地端Git repository並上傳到Github

#建立project
$mkdir git_practice

#切換到專案目錄
$cd git_practice

#建立repo
$git init


Initialized empty Git repository in /Library/WebServer/Documents/git_practice/.git/

#建立一個測試檔案
$vi index.php

#新增檔案
$git add *

#查看目前版本進行(列出你已變動的)
$git status

ibigdsMacAir:git_practice ibigd$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached ..." to unstage)
#
# new file:   index.php

#

#儲存當前的修改變動
$git commit -m "First Commit"

[master (root-commit) 0bd3330] First commit
 1 file changed, 2 insertions(+)

 create mode 100644 index.php


#在github上建立一個專案
 #複製你的repo的連結:

(1)看你要建立新的本地端的repo,再把遠端的更新回來
(2)或直接在遠端repo,複製(Clone)一份回本地端的repo


#照上面圖的教學,切換到remote
$git remote add origin https://github.com/iambigd/git_practice.git

#將本端的修正push到Git
$git push -u origin master

Counting objects: 3, done.
Writing objects: 100% (3/3), 221 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/iambigd/git_practice.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

檔案更新與上傳


#從遠端的Server repo更新資料 (預設的行為是將遠端的 repo => 即origin  與本地端合併)
$git pull origin master



$git pull

#資料上傳
$git push origin <目的分支或主線>



$git push

分支指令集 

#開新的分支
$git checkout  <分支名稱> <來源分支>

#開新的分支 (-b代表會直接切換到分支)
$git checkout -b <分支名稱> <來源分支>

#切換分支
$git checkout <目的分支>

#合併分支,需配合切換分支指令:D  (一定要利用-no-ff來合併才能保留git歷程圖形)
$git merge --no-ff <要合併的分支名稱>

#列出目前所有的branch
$git branch -r

#列出分支 * 代表目前所在的分支
$git branch
  develope
* master

#取出遠端的branch
$git checkout -t origin

#列出已經合併的
git branch -r --merged


#刪除分支 (會檢查目前的分支是否有合併過,若無則刪除無效。 -D 則會強制刪除)
$git branch -d <欲Delete的分支名稱>

#分支commit上去遠端的repo
$ git checkout -b feature_branch_name #前面有提供的建立分支並切換
$ git push -u origin feature_branch_name


TAG版本指令集


#tag 一個要發佈的版本, 如下一個版號
$git tag -a v2.0  -m "Release的版本訊息"

#本地端刪除
$git  tag -d TAG_NAME

#顯示tag
$git tag

$git tag -l

#顯示特地的tag: 搜尋字串,可以加*字元搜尋
$git tag -l QUERY_NAME

#發佈tag到repo
$git push origin TAG_NAME

#如果本地有很多tags可以透過--tags全部上傳
$git push origin --tags

#遠端刪除
$git push origin :refs/tags/

#使用此tag
$ git clone will give you the whole repository.
After the clone, you can list the tags with $ git tag -l and then checkout a specific tag:
$ git checkout tags/
Even better, checkout and create a branch (other you will be on a branch named after the revision number of tag):

$ git checkout tags/ -b


檔案復原

其他常用指令集

#刪除檔案
$git rm

#忽略不想加入Git的檔案
$vim .gitignore


#像Mac會產生不必要的暫存檔我們可以讓他忽略掉
.DS_Store                                                         
                                                                         
#VIM的swp暫存檔與log                                      
*.swp                                                               
log/*.log                                                           

#重新設定repo
$rm -rf .git 
$git init

遠端Repo操作

#列出遠端的repo
$git remote 

#列出遠端的repo詳細資訊(完整的path)
$git remote -v

#砍掉遠端的repo
$git remote remove REMOTE_NAME

#新增一個遠端的repo (常用在初始化一個遠端的repo)
$git remote add REMOTE_NAME GIT_PATH

$git remote add origin YOUR_GIT_PATH

#更名(相要切換別的遠端的repo使用,更名完需再搭配remote add origin)
git remote rename TARGET_NAME NEW_NAME






沒有留言:

張貼留言

留個話吧:)

其他你感興趣的文章

Related Posts with Thumbnails