[程式導師] 第一周:Command Line & Git

Henry Feng
6 min readJun 29, 2020

--

總算在開始上課的兩周後,正式開展這個系列的筆記寫作。6/12號開始跟著Huli的程式導師計畫,是一個為期半年的進修之旅,也希望透過Medium的書寫,介於鬆散與嚴肅之間,把筆記整理出來。

開始兩周後也發現要重拾每日學習的習慣還是挺困難的,但目前在進度上起碼還是跟著學習歷程的規劃。那就二話不說來記錄一下第一周學到了什麼!

第一周基本上學習兩大基礎課程:Command Line和Git,之前讀研究所時,這些東西也是在Big Data課程之前囫圇吞棗的學完,這次算是比較有系統性的梳理這兩個領域的相關知識。

Photo by Yancy Min on Unsplash

Command Line

Command Line是相對於GUI圖形化介面而言的電腦操作邏輯,是透過純文字的方式操作電腦。

以下介紹和「資料夾」有關的Command Line

pwd: print working directory > 印出所在資料夾
ls: list > 顯示該資料夾裡面所有的檔案、資料夾
ls -al: list all > 顯示資料夾裡面所有檔案和資料夾的資訊
cd: change directory > 轉換資料夾 ex cd desktop, 即跳到desktop的位址
cd..: change to upper level directory
cd~: back to main directory
clear: clear all record in command line tool
rmdir: remove directory
mkdir: create directory

以下介紹和「檔案」有關的command Line

touch: 1)change file name or 2)if the file doesn't exist, create one
rm: remove file
mv: move the file or change the file name > ex mv file_name folder_name
cp: copy file > ex. cp file1 file2
cat: check the content of the file
grep: find certain string in one file > ex. grep hello file1

還有一些其他操作的語言

">" : 將某個輸出的結果導入另外的檔案example:
echo '123' > 123.txt 將字串123存入123.txt的檔案裏面
echo '456' > 123.txt 將字串456存入123.txt的檔案裏面,而原本的123會被抹掉
echo '456' >> 123.txt 將字串456貼入123的後面,檔案裏面的字串為'123456'
PIPE > "|" 將眾多的command line指令連在一起

GIT

Git為版本控制的工具。版本控制是在開發中很重要的概念,尤其是多人協作的專案中。可以使用資料夾的邏輯思索版本控制。版本控制就是將所有要進行控制的檔案丟到一個第一版的資料夾裡面,然後每次要更動一個檔案,就是在複製整個資料的第二版、第三版中進行檔案的改動與存儲。

資料夾以外的檔案則可以當作母版,不加入版本控制的流程裡面。而每個版本號都是亂數決定的,用於多人協作避免重複。

以下整理一些基本的Git語法

git init: 對於一個資料夾開始版本控制的步驟
git -rf .git : remove git file
git status: check the git status
git add + file_name: decide which file to be added in the version control
git add . : add all files into version control
git rm --cached + file_name: remove the file from version control
git commit : create new version
git commit -m 'string': add the note to this commit
git commit -am 'string': add note and commit all file
git log: get the version name of each version control
git log --oneline: get the short description of each ver. control
git checkout + version_name: go to specific version (git checkout master)
git diff: check the difference between versions

再來我們來關注一下和Branch相關的指令。Branch是用於多人協作,一個專案通常會有一個master branch,而開發者會自行開出幾個branch,進行各自開發,以免master branch的功能被影響,開發完獨立的branch之後,再將branch匯流至master branch。

git branch -v : make sure how many branches in the version
git branch branch_name: create new branch
git branch -d branch_name: delete the branch
git checkout branch_name: get the different branch to work with
git merge branch_name: merge the branch

Github — 放各種Git版本的匯流處

一些常見的指令如下,讓本機端的Depository可以和遠端的Github Repository互動。

1. git pull origin master: 將遠端的master拉到本機
2. git push origin branch_name: 將本機端的branch推到Github
3. git clone SSH address: 可以複製有權限允許的Github repository到本機端

以上就是第一周教的概要,第一周也是為了讓以後交作業比較方便,敬請期待第二周的筆記!

--

--

Henry Feng
Henry Feng

Written by Henry Feng

Sr. Data Scientist | UMN MSBA | Medium List: https://pse.is/SGEXZ | 諮詢服務: https://tinyurl.com/3h3uhmk7 | Podcast: 商業分析眨眨眼

No responses yet