介紹開始使用 Git 前調教 config 的相關資訊
初始化 Git Repository 1 2 3 [root@centos7 git-lab] Initialized empty Git repository in /root/git/git-lab/.git/
Git config 常用 Git config (~/.gitconfig) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 [user] email = godleon@gmail.com name = godleon [core] editor = vim repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true [color] ui = true [alias] tree = log --all --graph --decorate=short --color --format=format:'%C(bold blue)%h%C(reset) %C(auto)%d%C(reset)\n %C(black)[%cr]%C(reset) %x09%C(black)%an: %s %C(reset)' logs = log --all --graph --decorate=short --color --format=format:'%C(bold blue)%h%C(reset)+%C(dim black)(%cr)%C(reset)+%C(auto)%d%C(reset)++\n+++ %C(bold black)%an%C(reset)%C(black): %s%C(reset)' stree = !bash -c '" \ while IFS=+ read -r hash time branch message; do \ timelength=$(echo \"$time\" | sed -r \"s:[^ ][[]([0-9]{1,2}(;[0-9]{1,2})?)?m::g\"); \ timelength=$(echo \"16+${#time}-${#timelength}\" | bc); \ printf \"%${timelength}s %s %s %s\n\" \"$time\" \"$hash\" \"$branch\" \"\"; \ done < <(git logs && echo);"' logv = log --all --graph --decorate=short --color --format=format:'%C(bold blue)%h%C(reset)+%C(dim black)(%cr)%C(reset)+%C(auto)%d%C(reset)++\n+++ %C(bold black)%an%C(reset)%C(black): %s%C(reset)' vtree = !bash -c '" \ while IFS=+ read -r hash time branch message; do \ timelength=$(echo \"$time\" | sed -r \"s:[^ ][[]([0-9]{1,2}(;[0-9]{1,2})?)?m::g\"); \ timelength=$(echo \"16+${#time}-${#timelength}\" | bc); \ printf \"%${timelength}s %s %s %s\n\" \"$time\" \"$hash\" \"$branch\" \"$message\"; \ done < <(git logv && echo);"'
Git config 優先權 Git config 的優先權如下:
.git/config
~/.gitconfig
/etc/gitconfig
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 [root@centos7 git-lab] [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true [root@centos7 git-lab] core.repositoryformatversion=0 core.filemode=true core.bare=false core.logallrefupdates=true [root@centos7 git-lab] fatal: unable to read config file '/etc/gitconfig' : No such file or directory [root@centos7 git-lab] fatal: unable to read config file '/root/.gitconfig' : No such file or directory
設定 & 移除 git config 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [root@centos7 git-lab] [root@centos7 git-lab] [root@centos7 git-lab] user.name=godleon user.email=godleon@gmail.com core.repositoryformatversion=0 core.filemode=true core.bare=false core.logallrefupdates=true [root@centos7 git-lab] [root@centos7 git-lab] user.email=godleon@gmail.com core.repositoryformatversion=0 core.filemode=true core.bare=false core.logallrefupdates=true
設定 Git alias 跟 Linux 的 alias 很像,可以自訂簡單的命令來替代複雜的指令:
1 2 3 4 5 6 7 8 9 [root@centos7 git-lab] [root@centos7 git-lab] user.email=godleon@gmail.com core.repositoryformatversion=0 core.filemode=true core.bare=false core.logallrefupdates=true alias.con=config -l
git config --global color.ui true
:加上顏色
git config --global core.editor vim
:把預設的文字編輯器改為 vim
git config --global diff.tool meld
:把預設的 diff tool 改為 meld (要先安裝 meld,且這是 GUI tool,不適用 text mode)
git difftool
:查詢檔案差異
將檔案存入 Git Repository .gitignore Git 追蹤檔案的方式,會將檔案 & 資料夾分成三類:
untracked
一開始所有檔案都是 untracked,執行 git status
可以看到 untracked 的檔案清單
tracked
當透過 git add
處理之後的檔案,狀態會變成 tracked,等待使用者進一步的 commit or cancel
ignored
會被 git 忽略處理的檔案,需要新增 .gitignore
檔案,並逐行宣告要忽略的檔案 (被設定忽略的檔案狀態不會變成 untracked)
以下為 .gitignore
設定範例:
1 2 3 4 5 6 *.txt !note.txt folder
git commit 1、從 tracked 復原到 untracked 1 2 3 4 5 [root@centos7 git-lab] [root@centos7 git-lab]
2、從已經 commit 後的狀態回復原狀 首先先來看看如何顯示目前 commit 的狀態
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 [root@centos7 git-lab] * 72f32d4 (HEAD, master) | [25 minutes ago] root: commit of file1 * fbaedbb [root@centos7 git-lab] * 72f32d4 (HEAD, master) commit of file1 * fbaedbb First commit [root@centos7 git-lab] * commit 72f32d4 (HEAD, master) | Author: root <godleon@gmail.com> | Date: 23 minutes ago | | commit of file1 | * commit fbaedbb Author: root <godleon@gmail.com> Date: 9 hours ago First commit [root@centos7 git-lab] [root@centos7 git-lab] nothing added to commit but untracked files present (use "git add" to track) [root@centos7 git-lab] * fbaedbb (HEAD, master)
比較檔案差異 & 從 Git Repository 取回檔案 比較檔案差異 git 提供了很多方式來檢視不同版本間的檔案差異,主要就是使用 git diff
& git difftool
來完成此功能,以下用個簡單範例來說明:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 [root@centos7 git-lab] * 81618ad (HEAD, master) | [2 hours ago] godleon: 2nd commit for file1.txt * fbaedbb [2 days ago] root: First commit [root@centos7 git-lab] no changes added to commit (use "git add" and/or "git commit -a" ) [root@centos7 git-lab] diff --git a/file1.txt b/file1.txt index 7cca94a..3aff906 100644 --- a/file1.txt +++ b/file1.txt @@ -1,2 +1,3 @@ file1, line 1 file1, line2 +file1, line3 [root@centos7 git-lab] [root@centos7 git-lab] diff --git a/file1.txt b/file1.txt index 7cca94a..3aff906 100644 --- a/file1.txt +++ b/file1.txt @@ -1,2 +1,3 @@ file1, line 1 file1, line2 +file1, line3 [root@centos7 git-lab] diff --git a/file1.txt b/file1.txt deleted file mode 100644 index 7cca94a..0000000 --- a/file1.txt +++ /dev/null @@ -1,2 +0,0 @@ -file1, line 1 -file1, line2
References