Git revert local commits
When a software engineer or a web developer uses Git, it is obvious that he pushes a lot of code and commits to the Git repository every day, and while doing so, the need to undo or revert a particular commit or a set of commits arises from time to time in a team environment.
In this block, we will understand the possibilities available with Git programs and commands to undo after committing to a local branch.
This document tries to understand what happens to the commits we make and what kind of undo we can do to recover commits that have not yet been synced with the remote branch.
Before dealing with this situation, we should make sure that we need it and want to modify the commit files and information. Because in some cases, we just want to edit our commits, which can be done with the Git command git amend.
But if we want to undo a command, this guide is the best place to learn about it. Let’s learn how to do it efficiently using the following Git commands.
Use git reset to undo committed changes
There are multiple situations where we want to undo the last commit, the easiest way to solve this problem and undo the last Git commit is to use the command git reset
and add the --soft flag. This feature of Git preserves the changes to our files in the local branch.
To do this, we have to specify the commit we want to undo, which is HEAD~1; Here, we want to undo the last commit, as this is a common practice where developers are most likely to need to undo their last commit. But we can mention any commit we want to undo.
For those situations, git reset
the commands listed below are the best choice.
$ git reset --soft HEAD~1
With the reset option, our current HEAD branch will be projected to the specified revision and back to one commit before the most recent revision, which makes our last commit undone and the changes will automatically come to our file modifications section.
With the help of the flag --soft the changes we make will remain in the file status section and will not be lost.
Also, if we do not want to keep our changes and we are sure that we do not need them anymore, then we will use the flag --hard instead of the flag --soft .
This will permanently undo the changes and all content will be lost. To permanently remove the changes, use the following command.
$ git reset --hard HEAD~1
Use git revert to undo committed changes
git revert
command is specifically used to develop a new commit that helps us revert the changes of a specified commit. This command is famous for completely reverting a commit without deleting it.
If commits have already been pushed to the remote repository, then using git revert
is the best and safest option as it will not overwrite the commit history.
The command to revert the last commit is as follows.
git revert <commit to revert>
The name of the commit is the commit ID that we want to revert; it can be reverted through git's command, that is, git log . Most developers prefer git revert over git reset because it undoes the changes with the help of a new command that performs the same undo operation.
It does not discard or overwrite previous commits. Reset discards all unattractive commits and also changes the commit history.
Now it's up to you to decide which command is best for your situation.
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
Changing drives in Git Bash
Publish Date:2025/03/30 Views:56 Category:Git
-
This short article will discuss how we can use Git Bash to have a Unix-style command line environment in Windows operating system and run multiple commands in this terminal. Git Bash Git is a collection of command-line utilities created to
Adding a remote branch in Git
Publish Date:2025/03/30 Views:142 Category:Git
-
Git does not allow its developers to create new branches on remote repositories. But instead, we can push an already existing local branch, and after doing so, we can bring it to the remote repository using some Git commands. In every versi
Synchronize your local repository with a remote repository in Git
Publish Date:2025/03/30 Views:92 Category:Git
-
This article outlines the process of syncing your local repository with a remote repository. We will also see how to sync a GitHub branch with a remote repository on the command line. Synchronize your local repository with a remote reposito
Creating a remote repository from a local repository in Git
Publish Date:2025/03/30 Views:105 Category:Git
-
This article discusses the necessary steps to create a remote repository based on a local repository. This is ideal when you have a local repository that needs to be available on a remote or SSH-enabled server. Creating a remote repository
Removing the upstream repository in Git
Publish Date:2025/03/30 Views:177 Category:Git
-
This article will teach you how to delete an upstream repository in Git. We may sometimes need to delete or change the remote repository that we use with our local repository. To do this, we can use the Git command git remote . Removing the
Git remote add SSH
Publish Date:2025/03/30 Views:53 Category:Git
-
In this day and age, the most widely used version control system is Git, which is operated by most developers within a team structure. This is mainly used to increase code efficiency, no matter how big or critical the project is. In this se
Differences between Git Reset, Revert and Checkout commands
Publish Date:2025/03/30 Views:185 Category:Git
-
This article discusses the differences between the git reset , , git revert and git checkout commands. These are some of the most useful Git utilities that allow us to undo certain changes in our repository. It’s easy to get confused with
Creating and using branches in Git
Publish Date:2025/03/30 Views:99 Category:Git
-
This article introduces Git branches. We will see how Git branches can help you organize your projects. Some of the commands we will deal with are git branch and git checkout . git branch Use commands to create, display, and delete branches
Git force pull
Publish Date:2025/03/29 Views:166 Category:Git
-
In this tutorial, we will learn how to force pull changes from a remote repository in Git. Sometimes, we may need to discard local modifications and replace them with updates from a remote repository in a collaborative development environme