Navigating through Git can sometimes be challenging, especially when you need to undo changes or revert to a previous state.
Understanding the various Git commands is important whether you want to revert the last commit, restore a previous commit, or change a commit message.
Commands such as git revert, git revert HEAD, and git revert commit ID allow you to safely undo changes without altering the commit history.
On the other hand, git hard reset commit and git reset local commits provide more drastic measures to reset your repository.
But the question is – which one of these commands is right for you?
In this guide, we will help you understand these commands so that you can make the right choice. We’ll also provide step-by-step instructions on how to revert your git repository to a previous commit.
Let’s get started!
What Is Git Reset?
Git reset is a powerful command that allows you to undo changes and modify your project’s history.
When you use git reset, it moves the current branch pointer to a different commit. This action can undo changes, remove commits from your branch history, and modify the state of your working directory and staging area.
Suggested read: GitHub vs. GitLab vs. Bitbucket – How Are They Different?
What are The Three Types of Git Reset?
Git offers several types of resets, each serving different purposes:
- The
--soft
reset moves the head to a specific commit without altering the index file or the working tree. This means all your changes remain staged and ready to be committed again. It’s useful when you want to undo the last commit but keep your changes staged.
For example,git reset --soft HEAD~1
undoes the last commit but keeps the changes staged. - The
--mixed
reset, which is the default mode, resets the index but not the working tree. This means your changes are preserved but not staged for commit. It’s handy when you want to unstage changes without losing them.
For example,git reset --mixed HEAD~1
unstages the last commit’s changes but keeps them in the working directory. - The
--hard
reset is more drastic as it resets both the index and the working tree to match the specified commit. This discards all changes made since that commit. It’s useful when you want to completely discard all changes and reset to a specific commit.
For example,git reset --hard HEAD~1
discards all changes and resets the working directory to the previous commit. - The
--merge
reset updates the index and working tree for files that differ between the specified commit and HEAD, but keeps local changes that are not yet staged. This is useful when you want to reset a commit but keep local changes.
For example,git reset --merge HEAD~1
resets the index and updates the working tree, preserving local changes. - The
--keep
reset is similar to –merge, but it aborts if there are local changes that differ from the specified commit. This ensures no local changes are lost.
For example,git reset --keep HEAD~1
resets the index and updates the working tree, aborting if there are local changes. - Lastly, the
--recurse-submodules
option resets the working tree of all active submodules according to the commit recorded in the superproject. This is useful when working with submodules and you want to ensure they are reset along with the main project.
For example,git reset --recurse-submodules HEAD~1
resets the working tree and all active submodules to the specified commit.
Suggested read: Laravel With GIT Deployment The Right Way
Git Reset vs Git Revert vs Git Restore vs Git Checkout
Git reset is often confused with other commands. Let’s take a look at each of them and find out which one is right for you:
Git Reset
You should use git reset to undo local changes or restructure your commit history. It offers flexibility, allowing you to choose how much you want to undo. However, you should be cautious when using git reset on shared branches, as it can cause conflicts with other developers’ work.
Git Revert
You can use git revert to safely undo changes in public repositories. It creates a new commit, keeping the original history intact. This is ideal for collaborative environments where preserving history is important.
Suggested read: The Best Web Development Tools To Level Up Your Stack
Git Restore
Git restore allows you to undo modifications to files in your working directory. It reverts selected files to their state in the last commit or a specific commit, discarding any uncommitted changes. This command is useful when you want to abandon local modifications and return files to their previous known good state without affecting your branch or commit history.
Git Checkout
When Git checkout is used with a branch name, it updates your working directory to match the specified branch’s latest commit. If it is used with a file path and commit reference, it restores that file to its state in the specific commit without changing your current branch.
Suggested read: The Easiest Way To Automate WordPress Deployments with Git
How to Perform a Git Reset to Revert & Restore to a Previous Commit? (With Detailed Steps)
Follow these detailed steps to perform a Git reset and restore your project to a previous commit:
Step 1: Identify the commit you want to reset to
First, you need to find the commit hash of the point you want to reset to. Open your terminal or command prompt, navigate to your Git repository, and use the following command to view your commit history:
git log
This command will display a list of commits, each with a unique hash, author information, date, and commit message.
Alternatively, you can view this commit history in your Git provider’s dashboard. Scroll through the list and find the commit you want to reset to. Note down the commit hash, which is a long string of letters and numbers.
Step 2: Choose the type of reset
Git offers several types of reset options – you can refer to our explanation in the previous section to understand each of them. You need to decide which type you need based on your goal. For this example, we’ll use a mixed reset, which is the default option.
Step 3: Perform the Git reset
Now that you’ve identified the commit and chosen the reset type, you can perform the reset. Use the following command, replacing <commit-hash>
with the hash you noted earlier:
git reset <commit-hash>
This command will move your branch pointer to the specified commit and update your staging area. Your working directory will remain unchanged, allowing you to review the changes before committing them again.
Step 4: Review the changes
After performing the reset, it’s important to review the changes. Use the following command to see the status of your working directory:
git status
This will show you which files have been modified, added, or deleted since the commit you reset to. You can also use git diff to see the specific changes in each file.
Step 5: Commit the changes
Once you have reverted the changes, you can make modifications and edits to the files as you normally would.
If you’re satisfied with the reset and any restorations you’ve made, you can now commit these changes. First, add the files you want to include in the commit:
git add .
git commit -m "Reverted to previous state and restored specific files"
Step 7: Push the changes (if working with a remote repository)
If you’re working with a remote repository and want to update it with your reset changes, you’ll need to force push. Be cautious with this step, as it can overwrite the remote history:
git push --force origin <branch-name>
Replace <branch-name> with the name of your current branch (e.g., main or master).
Suggested read: Understanding Continuous Integration vs. Continuous Deployment
Wrapping Up
Understanding Git reset is essential for working effectively with version control systems. By mastering these tools, you can confidently navigate your project’s history, undo mistakes, and maintain a clean commit history.
However, managing version control is just one aspect of web development and deployment.
For a truly seamless experience in managing and deploying web applications, RunCloud offers an unparalleled solution.
RunCloud simplifies server management and application deployment, allowing you to focus on your code rather than infrastructure complexities. With features such as automated backups, easy scaling, and robust security measures, RunCloud takes the hassle out of web application management.
One of RunCloud’s standout features is its support for atomic deployments. This automated solution ensures that your application updates are applied as a single, indivisible unit.
By combining your Git knowledge with RunCloud’s powerful platform, you can streamline your development workflow, from version control to production deployment.
Experience the hassle-free deployment process with RunCloud.
FAQ on Git Reset
Does git reset delete new files?
No, git reset does not delete new files. It only affects tracked files and the staging area, leaving untracked files untouched.
What is the difference between git reset and git restore?
git reset updates your branch and can change the commit history, while git restore is used to restore files in the working tree or staging area to a previous state without altering the commit history.
What is the difference between git reset and git reset hard?
git reset can be used with different modes (–soft, –mixed, –hard) git reset –hard resets the index and working tree to match the specified commit, discarding all changes in tracked files.
Is git reset local or remote?
git reset is a local operation. It only affects the local repository and does not interact with remote repositories.
Does git reset restore deleted files?
Yes, git reset can restore deleted files if they were tracked and the reset is performed to a commit where those files existed.
What does git reset file do?
git reset <file> unstages the specified file, moving it from the staging area back to the working directory without altering the working directory content.
Will git reset remove local changes?
git reset –hard will remove local changes by resetting the working directory and index to match the specified commit, discarding any modifications.