新建分支
$ git checkout -b iss53
Switched to a new branch "iss53"
它是下面兩條命令的簡(jiǎn)寫:
$ git branch iss53
$ git checkout iss53
刪除分支
刪除本地
git branch -D br
刪除遠(yuǎn)程
git push origin :br (origin 后面有空格)
代碼回滾
【本地代碼庫(kù)回滾】:
git reset --hard commit-id :回滾到commit-id隆嗅,講commit-id之后提交的commit都去除
git reset --hard HEAD~3:將最近3次的提交回滾
【遠(yuǎn)程代碼庫(kù)回滾】:
這個(gè)是重點(diǎn)要說的內(nèi)容,過程比本地回滾要復(fù)雜
應(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
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è)備份分支
如果使用了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指針尚骄。。。不建議這樣搞