分享到plurk 分享到twitter 分享到facebook

版本 f31bea9365c81c24364dcdab31b5ad1d51f24bed

git

Changes from f31bea9365c81c24364dcdab31b5ad1d51f24bed to c8de8669508f658c278ea01c85400d09867acbee

---
title: Git
categories: 版本控制
...

簡介
====

關於 git 的簡介可參考 Wikipedia: `Git<http://zh.wikipedia.org/wiki/Git>`_

安裝
====

在 Ubuntu/Debian 上,``sudo apt-get install git-core``

簡單操作
=======

初始化
-----

使用 ``git init`` 可在目前的目錄下初始化一個 git repository。

若需要建立一個沒有工作目錄 (working directory) 的 repository,可以使用 ``git init --bare`` 來初始化,所建立的 repository 僅有 git 容器內容,通常用於 repository server。

將檔案加入追蹤
------------
在工作目錄 (working directory) 下,git 不會自動追蹤新建立的檔案,必須透過 ``git add`` 指令先將檔案納入容器索引 (index) 才能 commit 進版本庫中。


在一個新的版本庫中新增一個檔案,並使用 ``git status`` 查看版本庫狀態。Git 會提示發現新的檔案,但是不會自動列入追蹤。

.. code-block:: prettyprint

    $ echo "Hellow World" > new.txt
    $ git status
    # On branch master
    #
    # Initial commit
    #
    # Untracked files:
    #   (use "git add <file>..." to include in what will be committed)
    #
    #       new.txt
    nothing added to commit but untracked files present (use "git add" to track)
---------------------------

使用 ``git add`` 將檔案列入追蹤後,git 會顯示檔案預計在下一次提交時送出。

.. code-block:: prettyprint

    $ git status
    # On branch master
    #
    # Initial commit
    #
    # Changes to be committed:
    #   (use "git rm --cached <file>..." to unstage)
    #
    #       new file:   new.txt
    #
--------------------------


``git add`` 指令後方除了檔名之外,也可以使用目錄名稱或是萬用字元當作輸入:

- ``git add .``:將目前所在目錄下的所有檔案 (包含子目錄) 加入追蹤。
- ``git add *.c``:將所有以「.c」結尾的檔案 (不包含子目錄) 加入追蹤。
- ``git add -p``:將目前所在目錄下的所有檔案的更改之處逐一顯示出來,並讓使用者確認是否要加入追蹤。





commit
------

``git commit``

接著撰寫 log

或 ``git commit -m 'log log log.....'``

查看目前狀態
----------

``git status``

會列出目前尚未追蹤、有改變、移除或更名等檔案的狀態,同時會有提示告訴你,

該下什麼指令來作到什麼事。

像是status這類常用的指令,可以善用熱建設定。``git config --global alias.st status``,這個熱建設定指令,

往後藉由輸入``git st``來達到與``git status``相同的效果。

查看 log
--------

``git log`` 可以加上參數增加其他資訊

如

``git log --graph`` 為 branch 畫圖

``git log --all`` 列出所有 branch 等

log 中通常會包含 commit 編號、作者、日期和 log 內容等紀錄。

若暫不需詳細資訊,可以使用 ``--oneline`` 將每個 commit log 縮短至一行。

參考資料
=======

http://git-scm.com/doc

http://blog.longwin.com.tw/2009/05/git-learn-initial-command-2009/