2021-01-11

git如何正确回滚代码

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

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

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

③把当前工作区的HEAD指针指向回滚的commit hash(注意不是branch的HEAD指针)

Notice:这个时候工作区HEAD没有指向分支,称为匿名分支detached HEAD

这个时候提交commit后无法保存状态,git中的任何提交必须是在当前工作区HEAD所在分支的HEAD上进行push hash入栈,所以HEAD必须是属于某个分支的HEAD位置,提交才生效。

$ git co resetVersionHash

④以该commit hash创建一个新的分支

$ git co -b newRevertedHash

⑤切换到当前分支,合并newRevertedHash。

$ git co currentBranch
$ git merge newRevertedHash

⑥进行代码diff,完成代码回滚,push到远程currentBranch

Notice: 也可以直接hotfix,从要回滚的地方直接重新打包一个新tag包,发版本hotFixVersion即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个获取当前日期时间,并将它以“XXXX年XX月XX日 hh:mm:ss”的格式输出打印的示例代码: ```java import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; public class DateUtils { // 获取当前日期时间,并将它以“XXXX年XX月XX日 hh:mm:ss”的格式输出打印 public static void showCurrentDateTime() { DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); Date date = new Date(); String dateTime = dateFormat.format(date); System.out.println(dateTime); } // 计算并显示若干天后的日期值 public static void showDateAfterDays(String currentDate, int days) { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { Date date = dateFormat.parse(currentDate); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_YEAR, days); Date newDate = calendar.getTime(); String newDateString = dateFormat.format(newDate); System.out.println(newDateString); } catch (ParseException e) { e.printStackTrace(); } } // 计算并显示两个日期之间相距的天数 public static void showDaysBetweenDates(String date1, String date2) { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { Date startDate = dateFormat.parse(date1); Date endDate = dateFormat.parse(date2); long diff = endDate.getTime() - startDate.getTime(); long diffDays = diff / (24 * 60 * 60 * 1000); System.out.println(diffDays); } catch (ParseException e) { e.printStackTrace(); } } } ``` 使用示例: ```java public static void main(String[] args) { // 获取当前日期时间,并将它以“XXXX年XX月XX日 hh:mm:ss”的格式输出打印 DateUtils.showCurrentDateTime(); // 计算并显示若干天后的日期值 DateUtils.showDateAfterDays("2021-01-01", 10); // 计算并显示两个日期之间相距的天数 DateUtils.showDaysBetweenDates("2021-01-01", "2021-01-11"); } ``` 输出结果: ``` 2021年05月01日 15:23:45 2021-01-11 10 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值