git tricks 2.0 === **Author**: **Yuehchuan** Welcome contribution! https://hackmd.io/83xI75i5S_KTvqsRdFobHg?both #### 動詞解釋: * fork:在網路上複製令一份相同的專案 * 發issue: 提出問題 ex: https://github.com/RobotLocomotion/LabelFusion/issues/2 * pull Request: 對自己的fork做了修改要對原本專案提交貢獻, 或你是專案成員要從分支(branch) 合併到主線 (master)需具有merge權限管理員檢視(review)後 ex: https://github.com/ros-planning/moveit/pull/470 #### 名詞解釋: * repository: 專案資料,常簡稱"repo" * remote:遠方的github網站或organization網站 * local:你的筆電 * branch:專案內的分支, 嘗試一些新東西 * LGTM: "Looks Good To Me" * cherry pick: #### 好教材 聖文的git教學 [link](https://www.dropbox.com/s/2v3imjzs2zn028v/git_tutorial.pdf?dl=0) the simple git guide [link](http://rogerdudler.github.io/git-guide/) git cheatsheet [link](https://services.github.com/on-demand/downloads/github-git-cheat-sheet.pdf) git fetch的細緻動作 [link](https://git-scm.com/book/zh-tw/v2/%E4%BD%BF%E7%94%A8-Git-%E5%88%86%E6%94%AF-%E5%88%86%E6%94%AF%E5%92%8C%E5%90%88%E4%BD%B5%E7%9A%84%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95) CS50 git https://rogerdudler.github.io/git-guide/ --- #### git branch sync with master 本地端分支和master更新 ```bash= git checkout master git pull git checkout mobiledevicesupport git merge master ``` --- #### git submodule 你的專案裡有第三方library, 你用clone的,到時候推上github發現它們是空的 法一. 上傳前把那些空專案.git隱藏資料夾都刪掉重新git init 法二. 使用submodule * 下載 ```bash= git submodule update --init --recursive ``` * 移除 https://blog.longwin.com.tw/2015/05/git-submodule-add-remove-2015/ https://gist.github.com/kyleturner/1563153 ```txt= Run git rm --cached <submodule name> Delete the relevant lines from the .gitmodules file. Delete the relevant section from .git/config. Commit Delete the now untracked submodule files. Remove directory .git/modules/<submodule name> ``` ```bash= git submodule deinit <submodule_name> ``` 寫.gitsubmodule ```git= [submodule "Habonbon"] path = habonbon url = https://github.com/duckietown-bunny/habonbon.git ``` from vgod' blog [link](http://blog.vgod.tw/2011/03/19/vimrc/) Synchronizing plugins with git submodules and pathogen [link](http://vimcasts.org/episodes/synchronizing-plugins-with-git-submodules-and-pathogen/) --- #### 如何為 Fork 出來的專案,同步上游的更新 [參考網址](http://fred-zone.blogspot.tw/2015/09/git-fork.html) 首先,加入上游的 repository 並命名為「upstream」: ```bash= git remote add upstream https://github.com/YOUR_USERNAME/YOUR_FORK.git ``` 未來想要更新前,可以使用 fetch 去拉上游的更新回來: ```bash= git fetch upstream ``` 最後再把 upstream 的內容,與現有的正在修改的進行「合併」: ```bash= git merge upstream/master ``` --- git merge branch to master git update master to local branch git merge --- Throw away local commits in git https://stackoverflow.com/questions/5097456/throw-away-local-commits-in-git use gitk tool --- git pull和git fetch 再git merge https://stackoverflow.com/questions/292357/what-is-the-difference-between-git-pull-and-git-fetch/7104747#7104747 git fetch git pull origin/master https://gitbook.tw/chapters/github/pull-from-github.html ---vs.---- git pull --- #### git for dummy ``` git init git add . git commit -m "awesome-commit" git remote add origint <http://....> git push origin ``` 保留電腦端檔案移除remote網路上 ```bash= git rm --cached file1.txt git rm -r --cached <directory> git commit -m "remove file1.txt" git push origin branch_name ``` https://stackoverflow.com/questions/2047465/how-can-i-delete-a-file-from-git-repo --- #### 抓程式碼 git clone <http://...> <命名> git clone http:// habonbon --- #### 換branch ![](https://i.imgur.com/AeAQnXB.png) ```bash= git check out "branch 名稱" ``` ex: `git checkout indigo-dev` 查看在哪個branch上 ```bash= git branch ``` ```bash= git branch <branch name> 或 git checkout -b <branch> ``` 創新的branch再換到上面 --- #### git remote 查看修改push和pull遠端網址 git remote -v git remote set-url <網站別名> <網址> ex: `git remote set-url origin https://github.com/habonbon/habonbon.git` 增加upstream git remote add upstream <網址> --- 更新程式碼 要先pull和遠端同步再push git pull <網站別名> <分支> ex: git pull origin yuehchuan-devel git push <網站別名> <分支> ex: git push origin yuehchuan-devel --- #### 下載remote分支到local ![](https://hackmd.io/_uploads/SkmtIAg6n.png) >git checkout -b eigen remotes/origin/eigen --- #### 寫 .gitignore 不要把大檔案(git LFS)跟執行檔push上去, https://github.com/duckietown-bunny/Software/blob/master/.gitignore --- >>by哲宇 rebase(interactive rebase搭配squash)其實在開發流程挺常需要用到 主要是merge有時候會產生redundant push comment 用rebase能夠有效防止這種情況發生 在code review的時候才不會大!崩潰 􏰀 Cherry pick - pulling specific commits to/from branches git lfs download git lfs linux386 version https://github.com/git-lfs/git-lfs/releases/tag/v1.2.1 $ cd DIRECTORY $ git lfs install $ git lfs track "*.psd" $ git add .gitattributes $ git add file.psd $ git commit -m "Add design file" $ git push origin master --- #### 下載遠端某一分支 git clone -b <branch> <remote_repo> git clone -b minimal_python_package https://github.com/fkromer/examples.git git fetch <remote> <rbranch>:<lbranch> git checkout <lbranch> --- https://github.com/JdeRobot/JdeRobot/wiki/How-To-Contribute --- #### untrack files 取消上傳文件 Untrack已經commit到遠端repo的檔案(通常用在.swp或binary 不小心傳上去) 範例: https://github.com/YuehChuan/resume untrack .resume.tex.swp 步驟: ```bash= get in to the directory git rm -r --cache .resume.tex.swp git commit -m 'untrack .resume.tex.swp' git push ``` 詳細說明: http://www.codeblocq.com/2016/01/Untrack-files-already-added-to-git-repository-based-on-gitignore/ --- #### git 暫存密碼15分鐘不用打密碼 git config --global credential.helper cache --- #### 撤銷git add git reset HEAD . git reset HEAD filename --- #### 知道自己站在哪個分支上 不能站在分支上進行操作 git fetch origin git merge origin/<master> --- #### 流程 本機端推到遠端倉庫 遠端倉庫在下載到另一臺電腦 --- #### 更新遠端新增的更新分支 To update the local list of remote branches: git remote update origin --prune To show all local and remote branches that (local) Git knows about git branch -a git fetch origin cmd-sort:cmd-sort git merge origin/cmd-sort https://stackoverflow.com/questions/36358265/when-does-git-refresh-the-list-of-remote-branches #### 在分之上做了一些修改 沒有和主線同步 突然想新開一個分支 git stash git checkout master 再增加新的分支 #### 問題:要從遠端更新local 分支 fatal: Refusing to fetch into current branch refs/heads/cmd-sort of non-bare repository git fetch --update-head-ok origin cmd-sort:cmd-sort ```bash= remote: Enumerating objects: 32, done. remote: Counting objects: 100% (32/32), done. remote: Compressing objects: 100% (22/22), done. remote: Total 32 (delta 17), reused 22 (delta 10), pack-reused 0 Unpacking objects: 100% (32/32), done. From https://github.com/YuehChuan/DataServerTest 0252ae8..afac84b cmd-sort -> cmd-sort 0252ae8..afac84b cmd-sort -> origin/cmd-sort ``` How to fix ‘Your local changes to the following files will be overwritten by merge’? https://appuals.com/how-to-fix-git-error-your-local-changes-to-the-following-files-will-be-overwritten-by-merge/ #### 從兩個remote更新到通一個新分支 https://www.educative.io/edpresso/the-fatal-refusing-to-merge-unrelated-histories-git-error #### 從遠端拉下特定branch git remote update pds --prune #### fetch certain gitbranch git fetch origin product:product git checkout -b localbranch origin/branch ### push到遠端另外分支 https://devconnected.com/how-to-push-git-branch-to-remote/ git push pds dev:product ### git 站在功能分支 git cherrypick <hash> git cherry-pick --abort ### !!! git merge --no-ff https://stackoverflow.com/questions/9069061/what-is-the-difference-between-git-merge-and-git-merge-no-ff ![](https://i.imgur.com/wsmdI2t.png) #### !!!! git push non fast forward git push origin benchmark:benchmark #### commit風格約定 Git Commit Message 這樣寫會更好,替專案引入規範與範例 https://wadehuanglearning.blogspot.com/2019/05/commit-commit-commit-why-what-commit.html https://stackoverflow.com/questions/7124914/how-to-search-a-git-repository-by-commit-message/7125014#7125014 #### reference http://rogerdudler.github.io/git-guide/files/git_cheat_sheet.pdf 支撑Github的开源技术 [link](http://www.infoq.com/cn/news/2014/03/projects-power-github) git 是相當複雜且強大的工具 這篇文章給予開發團隊6個協同開發的策略, 個人獲益良多: * **Formalize Git conventions for your team** - 基本上就是從 code, commit comments, tagging, branch 都有一定準則依循 * **Merge changes properly** - 每個 member 都在自己的 branch 上開發, 然而難免會修改到共同的檔案, 這時就需要人為介入. * **Rebase your feature branch often** - 經常與持續地將你的private development branch 對主要 branch 作 rebase * **Squash commits before merging** - 開發時可能會有許多個小 commits, 在與主要 branch merge 時, 先將其合併再以單一 or 僅數個 commits 作 merge. * **Use tags** - 對於重要的開發階段, 使用 tags 來保存狀態. 與 branch 不同, tag 是單一狀態的保存, branch 則有依附其的連續修改. * **Make the software executable print the tag** - 透過 git describe 指令產生 tag 資訊並在 building 過程嵌入被編譯的 source code 中, 當發生問題時可以輕鬆得到對應的版本. https://opensource.com/article/20/7/git-best-practices?fbclid=IwAR1xF1kX89bwdhZpsHKKumb-ACCky96vJE46jmwiggFoO2n9a1FY-2axOR8 https://developers.googleblog.com/2020/07/teaching-art-of-great-documentation.html?m=1&fbclid=IwAR3q4KKw9vNm_SUJRI5eOds-lH--MjTAxyinPFuRGoJwWsxmy0n4evp8UOs https://cythilya.github.io/2018/06/19/git-merge-branch-into-master/ --- 錯誤訊息 Q:Refusing to fetch into current branch refs/heads/master of non-bare repository A:在当前分支下fetch 。git checkout 到其他分支,再进行fetch即可。 ![](https://i.imgur.com/1QKbjOQ.png) ###### tags: `git` `tricks`