---
title: "【冷知識】為什麼大家都說在 Git 開分支「很便宜」？ - 為你自己學 Git | 高見龍"
description: "最白話、最深入淺出的 Git 教學，教您使用 Git 指令及圖形介面工具，建立正確的使用觀念，並使用 GitHub 與其它人一起共同協作"
canonical: "https://gitbook.tw/chapters/branch/why-branch-is-cheap.html"
image: "https://gitbook.tw/assets/images/tw/etc/og-image-gitbook.jpg?v=8eb3685e"
---

# 【冷知識】為什麼大家都說在 Git 開分支「很便宜」？

所謂的分支，其實就只是一個有 40 個字元的檔案而已，它藏在 .git 目錄裡面，讓我們進去挖出來給大家看看。先看一下現在有哪些分支：

```
$ git branch
  cat
  dog
* master

```

目前總共有 `cat`、`dog` 以及 `master` 三個分支。

![branch](https://gitbook.tw/images/tw/branch/why-branch-is-cheap/branch.png)

分支的資訊放在哪裡？打開 `.git` 目錄，裡面有一個 `refs` 目錄，再往下有個 `heads` 子目錄：

![branch](https://gitbook.tw/images/tw/branch/why-branch-is-cheap/branch-files.png)

所謂的分支其實就是放在這裡了！

> ### 小提示
> 
> `.` 小數點開頭的目錄跟檔案通常預設會被作業系統標記成隱藏檔，所以可能會需要打開「檢視隱藏檔」的設定才看得到喔。

看一下這些檔案的內容：

```
$ cat .git/refs/heads/master
e12d8ef0e8b9deae8bf115c5ce51dbc2e09c8904

$ cat .git/refs/heads/cat
dd8d48000140bcc66ed0aa5630b6072d5956d32e

$ cat .git/refs/heads/dog
919df8e360cf2482ff42dd0566f615263fa17214

```

這些數字好像在哪裡看過？你再比對一下上面 SourceTree 的那個畫面，就會發現其實這三個檔案的內容，就只是某個 Commit 的 SHA-1 值而已！

## 那如果把這些檔案刪掉會怎麼樣嗎？

那就把 `.git/refs/heads/dog` 這個檔案刪掉吧：

```
$ rm .git/refs/heads/dog

```

這時候的分支列表：

```
$ git branch
  cat
* master

```

那個 `dog` 分支就不見了。是的，不用懷疑，在 Git 的分支就只有這樣而已，不信的話，我們來把 `.git/refs/heads/cat` 這個檔案改名成 `bird` 看看：

```
$ mv .git/refs/heads/cat .git/refs/heads/bird

```

再回來看分支列表：

```
$ git branch
  bird
* master

```

原來的 `cat` 分支變成 `bird` 分支了。

所以，回來原來的問題，為什麼大家都說在 Git 開分支「很便宜」？ 所謂的分支不就是一個只有 40 個字元（某個 Commit 的 SHA-1 值）的檔案，所以在 Git 開一個分支是能多「貴」呢 :)

```json
{
      "@context": "http://schema.org",
      "@type": "Organization",
      "url": "https://gitbook.tw",
      "logo": "https://gitbook.tw/assets/images/tw/etc/og-image-gitbook.jpg?v=8eb3685e"
    }
```
