git如何正确回滚代码

jopen 8年前

git如何正确回滚代码

方法一,删除远程分支再提交

①首先两步保证当前工作区是干净的,并且和远程分支代码一致

$ git co currentBranch  $ git pull origin currentBranch  $ git co ./

②备份当前分支(如有必要)

$ git branch currentBranchBackUp

③恢复到指定的commit hash

$ git reset --hard resetVersionHash //将当前branch的HEAD指针指向commit hash

④删除当前分支的远程分支

$ git push origin :currentBranch   $ //或者这么写git push origin --delete currentBranch

⑤把当前分支提交到远程

$ git push origin currentBranch

方法二,强制push远程分支

①首先两步保证当前工作区是干净的,并且和远程分支代码一致

②备份当前分支(如有必要)

③恢复到指定的commit hash

$ git reset --hard resetVersionHash

④把当前分支强制提交到远程

$ git push -f origin currentBranch

方法三,从回滚位置生成新的commit hash

①首先两步保证当前工作区是干净的,并且和远程分支代码一致

②备份当前分支(如有必要)

③使用git revert恢复到指定的commit hash,当前分支恢复到a>3版本(见下图)

a)此方法会产生一条多余的commit hash&log,其实1c0ce98和01592eb内容上是一致的

b)git revert是以要回滚的commit hash(1c0ce98)为基础,新生成一个commit hash(01592eb)

$ git revert resetVersionHash

④提交远程分支

$ git push origin currentBranch

方法四,从回滚位置生成新的分支merge

①首先两步保证当前工作区是干净的,并且和远程分支代码一致

②备份当前分支(如有必要)

</div>

来自: http://www.cnblogs.com/human/p/5128482.html