GIT基本指令與觀念
GIT為一套版本管理套件, 本篇會先介紹GIT特色, 會在介紹一些基本指令,
GIT以下有幾個特色
- 大部分操作在本地完成不需要連網(有一些版本管理系統是統一由遠端管理)
- 完整性的保證(分散式)
- 盡可能添加數據而非刪除或修改數據
- 分支操作非常快速
- 與Linux命令兼容
指令集
本地庫初始化
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 在想要建立git的資料夾下, 會建立一個隱藏的.git目錄, 存放本地端的相關文件 | |
git init | |
# 設置簽名, 區分不同提交的身份, global當前操作系統範圍, 沒有的話就是by項目級別, cat .gitconfig | |
git config (--global) user.name charles | |
git config (--global) user.email charles@abc.com |
基本指令
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 確認是否有檔案變更與commit add各種狀態 | |
git status | |
# 將目標資料夾文件交給git暫存區管理 | |
git add (相反功能 git rm --cache [file]) | |
# 上傳到本地git庫 | |
git commit -A(全部) | |
git commit -m "modify xxx function" | |
# 每次變動紀錄 | |
git log (--pretty=oneline) (reflog 顯示head@{移動到當前版本需要的步數}) | |
# 回到特定版本 | |
--hard本地庫暫存區工作區移動指針 --mixed本地庫與暫存區移動指針 --soft僅僅在本地庫移動指針 | |
git reset --hard 9e3r342(先用log查, 移動HEAD到特定版本, 可以前進也可以後退, ~3往前3步只能往後) | |
# 比較文件差異 | |
git diff 比較多個文件 | |
git diff aaa.txt(工作區與暫存區進行比較) | |
git diff HEAD aaa.txt(工作區與本地庫某個版本進行比較) |
分支
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# branch操作 | |
git branch -v | |
git branch hot_fix | |
# branch 合併分支到master | |
git checkout hot_fix (切換) | |
vim aaa.txt (修改分支上的資料) | |
git checkout master | |
git merge hot_fix | |
# branch衝突 | |
git merge hot_fix (發生衝突後, 衝突會直接顯示在文件上) | |
vim aaa.txt (修改衝突部分) | |
git add aaa.txt (add表示修改衝突) | |
git commit -m "fix conflicts" (不用帶文件名) |
遠程交互
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 上傳到遠程庫(如果使用github, 也可使用SSH, 不用每次都登入github) | |
git remote add remote_name http......(git remote server) | |
git remote -v (看有幾個建立的網址) | |
git push origin[別名] master[分支名] | |
git fetch origin master(遠端庫下載到本地庫) | |
git merge (本地庫合併到工作區) | |
git pull (fetch + merge, 不太會產生衝突就直接pull) | |
# 取得遠端庫某專案到本地庫 | |
git clone 遠程地址 (做的事情 1.將遠程庫資料下載下來 2.git remote add origin 3.git init ) | |
# 上傳遠程庫必須為合作成員 管理者發出邀請連結給合作者 | |
github >> setting >> collaborators >> 輸入github帳號 >> 產生連結 >> 成員者點擊連結接受 | |
# 跨團隊管理 遠程庫複製 | |
git fork | |
將特定遠程庫複製一份出來, | |
B團隊對A團隊的專案fork, B會出現專案於自己的遠程庫, | |
fork專案修改後, 發出pull request(github尚有按鈕)給A, | |
A可於github上進行對話, 審核代碼, 同意合併Merge pull request | |
A打上commit訊息 |
簡單的流程如下:
A建立了一個featureA 分支, 改完後push到remote的featureA , 由資深的B進行pull, 確認程式沒有問題, 在本地端merge, 當沒有問題後在push上 remote的master
Ref:
- 尚硅谷IT培训学校
- https://medium.com/@totoroLiu/git-command-660f4c1e7a86
沒有留言:
張貼留言