--- tags: 109-1, lecture --- # Git Basic https://meet.google.com/zze-scaa-tyn ## What is git :::info ❓ 工程師究竟如何交流程式碼? ::: ### Git is a distributed version control tool. - 記錄各版本的差異 - 切換版本 - 利用分支同時開發不同功能 ## What is github Github is a git server. ## Register a github account https://github.com/ ## Install git CLI https://git-scm.com/book/en/v2/Getting-Started-Installing-Git Windows: [git bash](https://gitforwindows.org/) ## Create a repository https://docs.github.com/en/free-pro-team@latest/github/getting-started-with-github/create-a-repo ## Clone a repository 照著下面點複製repo網址  打開 terminal ,打以下指令 ``` git clone 剛才複製的repo網址 ``` (在terminal按ctrl+v可以貼上網址) ## config ```bash= git config --global user.email 你的email git config --global user.name 你的名字 git config --list # 列出所有 Git 在目前位置能找到的設定值 git config user.name # 列出所有 Git 的特定設定值(在此為 user.name ) ``` ## Git basic concept  ## Basic commands 以下只先教單人使用時比較會用到的command | Command | Description | | -------- | --------------------------------------------------------------- | | status | 看狀態 | | add | 把變更加到 staging area(提交區) | | commit | 把 staging area的變更寫成一筆紀錄到local repo | | push | 把 local repo 的紀錄寫到 remote repo(寫到git server) | | log | Show 出 commit log | | diff | Show 出離上次 commit 的差異 | | checkout | 對檔案使用的話可以恢復檔案 "Changes not staged for commit" 的變更 | | reset | 把 staging area 的東西變成 not staged, 或是倒回之前的 commit | ## Practice !!! 1. 打開terminal,cd進剛才clone下來的那個repo 2. 看一下狀態 ``` git status ``` * Changes to be committed(將要提交的檔案) * Changes not staged for commit(被更動但尚未要提交的檔案) * Untracked files(未被追蹤的檔案) 3. 新增一個檔案,看一下狀態 ``` echo "hello" > test.txt # 也可以用你喜歡的文字編輯器編輯然後存檔 git status ```  4. 加到staging area ``` git add -A # -A 代表全部add,也可以用git add test.txt一個一個慢慢加 git status ```  5. commit ``` git commit -m "Add test.txt" # -m 後面接commit message,寫你做了什麼 git status git log ``` 6. push ``` git push ``` 之後去github上看看吧 7. 修改檔案 ``` echo "hi" > test.txt # 把hello改成hi git status git diff ```  8. 回復Changes not staged的變更 ``` git checkout test.txt git status cat test.txt # 把test.txt裡的內容show出來,也可以用文字編輯器開 ``` 9. 嘗試各種情況下的倒回方法 ``` echo "hi" > test.txt # 把hello改成hi git add test.txt git status git diff git diff --cached git reset git status git add test.txt git reset --hard ``` 10. 分支管理 * 查看 branch ```bash= git branch git branch --all ``` * 建立 branch ```bash= git branch <new branch name> git branch newBranch # 建立名為 "newBranch" 的分支 ``` * 切換 branch ```bash= git checkout <target branch name> git checkout newBranch ``` * 合併 branch ```bash= git checkout master git merge bugFix git log ``` 11. conflict 處理 ... ## Note 基本使用 1. 基本流程就 clone => add => commit => push 1. 要常用 status ## Learn more https://zlargon.gitbooks.io/git-tutorial/content/ https://www.tutorialspoint.com/git/index.htm
×
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