Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Git for Data Science

Alex
January 07, 2019

Introduction to Git for Data Science

Alex

January 07, 2019
Tweet

More Decks by Alex

Other Decks in Technology

Transcript

  1. Why Git for Data Science • 將資料分析方法的程式碼放在公開的 GitHub 遠端儲存庫。 •

    製作統計分析或視覺化工具,分享給其他人 使用。 • 替文本和統計分析模型做版本控制。 • 允許多人協作與追蹤。
  2. Git Cloud Storage • GitHub - 全球最大的工程師交友平台。 • BitBucket -

    由 Atlassian 公司開發。 • GitLab - 開源、多為私人公司使用。 • Gitea - 輕量,基於 GO 語言。
  3. Git GUI Tools • SourceTree - 操作簡單功能強大,由 Atlassian 公司 開發。

    • GitKraken - 重視 UI,軟體稍微笨重一點,由 Axosoft 公司開發。 • GitHub Desktop - 由 GitHub 開發,適合新手使用。 • Tower - 多為 MacOS 用戶使用。
  4. A Basic Workflow Master Dev Test Time • Create a

    repository • Observe your repository • Working with Branch • Saving Changes • Syncing • Making a Pull Request (PR) branching merging merging commit commit
  5. How to install Git on Windows, Linux and MacOS Ubuntu

    / Debian Direct Download from Git Website Homebrew $ brew install git $ apt-get install git $ pacman -Sy git $ yum install git Arch Linux Fedora / CentOS
  6. Other Basic Settings $ git config --global core.editor emacs #

    更改編輯器 $ git config --list # 列出所有設定 $ vim ~/.gitconfig # 編輯設定
  7. Saving Changes $ git add [file] # 將單一個檔案加進暫存區 $ git

    add . # 將全部檔案加進暫存區 $ git commit # 提交檔案 (跳出文字編輯視窗) $ git commit -m [commit message] # 提交檔案
  8. Observe Repository $ git status # 顯示檔案當前的狀態 $ git diff

    [hash_id1] [hash_id2] # 顯示兩個 commit 改了什麼 $ git log --oneline # 顯示 commit 歷史紀錄 $ git reflog # 顯示所有的操作紀錄 $ git blame [file] # 顯示是誰修改了紀錄 (友情破壞者)
  9. Working with Branches $ git branch test # 建立名為 test

    的新分支 $ git checkout test # 建立並且切換至名為 test 的新分支 $ git merge test # 合併名為 test 的分支 $ git rebase test # 合併名為 test 的分支 (移花接木合併法)
  10. SSH Key 免密碼登入 $ ssh-keygen # 產生 SSH Key $

    cd ~/.ssh # SSH Key 位在根目錄的資料夾內 $ ls # 檢查目錄底下的檔案 # 會出現三個檔案:id_dsa id_dsa.pub known_hosts
  11. Syncing with Remote Repository $ git clone [repo url] #

    複製已經存在的 repository $ git remote add [alias] [remote repo url] # 增加遠端倉庫 $ git fetch # 取得遠端倉庫的內容,但不合併 $ git pull # 取得並且合併遠端倉庫的 內容 $ git push [alias] [branch] # 推送本地分支至遠端倉庫
  12. Merge Conflicts Discussion & Commit Again Solve Merge Conficts $

    git merge [branch] $ cat merge.txt # 發生 conflict 的文 件 <<<<<<< HEAD Content A ======= Content B >>>>>>> branch 討論發生衝突原因,並決定採取誰的 更動,修改過後重新 commit 一次。
  13. 新增一個 commit 回到過去 $ git revert HEAD # 新增一個 commit

    回復到上一個 commit 的內容 $ git revert HEAD --no-edit # 同上,但不修改 commit 訊息 $ git revert [hash_id] # 新增一個 commit 回復到 [hash_id] 的內容。 $ git revert HEAD~3 # 新增一個 commit 回復到前三次的 commit。 # revert 指令不會修改 commit 的歷史,所以有可能會有衝突發生
  14. 回到過去那次的 commit $ git reset --hard HEAD^ # 使用 hard

    模式取消上一次合併 $ git reset [hash_id] # 取消 [hash_id] 那次的提交 # git reset 共有三種砍掉重練的模式: --mixed (default, 只丟掉暫存區檔案) --soft (檔案皆保存) --hard (把所有檔案丟掉)
  15. 舊 新 • pick: 已存在的修改 • reword: 更改 commit 訊息

    • squash: 合併新舊分支 • drop: 刪除某次的 commit
  16. 其他常用的修改指令 $ git commit --amend # 修改上一個提交訊息 $ git reset

    HEAD [file] # 將檔案移出暫存區 $ git checkout -- [file] # 取消檔案修改 $ git checkout [hash_id] [file] # 將某個檔案回到過去某次的修改。
  17. 增加標籤 $ git tag [tag_name] [hash_id] # 新增 lightweight tag

    (輕量標籤) $ git tag [tag_name] [hash_id] -a -m [message] # 新增 annotated tag (有附註的標籤) $ git show # 觀察標籤
  18. $ git stash # 將手邊目前的進度加入 stash 裡面 $ git stash

    list # 查看目前的 stash $ git stash apply [stash ID] # 回到 stash 前的狀態 Git 儲藏
  19. Summary 基本指令 操作分支 修改過去 $ git add $ git init

    $ git commit $ git remote $ git push $ git clone $ git pull $ git branch $ git checkout $ git merge $ git rebase $ git revert $ git reset $ git rebase 觀察狀態 $ git status $ git diff $ git blame $ git log $ git show $ git reflog 其他指令 $ git tag $ git submodule $ git stash
  20. References • Git Website • Atlassian Git Tutorial • Tower

    Git Tutorial • 30 天精通 Git 版本控管 • 為你自己學 git
  21. Exercise • Git 基本設定 • 將自己的一份專案加入 Git 控管 • 練習

    Branch / Merge / Rebase / Reset 等操作 • 安裝一個 GUI 軟體,觀察 Git Tree 的狀態 • 練習解決 Merge Conflict • 練習提交 Pull Request