Undoing Pushed Commits in Git with reset and restore
We showed three ways to undo commits pushed from a remote repository in Git. To do this, we used the git reset
, , revert
and checkout
commands.
When we use git reset
, we also remove any traces of the unwanted commit from the repository history. But with git revert
, both the original commit and the undo commit remain in the history.
If we use git checkout
, we undo the changes in the new branch.
Use git reset
the command to undo the pushed commits
We created a undo_pushed_commits_local
repository and populated it with some healthy (good) commits.
mkdir undo_pushed_commits_local
git init
Then
add/modify files.
git add --all
git commit -m "Make healthy commit"
Add/modify more files.
git add -all
git commit -m "Make another healthy commit"
We will now push these commits to the repository on GitHub undo-pushed-commits-remote
.
git remote add undo-remote git@github.com:danielturidandy/undo-pushed-commits-remote.git
git push undo-remote master
Our remote repository now has two commits.
Now, let's make a bad commit.
echo "This is a bad addition" >> file1.txt
echo "This is a bad addition" >> file2.txt
git add -all
git commit -m "Make a bad commit"
We push this commit to the remote repository.
git push undo-remote
Our remote has the wrong commit:
We are now 重置
at the last good commit in our local repository. We use --hard
the -p option to remove any traces of the bad commit from our commit history.
git log
git reset --hard <SHA of the last good commit>
We use the -f option 推送
to force push the local repository to the remote repository.
git push -f undo-remote
This method is best suited for private repositories or repositories for small teams.
Large teams may run into problems if some developers pull remote to local environment after pushing bad commits before we reset to a good state. They have no commit history with 纠正
bad commits for their repositories.
Use git revert
the command to undo the pushed commits
Here we push four wrong commits to the remote repository.
We can use revert
to undo a single bad commit or a series of bad commits.
revert
Make a new commit that reverses the unwanted commit. Both the original commit and the reversed commit are preserved in the repository history.
revert
To undo a single pushed commit,
use
git revert <SHA of the commit we want to revert>
We will now push this change to the remote repository. Remember to use the -f flag to ensure there are no conflicts.
git push -f undo-remote
Our remote repository now has a new commit that reverses the bad commit.
revert
To undo a series of pushed commits,
use
git revert <SHA of the oldest commit to revert>..<SHA of the newest commit to revert>
This will revert all commits in the provided range (excluding the oldest commit, but this may also depend on the version/platform you are using.)
We will now push the changes to the remote using the -f flag.
git push -f undo-remote
This approach is great for public repositories with large teams. Since both bad commits and undo commits are kept in the history, developers can update their local repositories with all bad pushes/reverts.
Use git checkout
the command to undo the pushed commits
This technique is a quick hack that creates a new branch without the bad commits. It's great for small codebases and if you're pressed for time.
We first checkout
get to the last good commit.
git checkout <SHA of last known good commit>
We then reach the detached HEAD state at the last known good commit in the repository's history.
We now fork a new branch from this state.
git checkout -b branch_without_badcommits
We then push it to the remote into a new branch.
git push -f undo-remote branch_without_badcommits
The remote repository now has a new branch without any bad commits.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Git installation and establishment of local warehouse service
Publish Date:2025/04/05 Views:89 Category:Git
-
Git is a distributed version control system: the client does not only extract the latest version of the file snapshot, but also completely mirrors the original code repository. It has the following advantages: a. Since every extraction oper
git remote operation——multiple remote repositories for one project
Publish Date:2025/04/05 Views:131 Category:Git
-
Multiple remote repositories for a git project In our git project, the command to operate the remote repository information is $ git remote # 查看当前所有的远程仓库的名称 $ git remote -v # 查看远程仓库的名称和远程仓
Git cherry pick command usage
Publish Date:2025/04/05 Views:190 Category:Git
-
git cherry-pick is a powerful command that allows us to select an arbitrary Git commit by reference and attach it to the HEAD of the current working branch. Cherry picking is the act of picking a commit from one branch and applying it to an
Comparison between Git merge and Git rebase
Publish Date:2025/04/05 Views:171 Category:Git
-
The git rebase command may seem like Git wizardry to beginners, but if used carefully, it can actually make life easier for your development team. In this article, we compare git rebase with the related git merge command and identify all th
How to fix Git error Error: src refspec master does not match any
Publish Date:2025/04/05 Views:124 Category:Git
-
When using Git, we may encounter the error "src refspace master does not match any". Here's what the error means and how to fix it. What does src refspec master does not match any Mean in Git mean? We may encounter this error when we try to
Rebase local branch when pulling changes from remote repository branch in Git
Publish Date:2025/04/05 Views:144 Category:Git
-
This article will cover the basics of rebasing your local branch when pulling changes from a remote repository branch in Git. We use the version control system Git to track changes made to files. We commit changes in a local branch in our l
Undo Git Stash
Publish Date:2025/04/04 Views:187 Category:Git
-
This article explains how to make and save changes to a repository. Git allows you to save changes locally and push them to a server when needed. In Git, we don't use the term save , but commit . We use git add , git commit , and git stash
View a list of cache entries in Git
Publish Date:2025/04/04 Views:59 Category:Git
-
We often need to pause our work and focus on something else in our development environment. Therefore, we may need to temporarily save our current work and focus on a different one. We may want to resume our original work later. git stash T
Git stores specific files
Publish Date:2025/04/04 Views:115 Category:Git
-
This article will cover storing changes to only specific files in Git. In Git, when we make some changes in our working tree, we may have some changes which may or may not be staged in our local repo. We may now wish to save these changes f