[筆記]learning-Git-2019

Ashe_Li
Ashe’s Note
Published in
8 min readJul 13, 2019

前言

TL ; DR
First, if it isn’t yet clear, Git is fundamentally a content-addressable filesystem.
首先要弄明白一點,從根本上來講 Git 是一套內容定址 (content-addressable) 檔案系統

— by Git docs

00.重新認識 Git

  • 以前認識的刪除分支 Git branch
git branch -D newBranch

可以注意 最上方”newBranch”的分支線 ,刪掉就沒有了 ?!

  • 現在認識的刪除分支 branch
git branch -D newBranch

事實上並沒有被刪掉,只是「看不到(變成虛線)」。

可以先反覆看兩個例子的差異,再繼續往下。

可以想像一下, git 的 branch 是貼紙(一張大小約為 41位元的貼紙),
貼在你想標註的地方。
所以刪除的操作,只不過是把貼在 commit 上面的貼紙撕下來,commit 本身並沒有消失。

你可以把貼紙貼回去

git branch <branchname> [<commit>]
  • <branchname> 指的是你想命名的分支名稱,後續都會用這種表示法。
  • <commit> 是你想要指定的 commit,通常是一串很像亂碼的 sha-1,只需要輸入部分即可。
  • 看到中括號 [ ] 把 <commit> 包起來,中括號通常表示 可選參數 ,若不輸入指定的 <commit> 則會在當前位址產生新的分支。

範例:

黃色部分就是可以確認 <commit> 的地方,長短意思都是一樣的,所以通常只看到用短的。

做一個簡單的示範,我想讓之前刪掉的 newBranch 貼紙,貼回 885fc69 的位置

 git brach newBranch 885fc69

登愣!恢復了!!

*註:

SourceTree + 指令的版本,因為本地端的commit和上圖不一樣,所以輸入的commit(sha-1)值也會不一樣。

等等!
你說我以前是錯的?
你怎麼確定?
最好給我一個合理得解釋!

Q: 看不到 怎麼找回來?
A:可以用 git reflog 查看

Q: 如何復原刪除的分支?

其實沒有「被刪除」,只是看不到,可以回復,
使用的指令是:

git branch <branchname> [<commit>]

<branchname> 指的是你想命名的分支名稱,後續都會用這種表示法。
<commit> 是你想要指定的 commit,通常是一串很像亂碼的 sha-1,只需要輸入部分即可

是我想要讓它長出來的地方

登愣!恢復了!!

附上 source tree 版本

1. clone repo* [myGitHub](https://github.com/lucifiel0121/learning-git)
2. [Sourcetree](https://www.sourcetreeapp.com/)
3. [using visualizing-git](http://git-school.github.io/visualizing-git/)
- more infomation and practice here:http://onlywei.github.io/explain-git-with-d3/

gitbook from https://gitbook.tw/

also can get info from (Amos’s yt 直播)[ https://www.youtube.com/watch?v=Hl4tuzqaFJk]

— — -

before start :
- Basic Commands
1. git status
2. git add
3. git add .
4. git commit
5. git branch
- Remote Server
6. git push
7. git pull
8. git fetch
- Combine Branches
9. git merge

  • branch
    - 對分支的誤解 :樹枝 鐵軌
    - real : 貼紙
    - 畫圖解釋:貼紙
    HEAD 貼到任意地方 :

    HEAD + cat

HEAD + master

git branch dog : 增加一個 dog 的貼紙貼在 HEAD的位置
result: HEAD + dog + master

因為 HEAD 貼在 master,所以 git commit 只有 HEAD + master會往前跑
dog是一個貼紙,貼在之前的 e9b0e52 節點(commit)上

把位置貼在 HEAD 本身

使用`git branch -d <new>` 把 new 貼紙刪除

- 雜湊值(40 個字元長度 bit 的 SHA-1 字串)

  • reset

原本的圖型示意

使用 git reset e9b0e52 ,reset只是移動貼紙位置
後面的參數是對`紙貼的位置之後的節點(commit) 處理方式`

把下面的分支移到最前面,`看起來`被刪掉,其實`只是看不到`

如果`記得 Commit 名稱`,還是可以把貼紙貼回去
`git branch master 67fdf0b` :新增一個 `master` 在 `67fdf0b` 分支

忘記`Commit 名稱` 可以用 `git reflog` (reference log)

merge 後悔? `git reset ^ — hard`
-> git reset ^ : 貼紙撕起來貼回前一部
-> — hard : 其餘的全部不要留

>不加參數 defualt : mixed
— mixed:丟工作目錄
— soft :丟到暫存區 (隱含 `git add .`)
— hard :其餘的全部不要留

## rebase
`re`: 重新接
`base`:長出去節點的根

貼紙還是在 HEAD (#註1)

重新把她變回原本的圖,把貼紙貼回 master 再 `git rebase 2571caf` 一次

`366c16e` 後面那一串是操作 ,(#註1) 的圖

ORIG_HEAD
`git reset ORIG_HEAD — hard` : ORIG_HEAD是危險操作的暫存

## tag
87%像 ,差異在 tag 不會移動,會固定在commit上
tag 有不同等級。

  • 這篇文章是想測試看看 到底圖片比較好懂還是 gif ,希望有些回應~ 如果有人有回應超過10% read 數量 ,在考慮把 圖片更新成 Gif XDD
  • 其實直播更好懂,這篇只是筆記。
  • 直播(大師常來聊-高見龍談 Git 版控之final12345 | GIT教學 | 網頁教學 | 版本控制 by. CSScoke ) 紀錄網址
    https://www.youtube.com/watch?v=Hl4tuzqaFJk

--

--