# Q&A/Git :::info :book: **學習Git基本知識** - [Learn Git Branching](https://learngitbranching.js.org/?locale=zh_TW) - [連猴子都能懂Git入門](https://backlog.com/git-tutorial/tw/reference/basic.html) - [Git-tutoria](https://zlargon.gitbooks.io/git-tutorial/content/) ::: {%hackmd 1lpg7PuqRoaui6XyJcrylw %} ## 申請本練習GitLab Repository的Access權限 1. 到[auto-test-practice](http://mhqnetgitlab.moxa.com/SimonWX_Zhao/auto-test-practice) 2. 點「Request Access」,然後等回復。 ![](https://i.imgur.com/Z3oC2KP.jpg) ## commit code 1. 列出有更動的檔案 ```bash= git status ``` ![](https://i.imgur.com/VkCjlEX.png) 2. 加入特定要commit的檔案 ```bash=+ git add cellular.robot ``` 如果不指定,全部加入就用`.` ```bash=+ git add . ``` 3. 再次列出有更動的檔案,確認檔案已在「Changes to be commit」 ```bash=+ git status ``` ![](https://i.imgur.com/aWpuxfm.png) 4. 執行commit並加上描述該commit的訊息 ```bash=+ git commit -m "<描述該commit的資訊>" ``` ![](https://i.imgur.com/dowacqD.png) 5. 檢查commit ```bash=+ git log --graph --all --oneline ``` ![](https://i.imgur.com/5l3Qj0M.png) ## 暫存現在的更動 1. 暫存未commit的內容 ```bash= git stash ``` 2. 列出暫存內容 ```bash=+ git stash list ``` 3. 取出暫存 ```bash=+ git stash pop ``` ## 跟別的commit做比較 :::info :link: [git-diff完整用法](https://git-scm.com/docs/git-diff) ::: - 將staged(Changes to be commited)的內容與特定的commit比較。 ```bash git diff --cached <比較對象:commit hash/tag> ``` 範例: 將staged(Changes to be commited)的內容與標有`practice1-ans-r`的commit比較。 ```bash git diff --cached practice1-ans-r ``` - 將現在所在的commit內容與特定的commit比較。 ```bash git diff HEAD <比較對象:commit hash/tag> ``` 範例: 將現在所在的commit內容與標有`practice1-ans-c`的commit比較。 ```bash git diff HEAD practice1-ans-c ``` :::info :bulb: 沒顯示內容代表完全一樣! ::: ## 如何做git認證? - 如果是用remote repo的url是http用公司帳密密碼認證。 ![](https://i.imgur.com/u0lf2Ba.png) - [如果是用remote repo的url是ssh,要設SSH Key](https://docs.gitlab.com/ee/ssh/) ## 獲認證改了/認證設定錯誤/還沒設定認證,卻出顯示認證失敗/? 1. 重設git credential ![](https://i.imgur.com/0w1c9X5.png) ```bash= git config --system --unset credential.helper ``` ```bash= git config --global --unset credential.helper ``` * 若打了上述兩行指令都沒用的話 (1) 按win + R : ```bash= control /name Microsoft.CredentialManager ``` ![](https://i.imgur.com/H1rpkXj.png) (2) 重新設定gitlab帳號密碼,或是直接移除,git clone的時候再輸入一次就好 ![](https://i.imgur.com/gYrDcKE.png) ## 設定名字和email ```bash= git config --global user.email "Name@moxa.com" git config --global user.name "Your Name" ``` 建議跟你GitLab的一致,特別是email留意是「_」或「.」。 ![](https://i.imgur.com/PAg7k6L.png) ## 沒有Remote上的tag 1. 再載一次 ```bash git fetch [--tags] ``` 2. 檢查有沒有更新 ```bash git tag ``` ## 檢查現在所指的位置 ```bash git log --graph --all --oneline ``` ![](https://i.imgur.com/kXOxbys.png) ## 忘了先`git checkout -b <new-branch-name>` - 正常情況 我git commit -m " \<message> " 的message寫my_practice ![](https://i.imgur.com/m9DzsDG.png) - 在還沒有 git checkout -b 之前就 git commit 的話會長這樣 ![](https://i.imgur.com/QZpC0hp.png) - 直接在該commit做`git checkout -b <new-branch-name>`就好 - 承上,萬一還沒做branch就checkout到其他commit了 ![](https://i.imgur.com/MEvxJXX.png) - 可以 `git branch <new-branch-name> 07262e8` - 或是checkout回去再新增branch ```bash= git checkout 07262e8 git checkout -b <new-branch-name> ``` ## 有變動還沒commit沒辦法checkout到別的commit? - 先[做commit](#commit-code),之後若要繼續修改就用[`git commit --amend`](#修改最近的一個commit)。 - 先[暫存現在的更動](#暫存現在的更動),等回來再取出詹存繼續修改。 ## 修改最近的一個commit 1. 先做修改 2. commit時加上`--amend`,這樣修改內容就會和最近的一個commit合併 ```bash= git commit --amend ``` ## 有錯誤說「not a git repository」 ![](https://i.imgur.com/i7LIKov.png) ## 沒辦法開啟NBG GitLab 1. 改成用[http://mhqnetgitlab.moxa.com/](http://mhqnetgitlab.moxa.com/)。