Git 回滚代码

内容纲要

如果远程分支origin/master 新提交了A,但是发布代码时想发布A前面的提交版本B,需要先将远程分支回滚到B进行发布,发布后重新将远程分支恢复到A,可以按照以下步骤回滚远程分支并发布旧版本:

  1. 首先,使用以下命令切换到本地 master 分支,并确保它是最新的:

    git checkout master
    git pull origin master
  2. 然后,将本地 master 分支回退到要发布的旧版本 B:

    git reset --hard <commit-hash-of-B>

    注意:<commit-hash-of-B> 是旧版本 B 的提交哈希值。

  3. 接下来,将本地更改强制推送到远程 master 分支:

    git push -f origin master

    注意:使用 -f 参数会强制覆盖远程分支,因此请确保在执行前已经确认了回退操作。

  4. 将代码版本 B 发布出去。

  5. 最后,将远程 master 分支恢复到最新的提交 A:

    git push -f origin A:master

    注意:使用 <commit-hash-of-A> 替换 A ,是最新提交 A 的提交哈希值。

现在您已经成功回滚了远程分支并发布了旧版本 B,然后再恢复远程分支到最新的提交 A。。

One thought to “Git 回滚代码”

  1. Admiring the time and effort you put into your website and in depth information you offer.

    It’s nice to come across a blog every once in a while that isn’t the same unwanted rehashed
    material. Excellent read! I’ve bookmarked your site and I’m adding your RSS feeds
    to my Google account.

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注