Create a New Commit That Revert Back to an Old Commit
How to roll back Git code to a previous commit
Mistakes happen. Thankfully, in Git, admins have two command options to roll back to a previous commit. Learn more about both here.
With Git, IT teams can implement version control. Humans make mistakes, and consequently need to roll back to previous versions of content.
Let's look at how to roll back Git commits to a known-good version, as well as the implications and potential complications of doing so.
What is git reset?
Every time an IT admin commits a Git deployment, that latest commit becomes the head, or tip, of the code tree -- in other words, the current version. The Git commit process provides a point-in-time snapshot (PIT snapshot) of the version-controlled files at the time of every change.
An administrator can roll back the code repository to a previous commit -- that point-in-time copy -- in several ways, depending on the end goal. One approach is the git reset command.
Before using this command, you must understand what git reset does. Outcomes can vary between command uses, and with which switches. Use the command with caution. There are two modes for git reset:
- Soft. This mode resets the code tree's head to the designated former commit instance. All the files between that PIT snapshot and now are set to staged -- ready to commit but not yet committed. This is the default mode.
- Hard. Use this mode with extreme caution. These changes can't be reverted. This command will reset everything, move the head back to the indicated commit version and remove all changes added to the code tree after that specific version number. In effect, the git reset command instantiates a 'hard deletion' of all changes from now -- or point-in-time of code reversion -- to the designated former code commit. It resets the code tree to the version in question and deletes unstaged files.
How to undo a Git commit
First, decide how far back to go into the version history. To view the previous commits, use the git log –-oneline command. This provides the commit details.
Once the IT team chooses a code version to which their tree should revert, use the commit ID to execute the command. In the following example, x12345 represents the commit ID, gained from the git log output:
git reset x12345
Alternatively, there is a shorthand method to roll back the commit: To revert commits without knowing the necessary commit ID, admins can use the command below to revert code versions relative to where the current head is. In the example, ~ 1 refers to the number of commits backward to which the code tree will revert. Figure 1 illustrates the results for adding several commits and then reverting back one version.
git reset head~1

Admins can also use git revert. This command undoes the effects of a bad or incorrect commit. It creates a new head without the issues of the bad commit but doesn't revoke any previous work. However, this version contains history from the bad commit. This inclusion is by design: Git wants to track history accurately, and to delete current heads would create massive gaps in the system history.
With experimentation comes experience. But as noted above, take caution to avoid file loss.
Dig Deeper on Configuration Management and DevOps
-
How to revert a Git commit: A simple example
By: Cameron McKenzie
-
Uncommit Git's Last Commit
By: Cameron McKenzie
-
How to discard local changes in Git
By: Cameron McKenzie
-
Open source version control with Vershd 3.0
By: Adrian Bridgwater
Related Q&A from Stuart Burns
What are the main features of Ansible vs. Ansible Tower?
Both Ansible and Ansible Tower offer enterprises a wealth of resources for config management, but they aren't equals. Assess their differences in ... Continue Reading
Can I install Ansible for Windows system management?
Even though Ansible has its roots in open source software and Linux, is it possible to use the configuration management tool for Windows environments? Continue Reading
What are Ansible roles and when and how should I use them?
Roles are one of several functions in Ansible that simplify and streamline IT automation processes. Dig into this quick overview to learn the basics ... Continue Reading
Create a New Commit That Revert Back to an Old Commit
Source: https://searchitoperations.techtarget.com/answer/How-to-roll-back-Git-code-to-a-previous-commit
0 Response to "Create a New Commit That Revert Back to an Old Commit"
Post a Comment