1. Git 刪除本地分支和遠(yuǎn)程分支
- git 刪除本地分支:git branch -D <branch>
- git 刪除遠(yuǎn)程分支: git push origin :<branch> (origin 后面有空格)
2. 本地代碼庫(kù)回滾
- git reset --hard commit-id:回滾到commit-id岩瘦,講commit-id之后提交的commit都去除
- git reset --hard HEAD~3:將最近3次的提交回滾
3. 遠(yuǎn)程代碼庫(kù)回滾:
這個(gè)是重點(diǎn)要說(shuō)的內(nèi)容糟描,過(guò)程比本地回滾要復(fù)雜
3.1 應(yīng)用場(chǎng)景
自動(dòng)部署系統(tǒng)發(fā)布后發(fā)現(xiàn)問(wèn)題令花,需要回滾到某一個(gè)commit,再重新發(fā)布
3.2 原理
先將本地分支退回到某個(gè)commit泣洞,刪除遠(yuǎn)程分支,再重新push本地分支
3.3 操作步驟
- 1、git checkout <the_branch>
- 2践图、git pull
- 3、git branch the_branch_backup //備份一下這個(gè)分支當(dāng)前的情況
- 4沉馆、git reset --hard the_commit_id //把the_branch本地回滾到the_commit_id
- 5码党、git push origin :the_branch //刪除遠(yuǎn)程 the_branch
- 6、git push origin the_branch //用回滾后的本地分支重新建立遠(yuǎn)程分支
- 7斥黑、git push origin :the_branch_backup //如果前面都成功了揖盘,刪除這個(gè)備份分支
Note:
如果使用了gerrit做遠(yuǎn)程代碼中心庫(kù)和code review平臺(tái),需要確保操作git的用戶(hù)具備分支的push權(quán)限锌奴,并且選擇了 Force Push選項(xiàng)(在push權(quán)限設(shè)置里有這個(gè)選項(xiàng))
另外兽狭,gerrit中心庫(kù)是個(gè)bare庫(kù),將HEAD默認(rèn)指向了master鹿蜀,因此master分支是不能進(jìn)行刪除操作的箕慧,最好不要選擇刪除master分支的策略,換用其他分支茴恰。如果一定要這樣做颠焦,可以考慮到gerrit服務(wù)器上修改HEAD指針。往枣。伐庭。不建議這樣搞
4. git 創(chuàng)建分支并提交到遠(yuǎn)程
4.1,從已有的分支創(chuàng)建新的分支(如從master分支),創(chuàng)建一個(gè)dev分支
git checkout -b dev
4.2,創(chuàng)建完可以查看一下,分支已經(jīng)切換到dev
git branch
* dev
master
4.3,提交該分支到遠(yuǎn)程倉(cāng)庫(kù)
git push origin dev
4.4,測(cè)試從遠(yuǎn)程獲取dev
git pull origin dev
或者:
如果用命令行粉渠,運(yùn)行 git fetch,可以將遠(yuǎn)程分支信息獲取到本地,
再運(yùn)行 git checkout -b local-branchname origin/remote_branchname
就可以將遠(yuǎn)程分支映射到本地命名為local-branchname 的一分支
4.5,我覺(jué)得現(xiàn)在重要的就是設(shè)置git push,pull默認(rèn)的提交獲取分支,這樣就很方便的使用git push 提交信息或git pull獲取信息
git branch --set-upstream-to=origin/dev
取消對(duì)master的跟蹤
git branch --unset-upstream master