# git 應用指令紀錄 ###### tags: `git` Copyright 2021, [月下麒麟 YMont](https://hackmd.io/@YMont/note-catalog) --- ## 名詞解釋 **定義** * master:本地分支名 * origin master:origin 表示遠端,master 表示分支名,接在 origin 之後表示是遠端分支名 * origin/master:遠端分支在本地的拷貝,因此稱為本地分支 **範例** * git fetch origin master:取得遠端分支 master 的資料 * git merge origin/master:合併本地分支 origin/master 的程式碼 * git push origin master:將本地提交紀錄推至遠端分支 master ## 正常流程 **git clone** ``` git clone https://... ``` <br> **git init** ```git init Initialized empty Git repository in D:/1_project/4.CM/.git/ ``` <br> **git add** 修改檔案或是資料夾,就可以把變更紀錄存起來 ``` git add . ``` <br> **git status** ``` git status ``` 有被git add抓到的變更就會出現**綠色**字(==Changes to be committed==) ![](https://i.imgur.com/F9XfvOb.png) 沒有被git add抓到的,會出現**紅色**字(==Changes not staged for committed==) ![](https://i.imgur.com/jkpT31I.png) <br> **git commit** ``` git commit -m "your describe" ``` <br> **git remote** ``` git remote add origin http://192.168.xxx.xxx:xxxx/project/main.git ``` <br> **git push** ``` git push origin master ``` 成功的提示訊息 如下 ``` Enumerating objects: 11, done. Counting objects: 100% (11/11), done. Delta compression using up to 8 threads Compressing objects: 100% (7/7), done. Writing objects: 100% (10/10), 54.87 KiB | 13.72 MiB/s, done. Total 10 (delta 0), reused 4 (delta 0), pack-reused 0 remote: . Processing 1 references remote: Processed 1 references in total To http://192.168.xxx.xxx:xxxx/project/main.git 6d144e7..503c0f0 master -> master ``` <br> --- ## 除錯解法 **git rm remote** 錯誤描述 ``` git remote add origin http://192.168.xxx.xxx:xxxx/project/main.git error: remote origin already exists. ``` 解決方法 ``` git remote rm origin ``` <br> **rebase** ``` git pull --rebase or git pull --rebase origin master ``` <br> **rm .git** ``` rm -rf .git ``` <br> reference:[How to upload a project to Github](https://stackoverflow.com/questions/12799719/how-to-upload-a-project-to-github) --- --- ## git pull 與 git pull --rebase差異 ==git pull== = ==git fetch== + ==git merge== against tracking upstream branch ==git pull --rebase== = ==git fetch== + ==git rebase== against tracking upstream branch if you want to know how **git merge** and **git rebase** differ, following this. * ![](https://i.imgur.com/Yq2y67J.png) * ![](https://i.imgur.com/nW3fu6b.png) * ![](https://i.imgur.com/5PXs9oi.png) * ![](https://i.imgur.com/WtAp5jF.png) Reference:[Git: 四種將分支與主線同步的方法](https://cythilya.github.io/2018/06/19/git-merge-branch-into-master/) --- --- ## 重新pull & 再次commit ### Debug1 新的一天要git pull,從遠端到本地,但是一直失敗 **Command** `git pull origin master` **Error** `fatal: 'origin' does not appear to be a git repository` `fatal: Could not read from remote repository.` **Debug** 表示沒有連結到遠端的repository `git remote add origin http://192.168. ...` ### Debug2 因本地與遠端的內容不同,需要合併 **Command** `git pull --rebase` **Error** `error: cannot pull with rebase: You have unstaged changes.` `error: please commit or stash them.` **Debug** `git add .` ### Debug3 **Command** 接著再試一次 >> git pull --rebase `git pull --rebase` **Error** `error: cannot pull with rebase: Your index contains uncommitted changes.` `error: please commit or stash them.` **Debug** 所以要commit才可以 `git commit -m "descriptation` reference:[git---如何解决git pull之后报错](https://blog.csdn.net/Wbiokr/article/details/75270610) reference:[How to merge remote master to local branch](https://stackoverflow.com/questions/7200614/how-to-merge-remote-master-to-local-branch)