Git: roll back to a previous version
Do you know some of the main commands for Git to roll back to a previous version? Do you want to learn about them? In this post we will explain how to undo and redo changes in Git (go back to a previous version). This platform is a version control system which allows you to manage your projects and keep a record of the history of your programs. This helps you avoid losing them. Next, we tell you how to undo and redo changes in Git (go back to a previous version).
How to undo changes in Git (go back to a previous commit)
Commands in any program allow us to facilitate the process of writing code, from completing an action to storing a program. Even so, At some point, you need to recover a previous Git commit.
If you need to go back to a previous version of your program, Git has a command for you: git reset. This command has three forms of invocation: –hard, –mixed and –soft.
This time, we will tell you about the first one: –hard. The git reset –hard way rolls the system back into its dependency line up to the version proposed by the programmer. This leaves the code as it was in the version that was rolled back to.
#go back to a previous version, the number 1 changes depending on the steps you want to go back in the dependency boundary
git reset –hard HEAT~1
If we want to know the history of the repository to see what changes the git reset –hard command undid, you will have to ask the computer the following command: git log. This is used to explore the repository in Git.
There is another way to return to a position in Git using the unique key:
#return to a previous versionr with unique key or the «hash»
git reset –hard HEAT
There are other commands that will be useful for working with branches in Git and that are also used in the process of undoing and redoing changes in Git that you can find in the program’s documentation, such as git branchesgit checkout master or git log.
Now, according to this action, you can realize that Git has features, such as its operation with pointersbeing HEAD the current version of the program we are working on and branches or forks of the development of our program.
On the other hand, it is key that you know that Git never deletes anything, since each version has a unique key. Therefore, even if you undo the changes, there is a possibility of bringing them back.
Recover previous Git commit
If you want to recover the versions that you undid in a previous step, you must use the following command: git reflog.
Once the command is used git reflog, you will see how it returns a list with the movements that the repository has gone through, from the most modern movement to the oldest movement. In that list you will also find 7 digits (at the beginning of each line). These are the HASHwhich will allow you identify the commit and return to them redoing the changes.
#to redo changes from a given commit.
git reflog –hard HEAT
At this time, the HEAT marker should be on the commit that you have entered when adding its key or HASH.
What is the next step?
Although you already know how to undo and redo changes in Git, It is important that you learn about its functionalities for programmers and the relationship with Github, which is an online place where they can repository the programs that are saved in Git. It is a vital tool for your path as a programmer.
If you want to know more about how to use Git for your code projects or want to practice other concepts and tools that programming offers youwe invite you to take a look at the Bootcamp Learn to Program from Scratch. Don’t miss it and sign up!