---
title: "把先前的 commit 强制 push 到 repo"
description: "如果你已经 push 了一些代码，想要 force push 某个较早的 commit，可以这样做 shell git push origin +commit\\sha^:branch\\name"
canonical: https://oozou.com/til/force-push-previous-commit-to-repo-44
locale: zh-Hans
date: 2020-10-13
topic: shell
---

# 把先前的 commit 强制 push 到 repo

> 如果你已经 push 了一些代码，想要 force push 某个较早的 commit，可以这样做 shell git push origin +commit\sha^:branch\name

Published: 2020-10-13 · Topic: [shell](https://oozou.com/zh-Hans/til-topics/shell) ([markdown](https://oozou.com/zh-Hans/til-topics/shell.md))

如果你已经 push 了一些代码，想要 force push 某个较早的 commit，可以这样做
*shell
git push origin +commit\_sha^:branch\_name*

其中 git 会把 *sha^* 理解为 *sha* 的父 commit，把 *+* 理解为强制的 non-fastforward push。

你也可以使用 *HEAD^*，其中 *^* 代表 *HEAD* 的父 commit，所以 *HEAD^^^* 会推送当前 commit 往前数第 3 个的那个 commit。
例如
*shell
git push origin +HEAD^^^:feature/i\_did\_an\_oopsie*

---

Contact OOZOU: hello@oozou.com · +66 2544 0000 · https://oozou.com/zh-Hans/contact

More for AI agents: https://oozou.com/llms.txt · every page has a markdown twin at its URL plus ".md"
