---
# System prepended metadata

title: 2022軟體培訓-github教學
tags: [教學]

---

<style>
.red {
  color: red;
}
</style>

# 2022軟體培訓-github教學
###### tags: `教學`
writer: 林哲丞
email: snoopy195011@gmail.com

## What is git?
Git 是一種版本控制系統
![](https://i.imgur.com/sBCd40m.png)

如圖所示，隨著時間的變化，一開始這個 resume 目錄裡只有 3 個檔案，過兩天增加到 5 個。不久之後，其中的 2 個被修改了，過了三個月後又增加到 7 個，最後又刪掉了 1 個，變成 6 個。這每一個「resume 目錄的狀態變化」，不管是新增或刪除檔案，亦或是修改檔案內容，都稱之為一個「版本」，例如上圖圖例的版本 1 ~ 5。而所謂的「版本控制系統」，就是指會幫你記錄這些所有的狀態變化，並且可以像搭乘時光機一樣，隨時切換到過去某個「版本」時候的狀態。

簡單的說，Git 就像玩遊戲的時候可以儲存進度一樣。舉例來說，為了避免打頭目打輸了而損失裝備，又或是打倒頭目卻沒有掉落期望的珍貴裝備，你也許在每次要去打頭目之前之前記錄一下，在發生狀況的時候可以載入舊進度，再來挑戰一次。
## git vs github
事實上 Git 是一款版本控制軟體，而 GitHub 是一個商業網站，GitHub 的本體是一個 Git 伺服器，但這個網站上的應用程式讓大家可以透過 Web 介面來操作一些原本需要複雜的 Git 指令才能做到的事。
## initial setting
1. 產生ssh key $ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
2. 執行完上層指令後會有一些要輸入的東西，直接狂按enter
3. 設定金鑰代理 $ eval               
$(ssh-agent -s)
4. $ ssh-add ~/.ssh/id_rsa
5. 此時~/底下有.ssh資料夾，將資料夾內的id_rsa.pub的內容複製下來
![](https://i.imgur.com/661XuPR.png)
6. 到 Github > Settings > SSH and GPG keys 的設定頁面，選擇 New SSH Key將複製下來的public key貼上去![](https://i.imgur.com/i8Ushnc.png)
7. 設定使用者資訊讓git知道你是誰 $git config --global user.name "your_name"
8. $git config --global user.email "your_email"
9. 檢查name和email是否正確 $git config --list
## how to use github (Ⅰ)
the following steps is for the situation that you want to copy your own project from your github and update your github after you make some different in your local machine 
1. go to https://github.com/ and sign up with nycu.edu.tw( because of NYCU, we can set our repo with private)
2. create a new repository
3. click commits and you can view every change you made
![](https://i.imgur.com/yu4cKrb.png)

4. <span class="red">$ git clone https://........</span>
![](https://i.imgur.com/5j0FZhA.png)

5. complete your code 
6. after code finished, use <span class="red">$ git status</span> to check the files tracking status.(if occur some errors, use<font color="#f00">$ ls -la</font> to check if your file have .git file)
7. <span class="red">$ git add "name.cpp" ( or git add .)</span>
git add . can add all files, and the former command can add only one file 

8. git commit -m "write some message about what you have changed"

9. <span class="red">$ git push origin main</span> or <span class="red">git push -u origin main</span>(From now on, you can type only <span class="red">$git push</span>, rather than   <span class="red">git push origin main</span>)
10. enter your name and password(token)
## how to use github (Ⅱ)
the following steps is for the situation that you want to create a new project in your local machine and store this project at your own github 
1. craete your project in your local folder
2. complete your code
3. <span class="red">$git init</span>


4. <span class="red">$git status</span>
5. <span class="red">$git add .</span>
6. <span class="red">$git commit -m "some message"</span>
7. create a new repo at github
8. <span class="red">$git remote add origin https:/......</span>
(you can not directly type $git push origin master, since git do not know where to push your code )
![](https://i.imgur.com/0BiGMSI.png)
9. <span class="red">$git push origin master</span>
10. enter your name and password(token)

## git clone vs git pull
![](https://i.imgur.com/wdHBw3l.png)

## how to use github (Ⅲ)
the following steps is for the situation that you are now walking with teammates, and you want to make some revises of your team's projet

1. <span class="red">$git pull <remote> </span>
2. write some codes
3. <span class="red">$git add .</span>
4. <span class="red">$git commit -m "some message"</span>
5. <span class="red">$git remote add origin http://......</span>
(you can not directly type $git push origin master, since git do not know where to push your code, so you have to point out where to push while you push first time )
6. <span class="red">$git push origin master</span>
7. enter your name and password(token)

## git 架構
![](https://i.imgur.com/5IUDnXr.png)

## Reference
 git介紹: https://gitbook.tw/chapters/introduction/what-is-git
 github教學影片:https://www.youtube.com/watchv=RGOj5yH7evk&ab_channel=freeCodeCamp.org
 git pull vs git clone  https://stackoverflow.com/questions/3620633/what-is-the-difference-between-pull-and-clone-in-git

