筆記:版本控制系統 GIT 常用指令

2019112022:13
 

第一次使用,設定個人資料

$ git config --global user.name "Helloff"
$ git config --global user.email "[email protected]"
 
$ git config --local user.name "name22"
$ git config --local user.email "[email protected]"
 
查詢目前的設定
$ git config --list
credential.helper=osxkeychain
[email protected]
user.name=Helloff
 

快速鍵設定

$ git config --global alias.co checkout   
  git co = git checkout
 
$ git config --global alias.br branch
$ git config --global alias.st status
$ git config --global alias.ll "log --oneline --graph"
  git ll  = git log --oneline --graph
 
config設定資料都儲存於:
~/.gitconfig

內容像這樣:
[user]
        name = Helloff
        email = [email protected]
[push]
        default = matching
[alias]
        st = status
        br = branch

初始化

 
$ cd gittest
gittest $ git init       建立 Repository
Initialized empty Git repository in /home/gittest/.git/

gittest $ vi index.txt  建立一個檔案
 
gittest $ ls -l
total 4
-rw-r--r-- 1 user user 11 Nov 19 17:33 index.txt
 
gittest $ git status
On branch master
 
Initial commit
 
Untracked files:
  (use "git add <file>..." to include in what will be committed)
 
        index.txt
 
nothing added to commit but untracked files present (use "git add" to track)
 
 
gittest $ git add --all
  或
gittest git add .

  或指定檔名
gittest git add index.txt
gittest $ git status
On branch master
 
Initial commit
 
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
 
        new file:   index.txt
 
 
commit -> 放到 Repository 儲存庫
gittest $ git commit -m "first commit"
[master (root-commit) 1c5a8ee] first commit
1 file changed, 2 insertions(+)
create mode 100644 index.txt
 
 

查詢紀錄

 
查看紀錄
$ git log
commit 78c3c10f3ccca5beb0d1ab38520d9a3b0a015c65
Author: Hellff <[email protected]>
Date:   Tue Nov 19 17:42:06 2019 +0800
 
    #2 commit
 
commit 1c5a8ee05cf79030032b52f02f65ecbe47749388
Author: Hellff <[email protected]>
Date:   Tue Nov 19 17:35:05 2019 +0800
 
    first commit
 
$ git log --oneline --graph  #比較精簡的顯示方式
* 1c5a8e  (HEAD -> master) #2 commit
* 1c5a8e first commit
 
用關鍵字查詢過去 commit 訊息的資料
$ git log --oneline --grep="com"
78c3c10 #2 commit
1c5a8ee first commit

其他查詢方式: 查某個作者
$ git log --oneline --author="姓名"
commit "所有的檔案" 內含有特定關鍵字:
$ git log -S "Ruby"
 
查詢特定時段內 做了哪些 commit
$ git log --oneline --since="9am" --until="12am"

 

 

修改已經 Commit 紀錄

Rebase
Reset
--amend  修改最後一次的 commit 紀錄
 
$ git commit --amend
會跳到編輯器中讓你修改 commit 資料


修改多個過去的 commit 訊息
互動模式 (跳到 vi 中編輯):
$ git rebase -i

把多個 Commit 合併成一個 Commit

$ git rebase -i
vi 中編輯 squish

 

追加檔案到最後一次的 Commit

在檔案修改過後(或新增檔案)
$ git add .
 
$ git commit --amend
 
 
 

有些檔案我不想放在 Git 裡面

例如密碼檔案
 
把不想被放到 git 裡的檔案,寫在這個檔案裡
vi .gitignore
index.htm
images/abc.jpg
hello.htm
:: ::
 
特別注意
若 hello.htm 在寫入 .gitignore 之前就已經被 commit 過
那 若再修改過 hello.htm 時,仍會被 git 所感應 (也就是說,目前 .gitignore 中寫 hello.htm 是無效的)
 
必須:
  
$  git rm --cached hello.htm 刪掉 git 中的 hello.htm 檔案
$  git clean -fX   刪掉 git 中的所有列在 .gitignore 的檔案
  (f  指 force)
 
 
 

查看某一檔案的 commit 紀錄


$ git log index.htm
commit 100656a6e24943800e3d9ec8d397ac8b7eb1f4b3
Author: helloff <[email protected]>
Date:   Wed Feb 13 19:54:11 2019 +0800
 
    cat commit 1
 
commit 3776486ae7cbc67d923af38fe9f7b858a32ef80f (origin/master)
Author: helloff <[email protected]>
Date:   Tue Jan 8 11:29:55 2019 +0800
 
    cat commit 2
 
 
查看 index.htm 每次 commit 到底有哪些變動
$ git log -p index.htm
commit 100656a6e24943800e3d9ec8d397ac8b7eb1f4b3
Author: helloff <[email protected]>
Date:   Wed Feb 13 19:54:11 2019 +0800
 
    cat commit 1
 
diff --git a/cat.htm b/cat.htm
index 5a0aed4..98b7039 100644
--- a/cat.htm
+++ b/cat.htm
@@ -1,2 +1,4 @@
cat.htm
+cat 2019-02-13
+
 
commit 3776486ae7cbc67d923af38fe9f7b858a32ef80f (origin/master)
Author: helloff <[email protected]>
Date:   Tue Jan 8 11:29:55 2019 +0800
 
    g1 #2
 
diff --git a/cat.htm b/cat.htm
new file mode 100644
index 0000000..5a0aed4
--- /dev/null
+++ b/cat.htm
@@ -0,0 +1,2 @@
+cat.htm
+

 

