# Git ###### tags: `程式` `開發` `筆記` ## What's git - 一般在軟體開發中又分為 - 中央式系統(例如:Subversion、CVS 等) - 分散式系統(例如:Git、BitKeeper、mercurial 等) - Git 是一個分散式版本控制軟體 - Github 則是一個支援 git 程式碼存取和遠端托管的平台服務 ## git 功能 - Git 可以分為 Local和 Remote兩個環境 - 在 Local 中我們又分為 working directory(工作資料夾)、staging area(暫存區)和 repositories(檔案庫)。  ## 常用語法 ```git= // 下載檔案 git clone 網址 //下載檔案+指定分支 git clone <repo URL> -b <branch name> //加入檔案才會進到git的追蹤範圍(暫存區) git add filename git add . // 加入全部 //沒問題後就可以存到repository git commit git commit -m "版本資訊" //比較現在檔案和上次 commit 之間的差異 git diff //建立與刪除分支 git branch [name] git branch -d [name] // 移動HEAD到branch並查看 git checkout [name] ```  ## 遠端連線 ```git= //本地端專案知道 origin 對應到遠端網址 git remote add origin <remote 網址> //將本地端程式 push 到遠端檔案庫 //-u 是把origin 設成預設值 之後直接git push git push -u origin master //將遠端資料庫更新到本地端 git pull(git frtch + git merge) ``` ### git push -u origin master - 可被拆成 - git push origin master - git checkout master - git branch -u origin/master - 參數 -u 等同於 --set-upstream,設定 upstream 可以使分支開始追蹤指定的遠端分支。 - 只要做過一次 $ git push -u <remote name> <branch name>,並且成功 push 出去 - 本機端的 master 就會被設定去追蹤遠端的 <remote name>/<branch name> 分支。 - 第二次以後要上傳分支時,git push即可 ### git pull - git fetch : 只複製遠端資料庫到本地 - git merge : 只合併本地端資料庫的分支 ### git status(確認狀態) - 可以看出從上一次add到現在有哪些變動  ### git log (歷史紀錄) - 可以看到每一次的commit id,時間以及上傳者 ### git merge: - 將指定分支合併到當前分支  ### git rebase : 整合到同一條分支上 - 將當前分支移动到指定分支上,並生成一新分支  ## 少用語法 ```git= // 標籤 git tag [標籤] [branch] // 視覺化調整HEAD往上數四個節點的順序 git rebase -i HEAD~4 //沒問題後就可以存到repository git commit git commit -m "版本資訊" //比較現在檔案和上次 commit 之間的差異 git diff //建立與刪除分支 git branch [name] git branch -d [name] // 移動HEAD到branch並查看 git checkout [name] ``` ### 參考資料 - 免費練習網站: [Git Branch](https://learngitbranching.js.org/) - 免費教學影片: [六角學院-為什麼要用git?](https://www.youtube.com/watch?v=PNEM7CH3ZAg&list=PLYrA-SsMvTPOZeB6DHvB0ewl3miMf-2tj&index=1)
×
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