Git 指令
Posted
筆記一下 Git 指令。
Config
config
設定/取得 設定檔值
三個層級,設定優先權: local > global > system
- 系統
--system
- 使用者
--global
儲存庫
--local
(default)# git config [file-opstion] [name] [value] # # --system/--global/--local: 層級 # -l: 列出設定檔值 # --unset: 刪除設定 # 設定 user.name $ git config --global user.name clouding # 別名 tree $ git config --global alias.tree "log --graph --decorate --pretty=oneline --abbrev-commit" # 刪除設定 $ git config --global --unset alias.tree
更多設定 git-config
Getting and Creating Projects
init
建立/初始化 儲存庫
# git init [directory]
#
# --bare: 建立原型儲存庫
# 建立 repository
$ git init NewRepo
# 建立原型儲存庫
$ git init --bare NewRepo.git
clone
複製儲存庫
# git clone [directory]
#
# --bare: 建立原型儲存庫
# 複製 repository
$ git clone [email protected]:cloudingcity/git-test.git
# 將一般儲存庫轉成原型儲存庫
$ git clone --bare git-test git-test.git
Basic Snapshotting
add
新增內容到索引
# git add
#
# -f: 強制新增,忽略 .gitignore
# -A: 新增全部
# 新增全部
$ git add .
status
顯示工作狀態
diff
顯示 提交/工作狀態/分支差異
# git diff
# 比較 master new 兩分支差異
$ git diff master..new
commit
提交改變到儲存庫
# git commit
#
# -m: 提交的訊息,設定多個為段落
# --amend -m: 修該最近的提交訊息,會產生新的hash id,如果已經push出去了就不要用它
#
# 沒設定則用 vi 開啟編輯,或 core.editor 設定值
# 提交
$ git commit -m "First commit."
reset
Reset HEAD / hash id 狀態
# git reset
#
# --soft: 保留索引
# --hard: 強制回到初始狀態(使用小心)
# 強制 reset HEAD
$ git reset HEAD --hard
rm
移除檔案索引
# git rm
#
# --cached 保留檔案
# -f 強制刪除
# 移除索引
$ git rm --cached newFile.txt
# 移除索引並刪除檔案
$ git rm -f newFile.txt
Branching and Merging
branch
顯示/建立/刪除 分支
# git branch
#
# -d: 刪除分支
# -D: 刪除分支即使還沒合併
# -f: 強制
# 顯示分支
$ git branch
# 建立分支 new
$ git branch new
# 刪除分支 new
$ git branch -d new
checkout
切換分支
# git checkout
#
# -b: 建立分支並切換
# 切換到前一個分支
$ git checkout -
merge
合併分支
# git merge [branch]
# 合併分支 new
$ git merge new
mergetool
用合併工具合併衝突
git config:
- merge.tool 設定合併工具
- mergetool.keepBackup false 成功合併自動刪除
*.orig
檔案
log
顯示提交紀錄
# git log
#
# --oneline: --pretty=oneline --abbrev-commit
# 顯示master new 分支差異紀錄
$ git log master..new
stash
儲存追蹤檔案狀態
# git stash
#
# save [message]: 儲存狀態並給訊息
# list: 列出所有
# clear: 清空
# pop [index]: 取出
# show [index]: 顯示狀態
# Stash all tracked files without a message/description to index 0
$ git stash
# List the current stashes
$ git list
# Pop stash index 2
$ git pop 2
tag
標籤
# git tag
#
# -a: 名稱
# -m: 訊息
$ git tag -a MyTag -m "Thi is my tag"
Sharing and Updating Projects
fetch
抓取差異值,不改變 code
# git fetch <remote> <place> (<place> = source:destination)
# 新增分支
$ git fetch origin :newBranch
pull
# git pull <remote> <place> (<place> = source:destination)
#
# --rebase: 以 rebase 合併,而不是 merge
# 以 rebase 合併
$ git pull --rebase
git fetch + git merge
push
# git push <remote> <place> (<place> = source:destination)
#
# -u, --set-upstream: set upstream for git pull/status
# 推送新分支
$ git push origin newBranch
# 刪除遠端分支
$ git push origin :newBranch
remote
# git remote
# -v: 詳細
# show origin: 顯示遠端分支資訊
# add upstream: 新增遠端分支
# prune: 刪除有追蹤但遠端已刪除的分支
# 列出遠端已刪除的分支
$ git remote prune -n origin
Inspection
show
查看指定提交紀錄
# git show [hash id]
blame
追蹤檔案異動
# git blame [file]
Patching
cherry-pick
選用已存在的提交,可多個,範圍
# git cherry-pick
$ git cherry-pick hash
$ git cherry-pick feat1~2..feat1~0
rebase
線性合併
# git rebase
#
# -i: 互動模式
revert
復原修改,會有提交紀錄
# C0->C1->C2->C2'
$ git revert C2
Administration
clean
Remove untracked files from the working tree
# git clean
#
# -n: 列出欲刪除檔案
# -f: 強制
# -d: 整個資料夾
reflog
查詢 git 操作紀錄
archive
備份儲存庫
# 備份某個分支
$ git archive master --format=zip --output=../repbck.zip
# 備份某次提交
$ git archive HEAD --format=zip --output=../headbck.zip
bundle
打包整個儲存庫
# 打包
$ git bundle create ../repo.bundle master
# 還原
$ git clone repo.bundle repo -b master