查詢檔案的"每一行變動"是誰修改的

 
$ git blame 2.htm
bfeef916 (ff Shieh 2018-08-11 11:02:07 +0800 1) 2.htm
84d5a41c (hellofff  2019-01-08 11:16:31 +0800 2) edit 001
84d5a41c (helloff  2019-01-08 11:16:31 +0800 3) edit 002
bb60363b (ff Shieh 2019-01-08 11:28:54 +0800 4)
bb60363b (ff Shieh 2019-01-08 11:28:54 +0800 5) 333
bb60363b (ff Shieh 2019-01-08 11:28:54 +0800 6) 333
bb60363b (ff Shieh 2019-01-08 11:28:54 +0800 7) 444
bb60363b (ff Shieh 2019-01-08 11:28:54 +0800 8) 555

 

檔案誤刪時 (或改錯了)

 
救回指定的檔案 (拿回上次 commit 的檔案)
$ git checkout 2.htm
 
拿回"上上"次 commit 的檔案
$ git checkout HEAD~2 2.htm
 
救回全部檔案
$ git checkout .


剛才的 Commit 後悔了,想要拆掉

不會影響到你專案的檔案內容

$ git log --oneline
0e6f0aa (HEAD -> master) add sencond.txt   <--想拆掉這一次的commit
d98ea58  33 commit 333gogo
4619eeb 33 commit
a58216f first commit

$ git reset 0e6f0aa^      = git reset HEAD^
  一個 ^ 符號代表拆掉  "上一次"commit    

  兩個 ^^ 符號代表拆掉 "上上一次"commit

  或是拆掉50次commit (回到前50次commit)
  git reset 0e6f0aa~50

$ git log --oneline
d98ea58 (HEAD -> master) 33 commit 333gogo
4619eeb 22 commit
a58216f first commit


查詢
$ git reflog
d98ea58 (HEAD -> master) HEAD@{0}: reset: moving to 0e6f0aa^
0e6f0aa HEAD@{1}: commit (amend): add sencond.txt
cccb5ef HEAD@{2}: commit: 22:55
d98ea58 (HEAD -> master) HEAD@{3}: commit (amend): 33 commit 333gogo
fc2ff3e HEAD@{4}: commit (amend): 33 commit 333gogo
d991820 HEAD@{5}: commit: #3 commit
4619eeb HEAD@{6}: commit: 22 commit
a58216f HEAD@{7}: commit (initial): first commit


查詢 git 物件的型態/內容

 
$ git cat-file 0282b4e0bda1b90243af15b94391e5767b26698f -t
tree
 
$ git cat-file 0282b4e0bda1b90243af15b94391e5767b26698f -p
100644 blob 8614522ad2e81150edd59cccf40cda68500d18c6    index.txt
100644 blob 8f5fe4780fd46c409386c3f69555127da0052535    test.txt
 
 
$ git cat-file 8614522ad2e81150edd59cccf40cda68500d18c6 -t
blob
 
$ git cat-file 8614522ad2e81150edd59cccf40cda68500d18c6 -p
hello1
hello2
hello2
 
 

計算 sha1


$ cat README.md | git hash-object --stdin
8b9f00411dbf4302c5e2260a7ea60dac6b1f1bad

$ echo "hello" | git hash-object --stdin
ce013625030ba8dba906f756967f9e9ca394464a



Branch 分支

$ git checkout -b bird
Switched to a new branch 'bird'
切換到 bird 分支,若 bird分支不存在=>就自動建立 並切換過去


刪除一個分支
$ git branch -d [分支名稱]
** 若 branch 沒被合併過,會出現警告訊息


強制刪除一個分支(不會有警告訊息)
$ git branch -D [分支名稱]
Deleted branch cat (was 6fb6143)
相當於  git branch -d --force [分支名稱]


救回剛剛刪除的分支
$ git branch newcat 6fb6143



合併分支
#1 先切換回主線
$ git checkout master 

#2 將 cat 分支合併到目前的主線 master
$ git merge cat


標籤

設定標籤
$ git tag betatag1  8614522ad2e81150edd59cccf40cda68500d18c6


$ git tag betatag1  8614522ad2e81150edd59cccf40cda68500d18c6 -a -m "beta version"


刪除標籤
$ git tag -d betatag1 

 

其它


$ git ls-files -s

$ git ls-files

$  git fsck --unreachable




GIT 常用的指令與作業流程

 
--------

其它參考資料

人生不能重來,但 GIT 可以

線上影片教學 ,付費
深入淺出的內容,100分 推薦~~~
    這個課程也有免費的文字版
     為你自己學 Git https://gitbook.tw/ (部分單元有影片教學)
 

Git 版本控制系統 | Git 基本操作 - ihower 的 Git 教室

GIT 免費線上教學 - 六角學院
$ git ls-files -s
100644 6f76fd632c83ebcf6cfc9ce9d41992c03eada462 0       README.md
100755 6ef76ca68573074c256d48a2a1e8faedca3920e0 0       _data/001.htm
100755 4cee01e41880261de8a161dccdffbb6965f21368 0       _data/002.htm
100755 149a9b2d065e8ea2a0adb9247f1baf8efc9043ee 0       _data/Introduction.jpg
100755 f078733ff9a5e2e77f47f64952891414a0ef077f 0       _data/S__52355228.jpg
100755 177a7a1aee7fd39763a37520e9580b5f6f6a1b4c 0       _data/bg.jpg