Try   HackMD

Git整理筆記

難度:★★進階

以前沒有用過Git這套版本管理軟體。第一次接觸,雖經老師熱心指導,還是非常陌生。現在一邊使用一邊記錄整理,方便自己日後複習,也提供同學參考。

  • 本文講的是Windows下git的操作,不是Linux也非Mac的Git。
  • 隨著使用經驗增長,本文會不斷補充修改。

命令列模式

  • git的操作基本上是命令列模式,在Win的命令列(command line)視窗上輸入指令執行。

  • 記得先cd到欲進版本管理的那個資料夾(目錄)。如:

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • 所有Git的指令都在該資料夾內下達:

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
     
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • 目前已發現的問題:Git log時中文會變成亂碼:

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    在網路上找到的其中一個solution如下:

    git config global i18n.logOutputEncoding big5

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    但是我測過,執行完以上的指令之後,Git log中文依然是亂碼。問題沒有解決。

  • 後來安裝Git for Windows SDK (安裝要很久),在SDK的界面上跑Git log,才沒有亂碼。不過上述的git config 指令還是需要。

    以下是Git SDK的界面,可以看到Git log時中文正常顯示,沒有亂碼。

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    對了,SDK界面預設的字型大小是9號字,實在太小了。看個幾分鐘眼睛就很疲勞,受不了。強烈建議安裝後立即調整字型大小。方法是在SDK視窗按滑鼠右鍵,再選Options:

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    除了上面的git config global i18n.logOutputEncoding big5指令,這個界面本身也有文字編碼系統的選項。我測試的結果是兩者都要設定Big5,才能正常顯示中文,缺一不可。

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • 至於在Windows本身的命令列上Git log中文不變亂碼,網路上也有人提供其他解決方法。大多要修改Git的config檔,將encoding(文字編碼)改為Big5,目前我沒有測試這個部份。另外有人建議採取另外一個途徑:修改Windows的輸出輸入編碼。在命令列上輸入:

    chcp 65001

    我試過,結果是 'It is not working'。希望不是 'It does not work'。

圖形界面

  • TurtoiseGit是Git的圖形化軟體,GUI操作。

  • 以我淺薄的經驗,發現有些地方用起來不是那麼順。可能是對這套軟體還不夠熟練。

  • 安裝後首先修改Settings。請在Windows桌面任何地方按滑鼠右鍵,選TurtoiseGit > Settings:

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    • 修改字型大小(強烈建議)

      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →

    • 改變語言(依個人需要)

      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →

  • TurtoiseGit的基本操作都要進入欲做版本管理的資料夾裡面,按滑鼠右鍵進行。

    • git init(初始化)

      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →

      init之後該資料夾會產生一個隱藏的子資料夾,名叫.git。這當然是Git內部的系統資料夾,不能刪除。

      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →

    • git commit -m “原始版本。”(TurtoiseGit在commit之前好像不必做git add)

      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →

    • git log

      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →

    • git checkout

  • 但有時直接選Switch/Checkout想回復之前的版本,進入之後卻會看不到所有版本。

    這情形看來是在已經有多個版本,但完全沒有還原過或只還原少數版本的時候出現。我對這套軟體還不熟悉,不知道這個現象是特意的設計還是bug,抑或是本人因誤解而操作不當所致。碰到這問題時,目前我找到的解決方法是暫時不要選Switch/Checkout,改選Show Relog(不是Show log),就會看到所有版本,然後對著欲還原的版本按滑鼠右鍵,再選Switch/Checkout to this即可。

  • 也可以從Show log進入,再選某一版本然後再選Switch/Checkout to this。不過Show log有時也不會顯示所有版本喲。以下是Show log的擷圖:

  • 以我目前的摸索,進Show Relog才保證看到所有版本。不過在所有版本都還原過後,就以進Switch/Checkout to this為宜。進Show Relog或Show log會列出一大堆logs,而且有時會有warning(技術原因,無法擷圖):

    A branch with this name already exists. Use a different name or use the "Override branch" option。

    我對Git還不夠熟悉,看到以上的warning只好取消checkout,改從外層的Switch/Checkout做,就不會有warning(原因不詳)。


Happy Gitting!

Git官網
Git Branching學習網

tags: Git, TurtoiseGit, utf-8, Big5, 中文亂碼