# Git ![](https://i.imgur.com/j9TgY5S.png) ## Command Line Interface 我個人較常使用`Windows` 的`cmd`和 `git bash`操作,亦可使用`Windows` 的`powershell`或其他作業系統的`命令列介面(CLI)`。 ## 📌Git commands ```cmd= git --version ``` ```cmd= git status ``` ```cmd= git merge git pull git push ``` ## 📌Ssh key ### Generate a private and a public key ```cmd= ssh-keygen -t rsa -b 4096 -C "XXX@gmail.com" #Ur email associate with ur Github Account ``` ### Echo the contents of ur file ``` cmd= type key.pub #Show the content of ur public ssh key ``` When u got ur ssh key, copy it to ur Github account (Settings -> SSh & GPG keys). ## 📌Remote Connection ```cmd= git remote add origin git@URL ``` Connect ur local to ur Github account ```cmd= git commit -m "Update Bla Bla Bla" ``` Commit message to ur Github ## 📌Git push ### Set default to (upstream) ```cmd= git push -u origin master ``` When u enter the cmd line above, u can enter this line in any other situations afterwards ```cmd= git push ``` ## 📌Git Pull ```cmd= git pull origin master ``` When there are new updates at ur remote repository, u can pull them to the local ## 📌Git Branching ![center](https://i.imgur.com/DFdZC6b.png) `Master branch` | `Feature branch` | `Merge` ```cmd= git branch ``` Check whether the branch exists and ur current branch ```cmd= git checkout -b feature ``` Create a new branch and switch to it ```cmd= git checkout master ``` Switch branches (switch to master) ```cmd= git diff feature ``` See the differences ```cmd= git push -u origin feature ``` Push feature to Github ```cmd= git pull ``` ```cmd= git branch -d feature ``` Delete branch ```cmd= git reset Filename ``` Unstage ```cmd= git reset HEAD~1 ``` * Reset to ur last commit * `HEAD` : tells Git to point toward ur last commit * `~1` : 1 step before your last commit ## 📌Update Forked Repo 先切換到 local repo 底下 用 `git remote -v` 查看遠端來源 ```cmd= git remote add upstream git@URL ``` 將已經更新/官方的repo放到遠端倉庫 ```cmd= git checkout master git pull upstream master ``` 切換到`main branch` 然後將上一部的`upstream`拉下來 ```cmd= git push origin master ``` 從`local`端`push`回自己的`Github` ```cmd= git push -f origin main ``` 強制`push` ## 📌Log Records ```cmd= git log ``` Show log records in chronological order. ## 📌References 1. https://www.youtube.com/watch?v=RGOj5yH7evk&list=PLc_rdjKAluzdy4YVqZU2D8XxAPGcbK9QT&index=10&t=1380s 2. https://digitalvarys.com/git-branch-and-its-operations/