簡介:
公司開發(fā)都是基于 repo 進行 fork略吨,然后再從 fork 的資源里面提交爆捞,開發(fā)完畢后再提合并請求(pull request)炎疆。
這個過程中卡骂,會出現(xiàn)兩個問題,一是從主倉庫中更新代碼形入,而是同步主倉庫中的分支全跨。下面是基于這兩個問題,找到的解決方案亿遂。
【PS】請先自己弄測試庫進行嘗試浓若。
1. 從主倉庫中更新代碼
1. 添加遠程倉庫鏈接,名字叫 mainRepo蛇数,可以自行修改
> git remote add mainRepo [mainRepo_link];
2. 更新 mainRepo 中的數(shù)據(jù)
> git fetch mainRepo
3. 切換到你想要更新的分支
> git checkout [your_branch]
4. 更新代碼
> git rebase/merge mainRepo/[your_branch]
rebase 會有之前的提交記錄挪钓,當你合并到主倉庫時,記錄會跟著一起提交耳舅。
merge
自己寫的一個簡易的腳本文件
#!/bin/bash
branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
git fetch main && git stash && git merge main/$branch && git stash pop && git pull && git push
2. 從主倉庫中拉取新分支
1. 先拉取主倉庫中的信息到本地
> git fetch mainRepo
2. 基于主倉庫的分支诵原,常見新分支
> git checkout -b [newBranch] mainRepo/[newBranch];
3. 提交倉庫到你 fork 的倉庫
> git push origin head:[newBranch];
4. 切換到新分支,需要刪除本地的這個 newBranch 分支
> git checkout master && git branch -d [newBranch];
5. 切換到新提交到遠程的分支
> git fetch && git checkout [newBranch];
自己寫的一個簡易的腳本文件
#!/bin/bash
# fileName newBranch.sh
newBranch=$1
git stash && git fetch main && git checkout -b main/$newBranch && git push origin head:$newBranch && git checkout master && git branch -d $newBranch && git checkout $newBranch && git stash pop