---
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-Hant
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-Hant/til-topics/shell) ([markdown](https://oozou.com/zh-Hant/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-Hant/contact

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