<style> h3 { color: #B3A939; } li:first-line { color: #73A939; } </style> ###### tags: `Git` # Git https://gitbook.tw ## 上傳repository https://tw.alphacamp.co/blog/git-github-version-control-guide sum up: ``` cd 某資料夾 git init [echo SomeWhat > file1.txt] 要上完的檔案 git add 檔案名 `or` git add . `or` git add README.md git commit -m "訊息訊息" git remote add origin 這邊打網址git內的repository git push -u origin master //每次都要加這行 github才會出現 ``` - 看狀態 `git status` `git remote -v` `git reflog` 看編輯紀錄 `git reset b174a5a --hard` 可復原到某一步驟 - 看圖 `git log --all --graph --oneline` - 讓head指向編號 `git reset 編號` - 修改 `git commit --amend -m "修改commit message"` - git帳號 查看 `git config --list` vsCode改帳號 `git config --global user.name "用户名"` --- 進入一資料夾(裡面放所有要上傳到git的東西) - 初始化 打 `git init` or `git init Initialized empty Git repository in 路徑` - 看初始化是否成功 -a表 可看見隱藏檔案(檔名有.) `ls -a` 如果跑出 .             ..          .git           file_1.txt 就是成功 - 查看狀態 `git status` 會顯示Untracked files/ tracked files - 提交程式碼: 打 `git add 檔名` 或 `git add .` 上傳全部的意思 此時打git status 就會顯示綠色的檔名,代表已上傳 - 完成它: `git commit -m “一些message”` - 接下來就要去hithub網頁,新增一個repostory 再回來cmd 打 `git remote add origin 這邊打網址https://github.com/cce932/test1.git` 意思是主機位置 origin 和想推到遠端的分支 master (origin和master都是可以改的) // 如果這邊打錯,可以打`git remote rm origin` // 打錯有可能會出現此錯誤 fatal: remote origin already exists - push 才能在github看到 `git push -u origin master` -u 是 --set-upstream 的縮寫, 一旦設定過 upstream,以後就不用每次都要打 origin master。 - 確認是否完成 `git remote -v` 跑出這些就是完成了 origin [your GitHub repo] (fetch) origin [your GitHub repo] (push) ## branch https://kingofamani.gitbooks.io/git-teach/content/chapter_3_branch/chapter_3_branchmerge.html - 查看branch `git branch` - 新增 `git branch 分支名` - 切換(切到哪 資料夾就顯示哪個分支的檔案) `git checkout 分支名` - 新增+切換 `git checkout -b 分支名` - 刪除 如果要刪feature分支,記得跳出此分支 `git checkout master` `git branch -d feature` - 合併 `git merge 分支名` - 之後會跑到vi 按esc從可打字 到不可打字(normal)模式 此模式可選擇存檔(:w)或離開(:q) 按i, a, o 可切換到可打字(insert)模式 ![](https://i.imgur.com/9oQKrcP.png) https://gitbook.tw/chapters/command-line/vim-introduction.html - 要復原的話 `git reset --hard ORIG_HEAD` 這個 ORIG_HEAD 會記錄「危險操作」之前 HEAD 的位置 `git reset HEAD^ --hard` - rebase 剪下分支 類似貼上 https://gitbook.tw/chapters/branch/merge-with-rebase.html `git rebase 分支名` 如果在a rebase b,那就是把a接在b上 - 要復原的話 `git reset --hard ORIG_HEAD` - `git rebase -i 分支名` 之後會跑出一個vim,可以編輯紀錄(做squash, pick等操作)。 用意是把細節的紀錄合併成一個,看起來才不會太亂 https://gitbook.tw/chapters/rewrite-history/merge-multiple-commits-to-one-commit.html - reset 退回(變成)某步驟 https://gitbook.tw/chapters/using-git/reset-commit.html `git reset commit碼` - 先打`git log --oneline`查詢歷史紀錄 ``` 會出現: e12d8ef (HEAD -> master) add database.yml in config folder 85e7e30 add hello 657fce7 add container abb4f43 update index page cef6e40 create index page cc797cd init commit ``` - 還原到某步驟 (以下由舊到新) 1 commit a.js 2 commit b.js 3 commit c.js 如果寫`git reset 2` 會復原c.js而已 b.js的commit紀錄還會在 ``` 如果要undo最後一次commit(e12d8ef) 可以寫 git reset 85e7e30 --hard //hard可更改 或是 git reset e12d8ef^ 或是 git reset HEAD^ //一個^是前一次的意思 若要返回五個以上 就寫~5 或是 git reset master^ ``` 如果不小心reset錯,還是可以救回來(就算是--hard) https://gitbook.tw/chapters/using-git/restore-hard-reset-commit.html - reset的模式 --mixed 是預設 ![](https://i.imgur.com/0M9PrMk.png) 這些不同的模式,將會決定「Commit 拆出來的那些檔案何去何從」 ![](https://i.imgur.com/Eq5NzOP.png) - revert 改動資料 留下紀錄 `git revert HEAD --no-edit` 意思是取消HEAD指向的commit, 會多出一筆「取消」的紀錄。 (--no-edit 表示不編輯commit message) - 差別 - Reset 把目前的狀態設定成某個指定的 Commit 的狀態, 通常**適用於尚未推出去的Commit**。 **不會在github上留下commit的紀錄**, 但是`git reflog`會。 - Rebase 不管是新增、修改、刪除 Commit 都相當方便, 用來整理、編輯**還沒有推出去的Commit**相當方便, 但通常也只適用於尚未推出去的 Commit。 - Revert 新增一個 Commit 來反轉(或說取消)另一個 Commit 的內容, 原本的 Commit 依舊還是會保留在歷史紀錄中。 雖然會因此而增加 Commit 數, 但通常比**較適用於已經推出去的 Commit**, 或是不允許使用 Reset 或 Rebase 之修改歷史紀錄的指令的場合。 - `cherry-pick` 撿(複製)別的分支的commit到現在的分支合併 `git cherry-pick 6a498ec` 若現在在cat分支, 打了上面那行之後,cat就會長出一個新commit。 加上`--no-commit`, `git cherry-pick 6a498ec --no-commit` 表示先剪過來但不合併。 - 把檔案拉下來 `git pull --rebase` 加上 rebase 的意思是,會先 1.把本地 repo. 從上次 pull 之後的變更暫存起來 2. 回復到上次 pull 時的情況 3. 套用遠端的變更 4. 最後再套用剛暫存下來的本地變更。 https://ihower.tw/blog/archives/3843 是**第一次**想要下載到你的電腦裡,請使用**Clone**指令; 如果你已經下載回來了,你只是想要**更新最新的線上版內容**, 請使用 Pull(或 Fetch)指令。 Fetch: https://gitbook.tw/chapters/github/pull-from-github.html # git flow https://gitbook.tw/chapters/gitflow/why-need-git-flow.html ![](https://i.imgur.com/BZcySe9.png) 分支有 master、develop、hotfix、release 以及 feature 這五種分支, 長期存活的只有master、develop。 #### master 主要是用來放穩定、**隨時可上線**的版本, 開發者不會直接 Commit 到這個分支。 #### develop 是所有**開發的基礎分支**, 當要新增功能的時候,所有的 Feature 分支都是從這個分支切出去的。 而 Feature 分支的功能完成後,也都會合併回來這個分支。 #### hotfix 線上產品發生**緊急問題**的時候,會從 Master 分支開一個 Hotfix 分支出來進行修復,Hotfix 分支修復完成之後,會合併回 Master 分支,也同時會合併一份到 Develop 分支。 #### release 當認為 Develop 分支夠成熟了, 就可以把 Develop 分支合併到 Release 分支,在這邊進行算是**上線前的最後測試**。 測試完成後,Release 分支將會同時合併到 Master 以及 Develop 這兩個分支上。 (合併回 Develop 分支的目的,是因為可能在 Release 分支上還會測到並修正一些問題,所以需要跟 Develop 分支同步,免得之後的版本又再度出現同樣的問題。) #### feature 當要開始新增功能的時候, 就會從develop分出feature,開發完成後再merge回develop。