2017年4月14日 星期五

Git 常用指令筆記

Basic Workflow
git init 將當前目錄設為working directory
rm -rf .git 刪除git的追蹤

git status 顯示working directory中已修改檔案清單
-s 僅會顯示檔案名稱
-b 顯示分支的資訊
-s -b 僅顯示分支與檔案名稱

git add [filename] 註冊filename到staging area
git add . 註冊所有檔案

git diff 列出working directory與repository的差異
git diff [filename] 僅列出filename與repository的差異
git diff [commit1] [commit2] 比較commit1與commit2的差異

git commit 將staging area的檔案提交到repository
git commit --amend 可以用來修改上次的 commit內容及 log
git commit --amend --author="authorname <authoremil>" --no-edit 修改上次提交
的 Author,--no-edit代表不開啟編輯

git log 顯示commit的列表
git log --pretty=oneline 使用單行顯示commit的列表
gitk 使用圖形化界面顯示commit的列表


Git Config
git config --global user.name "[username]" git config --global user.email "[useremail]" 設定全局的 ~/.gitconfig設定檔中的使用者名稱及 email,通常在第一次使用 git時就設定

git config --local user.name "[username]" git config --local user.email "[useremail]" 設定此目錄 .git/config設定檔所要使用的使用者名稱及 email

git config --global --list git config --local --list 列出全局或此目錄設定檔中的設定

How to Backtrack
HEAD為最近一次的commit

git show 查看 commit的詳細記錄
ex: git show HEAD 查看最近一次commit的紀錄

git checkout HEAD [filename] 把在工作目錄中更改過的filename還原成最近commit的狀態

git checkout [commit_SHA] checkout到 commit_SHA。這時狀態為 dached HEAD state,意思是 HEAD指向某個
commit,而不是 branch。
git switch -
復原這個操作,將 HEAD重新指向 branch。
git switch -c [new_branch]
在此狀態下建立到新的 branch。

git reset [filename] 把在Staging Area中的filename移出Staging Area

git reset HEAD [filename] 把在Staging Area中的filename移出Staging Area並還原成最近的commit狀態

git reset [commit_SHA] 改變git log的history,將commit_SHA 變為最近的 commit,檔案移出 staging area

git reset --soft [commit_SHA] 將 HEAD指向 commit_SHA,不改變git log的history

git reset --hard [commit_SHA] 將 commit_SHA變成最近的 commit,移除之前所有的commit

git cherry-pick [commit_SHA] 將另一個 branch的 commit_SHA "摘下來",加進當前的branch

Git Branching
git branch 顯示目前所在分支

git branch [new_branch] 建立名為new_branch的branch
-d 刪除branch

git checkout [branch_name] 切換分支到branch_name
-b 建立名為 branch_name的分支,並切換到該分支

git merge [branch_name] 將branch_name所做的改變加到當前分支


Git Teamwork
git clone [remote_location] [clone_name] 將遠端的Repository(remote_location URL)複製到Local端命名為clone_name

git remote 顯示遠端Repository的詳細情況
git remote -v 顯示目前 remote所使用的名稱及 URL
git remote set-url [remote_name] [remote_url] 修改 remote所使用的 URL

git fetch 更新Local端Repository


標籤
git tag 列出標籤
git tag [tag_name] 輕量級標籤(lightweight)
git tag -a [tag_name] -m [message] commit_SHA 含附註標籤

git push origin [tag_name] 將標籤推送到遠端
git push --tags 推送所有標籤到遠端

Submodule
git submodule add [remote_location] [submodule_name] 將遠端的Repository(remote_location URL) 設為submodule,並放到submodule_name folder中

Patch
git format-patch -[n] 產生最近n個commit的patch

git format-patch [commit_SHA] 產生commit_SHA之後到最新commit的所有patch (不含commit_SHA)

git format-patch -1 [commit_SHA] 只產生commit_SHA的patch

git apply [patch] 匯入patch但不會commit
git apply --check [patch] 檢查patch是否能apply成功,不會做任何改動

git apply -R [patch] 撤銷 patch但不會commit

Stash
git stash push 將當前的修改存到 "stash entry",並恢復到 HEAD

沒有留言:

張貼留言