git 刪除分支
1. 本地分支:
git branch -D 分支名
2. 遠(yuǎn)程分支:
git push origin :分支名
代碼回滾
1. 本地回滾
將代碼庫(kù)某分支退回到以前的某個(gè)commit ,commit-id之后提交的commit都去除
git reset --hard commit-id
將最近3次的提交回滾
git reset --hard HEAD~3
2. 遠(yuǎn)程代碼庫(kù)回滾
- 應(yīng)用場(chǎng)景:
自動(dòng)部署系統(tǒng)發(fā)布后發(fā)現(xiàn)問題,需要回滾到某一個(gè)commit,再重新發(fā)布 - 原理:
先將本地分支退回到某個(gè)commit,刪除遠(yuǎn)程分支,再重新push本地分支
1、git checkout the_branch
2、git pull
//備份一下這個(gè)分支當(dāng)前的情況
3、git branch the_branch_backup
//把the_branch本地回滾到the_commit_id
4、git reset --hard the_commit_id
//刪除遠(yuǎn)程 the_branch
5、git push origin :the_branch
//用回滾后的本地分支重新建立遠(yuǎn)程分支
6、git push origin the_branch
//如果前面都成功了,刪除這個(gè)備份分支
7、git push origin :the_branch_backup
如果使用了gerrit做遠(yuǎn)程代碼中心庫(kù)和code review平臺(tái),需要確保操作git的用戶具備分支的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指針。。。不建議這樣搞