###### tags: `高中生也能懂的Git基本教學` `程式` `教學` 初次上路 === 一開始的話可以建立一個專門存放專案資料夾 以我來說我是在`D:\`建立`git-repos`的資料夾 這樣以後的專案就可以放在裡面 也方便管理 建立專案 --- 首先新增一個資料夾 以此範例來說資料夾名稱為`example` 因為這是新的資料夾 並不會被**git**納入版本管理 畢竟不可能安裝完**git**後 整個Windows都版本管理 資料量太龐大了 所以要先指定從哪裡開始版本管理 在命令列輸入 ``` git init ``` 這邊的`init`指的是**Initialize** 中文為 **初始化** 點進資料夾裡面以後 右鍵開啟`git bash`  接著我們就可以試著建立檔案看看 在剛剛開啟的命令列輸入 ``` touch test1.txt ``` 回到資料夾後發現`test1.txt`這個檔案已經被建立了 這是為什麼呢? 在**Linux**系統裡 `touch`這個指令指的是==更新檔案的時間戳記== 因為有更新時間戳記的特性 所以可以拿來新增檔案 再回到命令列輸入 ``` git status ``` 會看到系統回傳 ``` On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) test1.txt nothing added to commit but untracked files present (use "git add" to track) ``` 我們來看看這是什麼意思 第一句的`On branch master` 在開發軟體時 可能同時會有多人在開發同一功能或修復錯誤 也可能會有多個發佈版本的存在 並且需要針對每個版本進行維護 為了能支援==同時進行數個功能的增加或版本控制== Git具備了branch(分支)的功能 在數據庫進行最初的提交後 Git會建立一個名為master的分支 之後的提交在==切換分支之前都會增加在master分支裡== 第二句`No commits yet` 還沒提交檔案上去 下面三行的 ``` Untracked files: (use "git add <file>..." to include in what will be committed) test1.txt ``` 顯示了`test1.txt`沒有被追蹤 要用指令`git add <檔案名稱>`新增至要被提交的狀態 那我們就跟著系統的指示去做 到命令列輸入 ``` git add test1.txt ``` 此時的檔案已經被加到待提交的區域了 我們可以透過`git status`來看 ``` On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: test1.txt ``` 在提交之前還有東西要設定 不然的話有些人無法提交 因為你沒有Author的名稱以及email 我們可以透過 ``` git config --global user.name <名字> git config --global user.email <信箱> ``` 來更改 最後再用 ``` git config --global --list ``` 來看看是否更改成功 那我們要如何提交呢? 要利用`git commit`這個指令 但是我們輸入指令後跳出來的是vim編輯器 ``` # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # On branch master # # Initial commit # # Changes to be committed: # new file: test1.txt # ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ D:/git-repos/example/.git/COMMIT_EDITMSG [unix] (14:33 09/08/2021) 4,1 All "D:/git-repos/example/.git/COMMIT_EDITMSG" [unix] 11L, 231B ``` 第一次看到vim編輯器的我也嚇到 逢除了亂按真的不知道能幹嘛 不過沒關係 我們可以用`git commit -m "敘述"`直接提交就好了 ``` [master (root-commit) f622042] test commit 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test1.txt ``` 這時試試看`git log`指令 這個指令會把所有的commit列上來 ``` commit f622042314368e1cf03814d2e9ca0691aabaeb05 (HEAD -> master) Author: wayne <wayne71112@gmail.com> Date: Mon Aug 9 14:38:33 2021 +0800 test commit ``` 你就會看到之前提交的紀錄 上傳到Github --- 沒有git hub帳號的快去辦 這邊就不教了 先打開自己的github 點左上角的`+` `new respository`新增一個respository  輸入完名稱後  點`create respository`就會有一個新的repository 然後你要把這個repository連接到本地端 這樣才可以把東西更新(push)上去 那要怎麼做呢?  其實就是複製上面的指令到命令列 ``` git remote add origin https://github.com/yuuto-0226/example.git ``` 指令的意思就是把網址加到你的remote(遠端)然後名稱叫做origin 所以這個origin是可以改的喔 接著我們就可以把提交的東西push上去了 回到命令列 ``` git push -u origin master ``` 這串的意思是你會把你的Master branch(分支) 推到orign上面也就是github上面 然後這邊的-u是把預設設定成origin master 那我們就來試看看 ``` touch test2.txt git add test2.txt git commit -m "test2 commit" ``` 這個時候我們就不用再打`git push origin master` 直接`git push`就可以推上去了 然後我們把github重新整理 就會發現東西都push上去了  從github上載入 --- 那假如你需要在另一台電腦上開發 你可以怎麼做?  複製clone的網址後 打開放專案的那個資料夾 不用在新增一個 命令列輸入 ``` git clone <網址> ``` 它就會把整個repository下載下來
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up