Git基礎介紹
- 2. Outline
❖ What’s Git?
❖ Why Git?
❖ Basic Operation
➢ Setup
➢ Status Transformation
➢ GitLab & Cooperation
❖ Branch
❖ Branch Management
2
- 10. Setup Git Download
設定識別資料
檢視所有Config設定
檢視/修改 Config的某個參數
$ git config --global user.name “aaabbb”
$ git config --global user.email aaabbb@gmail.com
$ git config --list
$ git config user.name # 檢視
$ git config user.name “xxxxx” # 修改
10
- 11. Git Initial
在現有無版控的專案
$ cd <專案的資料夾路徑>
$ git init
$ git status #可以先看個檔案狀態
$ git add .
$ git status #add後也可以看個檔案狀態
$ git commit -m “intitial commit”
在要clone下來的某個路徑
#下載一整個remote專案包括所有歷史紀錄
$ git clone <Repository Directory>
11
- 15. Status Transformation - Track Files
檢視當前git所管理的資料狀態
$ git status
新增要追蹤的檔案
$ git add index.html 一次把目錄下的所有檔案加入:
$ git add .
15
- 16. Status Transformation - Modify Tracked Files
已修改 已暫存
工作目錄 暫存目錄
add
modify
修改一個先前已經追蹤過的檔案
修改一個先前已經追蹤過的檔案
$ git status
$ git add index.html
$ git status
16
- 18. Status Transformation - Commit Files
提交站存區裡的內容到git資料庫
$ git commit -m “commit message”
#或是
$ git commit
跳過提交暫存區的動作 直接commit
$ git commit -a -m “commit message”
已修改 已暫存 已提交
工作目錄 暫存目錄 git目錄
git commit -m
git commit -a -m
18
- 19. GitLab & Cooperation - SSH Key
Generate SSH Key & Add it to GitLab:
https://gitlab.com/help/ssh/README
19
- 20. GitLab & Cooperation - Add Remote Repository
新增remote repository URL
# git remote add <name> <url>
$ git remote add origin https://gitlab.softbi.com/OCB_WMS/OCB_WMS_AP.git
$ git remote add origin ssh://git@gitlab.softbi.com:3078/OCB_WMS/OCB_WMS_AP.git
檢視remote git 清單
$ git remote -v
http(s): 可讀寫,方便,較慢
ssh: 適合讀寫,一定是加密,較快
git: 唯讀
20
- 21. GitLab & Cooperation - Push & Pull
push to remote
# 將master branch 上傳到remote origin倉庫
$ git push origin master
# 上傳到remote origin倉庫並且追蹤該branch
$ git push -u origin master
pull from remote
# 將remote的master branch
$ git pull origin master
其他pull方式
# 避免無謂的merge可下
$ git pull --rebase origin master
# 先將remote所有branch/tag拉下來,再作rebase
$ git fetch
$ git rebase origin/master 21
- 24. Branch - Create Branch
新增branch
$ git branch testing
HEAD所指的地方就是目前commit
預設branch為master
新增一個branch -> testing
24
- 27. Branch - Branch Split
$ git checkout master
$ git commit -a -m “modify something”
27
- 28. Branch - Merge
$ git checkout -b iss53
$ git commit -a -m “modify something”
28
今天有個issue53要修正
- 29. Branch - Merge
$ git checkout master
$ git checkout -b hotfix
$ git commit -a -m “fixed the bug”
29
發現有個bug要緊急修正
- 31. $ git branch -d hotfix
$ git checkout iss53
$ git commit -a -m “modify something”
31
- 34. Branch - Conflict Recovery
<<<<<<< HEAD
<div id="footer">contact : email.support@github.com</div>
=======
<div id="footer">
please contact us at support@github.com
</div>
>>>>>>> iss53
<div id="footer">
please contact us at support@github.com
</div>
手動決定要保留的部分
34
- 35. Branch - Conflict Recovery
# 確認檔案狀態
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: index.html
# 確認解決衝突後就可以commit出去
# 直接下git commit即可,git會自動加Merge branch ‘iss53’ into master的message
$ git commit
35
- 36. Branch - Conflict Recovery
發生confict 時的處理步驟:
1. 抉擇conflict files要留下哪個branch內容(或是兩邊都有一些code要保留)
2. 使用git add 將處理好的檔案加入stage
3. 反覆步驟 1~2 直到所有 confict 處理完畢
4. git commit 提交合併訊息
36
- 37. Branch - Stash
37
$ git status
$ git stash
$ git stash list
$ git stash pop
$ git stash pop stash@{n}
- 39. Branch - Rebase
39
$ git checkout experiment
$ git rebase master
$ git checkout master
$ git merge experiment
- 40. Branch - Reset
$ git reset HEAD^
$ git reset HEAD <file>
$ git reset HEAD~3
$ git reset <commit>
40
- 45. Git Flow - Master & Develop branch
45https://github.com/nvie/gitflow/wiki/Installation
- 46. Git Flow - Develop & Feature branch
$ git flow feature start myfeature
# 等於以下的事情
$ git checkout -b feature/myfeature develop
$ git flow feature finish myfeature
# 等於以下的事情
$ git checkout develop
$ git merge --no-ff myfeature
$ git branch -d feature/myfeature
46
- 51. SVN to Git
# git svn clone <svn repository> --stdlayout
$ git svn clone
svn://bisvn.softbi.com:80/taiwan/OCB/WMS/SRC/AP --stdlayout
$ git svn clone <svn repository> --trunk=<主幹路徑> --
branches=<分支路徑> --tags=<標籤路徑>
$ git svn show-ignore >> .gitignore
$ git add .gitignore
$ git commit -m “convert svn:ignore properties to .gitignore”
51
Editor's Notes
- 資料來源
https://git-scm.com/book/zh-tw/v1
http://zoomq.qiniudn.com/ZQScrapBook/ZqFLOSS/data/20081210180347/