Git設定

York Chen
8 min readAug 12, 2020

--

首次使用者設定

git config --global user.name york
git config --global user.email york@xxx.com
git config --global alias.st status
git config --global alias.sts "status -s"
git config --global alias.br branch
git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.cm "commit --amend -C HEAD"
git config --global alias.re remote
git config --global alias.di diff
git config --global alias.type "cat-file -t"
git config --global alias.dump "cat-file -p"
git config --global alias.lo "log --oneline"git config --global help.autocorrect 30
git config --global core.autocrlf false
git config --global core.quotepath false
git config --global color.diff auto
git config --global colot.status auto
git config --global color.branch auto
git config --global --list # 列出使用者設定git config --global color.ui auto # 讓 git 有顏色
git config --global core.editor sublime # 變更預設編輯器

常用指令

在Github開repo,把code從Github抓下來

git init
git clone [remote_url] [directory_name]

查詢檔案狀態

git status
git status -s #查修改那些檔案

比較檔案

git diff  [file-name]
git diff [first-bcommit] [second-commit]
git diff --staged # 檢視加入索引的檔案和前次提交的檔案有何差異
git diff [first-branch] [second-branch] # 檢視兩個 branch 的檔案有何差異
git diff | pbcopy # 把內容複製到剪貼簿,可以存成 .diff 檔就可以看到差異
git show [commit] # 檢視某一次 commit 有何差異

拉檔案和遠端同步

git pull origin master

復原檔案

git checkout .                    #將所有檔案復原到最後一次commit的狀態 
git checkout [fileName] #復原檔案
git checkout -b [branch-name] #切換分支
git branch -d [branch-name] #刪除分支
git checkout [fileName] #2.23以後的復原檔案

分支

git branch                        #查詢分支狀況
git branch [branch-name] #建立分支
git checkout -b [branch-name] #切換分支
git branch -d [branch-name] #刪除分支
git switch [branch-name] #2.23以後的切換分支

一般的合併方式,切換到master、合併分支並push到遠端分支

git add .
git add [fileName]
git commit -m ["commit-message"]
git checkout master
git merge [branch-name] 和master merge起來
git push -u origin [branch-name]
改變根基的方式
git pull --rebase

修改commit

git commit --amend         
# 修改當前 commit 訊息內容,ESC :wq 可離開 amend
git commit --amend -m "[message]"
# 將新的 commit 合併到上一次的 commit 中(上一次的 commit 的內容會被覆蓋)

merge

優點:知道分支的歷史紀錄

git merge  [branch-name]  在目前的分支合併另一分支
git merge [branch-name] --no-ff 不希望 Git 使用 Fast Forward 方式快轉合併,可以完整保留分支(有小耳朵)
git reset --merge #回到合併分支之前的狀態,
git merge --abort #回到合併分支之前的狀態, Git 1.7.4 版本之後,可使用

rebase(變換根基的合併方式)

缺點:合併後的分支踩在原本的分支上,會看不出原本分支的樣子(歷史紀錄)

git checkout [branch1]
git rebase [branch2] # branch1採到branch2上

reset 是指「go to」或「become」的概念。

git reset HEAD [filename]         # 取消已經被 add 的檔案git reset HEAD^       
git reset HEAD~1
# 取消最後一次提交的 commit ,檔案內容回到 unstaged 狀態(已編輯的檔案會被保留)
git reset HEAD~1 --hard
# 取消最後一次提交的 commit ,檔案內容回到 unstaged 狀態(已編輯的檔案不會被保留)
git reset HEAD [commit] --hard
# 取消最後一次提交的 commit ,檔案內容回到特定 [commit]版本 狀態(已編輯的檔案不會被保留)
git reset [commit] 檔案內容回到特定 [commit]版本(已編輯的檔案會被保留)
git reset [commit] --mixed 檔案內容回到特定 [commit]版本(已編輯的檔案會被保留) (預設)
git reset [commit] --soft 檔案內容回到特定 [commit]版本(已編輯的檔案會留在暫存區)
git reset [commit] --hard 檔案內容回到特定 [commit]版本(已編輯的檔案不會被保留)
git reset --hard ORIG_HEAD
# 將不小心透過上行指令刪除的 commit 復原

log

git log
git log --oneline #log用一行顯示
git log -p [FileName] # 檢視單一檔案的歷史紀錄
git log --follow [FileName] # 針對某一支檔案顯示 log
git log --graph # 以圖表方式顯示分支歷史
git reflog
# 每次 HEAD 移動時就會記錄狀態(保留 30 天),因此可以檢視最完整的編輯記錄(包含 reset)
git blame [FileName]# 顯示某一支檔案內容的完整編輯記錄,抓出每行程式碼是誰寫的
git blame -L 5,10 [FileName]
# 顯示某一支檔案內容的完整編輯記錄(只顯示特定行數)

git stash 暫存

git stash (save)                          # 把目前狀況存下來
git stash save [description] # 把目前的狀況存下來,並加上描述
git stash apply # 把最後一次 stash 拿出來用
git stash pop [stash_key] # 把 stash 拿出來用並且將該暫存從 stash 移除(等同於 apply + drop)
git stash drop # 刪除最後一的 stash
git stash drop stash@{0} # 刪除特定一次的 stash
git stash list # 查看所有暫存的項目
git stash -u # Untracked 狀態的檔案預設沒辦法被 Stash,需要額外使用 -u 參數

和遠端同步,重推分支

先拉最推
git pull
git pull --rebase #把自己的拉到最前面,歷史紀錄乾淨一點
git stash
git rebase master
git push -u origin pufii

git rm 刪除檔案

# git rm 刪除檔案
git rm [file_name] # 將該檔案從資料夾中刪除
git rm [file_name] --cached # 清除追蹤某個已經被加入 git 版控的檔案(不會從資料夾中刪除)
git rm -r --cached path_to_your_folder/ # 清除追蹤某個已經被加入 git 版控的資料夾(不會刪除檔案)
# git 目錄檔
rm -rf .git # 刪除這支檔案等於把整個 git 對這個專案的版控刪除

搭配使用VS Code套件:Git Graph

--

--

York Chen
York Chen

Written by York Chen

Cooking is an indispensable part of life. It is a taste that can’t be forgotten.

No responses yet