Terminus by Warp
Undo a git push

Undo a git push

Philip Wilkinson
Philip Wilkinson
Software Engineer, Amazon

Version control systems such as Git allow developers to track changes, collaborate seamlessly and manage projects efficiently. However, even the most careful developers can occasionally make mistakes such as pushing code to a remote repository prematurely. This could be because you pushed to the wrong branch, the code is not ready for deployment, or sensitive information was accidentally pushed to a public repository.

[#using-git-reset]Using Git reset[#using-git-reset]

If you need to make sure that the changes introduced by the pushed commits are no longer a part of the repository, then you can use the [.inline-code]git reset[.inline-code] command to undo a [.inline-code]git push[.inline-code]. 

For this, you need to find the commit hash to which you want to reset your branch. This can be done using the [.inline-code]git log[.inline-code] command to view your commit history and identify the desired commit hash. You then need to perform a [.inline-code]git reset[.inline-code] to move the branch pointer to the desired commit:

 $ git reset --soft <commit-hash>

Using the [.inline-code]--soft[.inline-code] flag will ensure that the changes in the commits will remain as part of the working directory and will be staged to be committed. If however, you want to completely remove the changes introduced then you can use the [.inline-code]--hard[.inline-code] flag which will remove all changes from the working directory.

Once you have performed the [.inline-code]reset[.inline-code] you will then need to push the new history back to the remote repository. This can be done using the [.inline-code]--force[.inline-code] flag:

 $ git push --force

This will push the current branch to the remote repository and overwrite the remote history to remove the undesired commits.

It is important to note that by doing this you are overwriting the history of the remote repository. You need to make sure that no other developers are using the changes that you are overwriting as otherwise, this can cause conflicts and headaches for your team.

Easily retrieve this command using Warp’s AI Command Search

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Search feature:

Entering [.inline-code]git push force[.inline-code] into the AI command search will prompt a [.inline-code]git[.inline-code] command that can then be quickly inserted into your shell by doing [.inline-code]CMD+ENTER[.inline-code].

[#using-git-push-force]Using Git push force[#using-git-push-force]

Alternatively, if you don’t want to change your local repository but rather only want to overwrite the changes to the remote repository then you can simply overwrite the remote history. This can be done using:

 $ git push --force origin <last_known_good_commit>:<branch_name>

This will overwrite the history of the remote repository with the last known good commit (identified by using [.inline-code]git log[.inline-code]) from the specified branch name. The benefit of this is that it does not involve changing your local repository to undo the [.inline-code]git push[.inline-code] that was performed. 

With this, just like the [.inline-code]git reset[.inline-code], you need to be careful that no other users of the repository are fetching the incorrect changes or trying to build on top of the commits that you are wanting to remove. This is because with this commit you are rewinding history which can lead to conflicts if other developers already have the changes checked into their local repositories.

[#using-git-revert]Using Git revert[#using-git-revert]

A less dangerous method for undoing a [.inline-code]git push[.inline-code] is using the [.inline-code]git revert[.inline-code] command. This is because [.inline-code]git revert[.inline-code] will revert the changes introduced by a specific commit by creating a new opposite commit. This ensures that the history of the remote repository is preserved and will make it easier if other developers have already checked out the changes made in the remote repository.

You need to first identify the commit that you want to undo using [.inline-code]git log[.inline-code]. Once you have identified this commit then you can run the command:

 $ git revert <commit_hash>

This will create a new commit that will undo the changes in the commit hash specified. To then undo the original git push, you can simply perform [.inline-code]git push[.inline-code] again to add the new commit that undoes the undesired commit. 

This is beneficial when other developers have already checked out the change and when the push that you want to undo does not contain any secrets. This is because the commits in the first [.inline-code]git push[.inline-code] will still remain a part of the history of the repository and so will still be visible to others.

[#undoing-a-forced-push]Undoing a Git push force[#undoing-a-forced-push]

Generally you don’t want to use the [.inline-code]-f[.inline-code] or [.inline-code]--force[.inline-code] option unless you really mean to overwrite the history of the remote repository. This can often make it hard to recover any changes you make, especially if you have used [.inline-code]git reset --hard[.inline-code] to reset the branch before the forced push. 

The ability to recover from a forced push depends on the operations done before performing the push and whether Git has done any cleanup since. However, you can usually use the [.inline-code]git reflog[.inline-code] to identify the state of the branch before performing the forced push. In this case, you can often simply reset the branch to the state before the push as:

 $ git reset --hard origin/<branch_name>@{1}

You need to make sure that this is the desired commit and you may need to use the [.inline-code]git reflog[.inline-code] again to find the desired commit and reset [.inline-code]--hard[.inline-code] to that one. You can then push the branch back to the remote server using:

 $ git push -f origin <branch_name>

Which will again overwrite the remote history but with the branch before if was force pushed originally.

[#preventing-future-mistakes]Preventing future mistakes[#preventing-future-mistakes]

Undoing a [.inline-code]git push[.inline-code] is a valuable skill but prevention is even better. Here are some strategies to minimize the chances of making such a mistake in the future:

[#feature-branches]Using a feature branch [#feature-branches]

You can develop new features and bug fixes on separate feature branches. This reduces the risk of pushing incomplete or untested code to the main branch. You should push any changes to the feature branch first and once you are okay with the changes then you can merge them back into the main branch.

[#code-reviews] Code Reviews [#code-reviews]

You should implement code review processes to catch mistakes before they are pushed. A second pair of eyes can often identify issues that may have been overlooked. This stage happens before the changes are merged into the main branch to ensure that only the correct code is merged in correctly.

[#git-hooks] Git Hooks [#git-hooks]

You can set up pre-push and pre-commit hooks to enforce checks before a code is pushed or merged into the main branch. These hooks can run tests, linting and other validation processes to ensure that the code does not introduce any breaking changes.

[#git-stash] Git Stash [#git-stash]

If you are uncertain about a push or part of a push, you can use [.inline-code]git stash[.inline-code] to temporarily save your changes without making a commit. This allows you to revert back to a clean state if needed.

Experience the power of Warp

  • Write with an IDE-style editor
  • Easily navigate through output
  • Save commands to reuse later
  • Ask Warp AI to explain or debug
  • Customize keybindings and launch configs
  • Pick from preloaded themes or design your own
brew install --cask warp
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist or join the Windows waitlist
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.