DEV Community

Cover image for Without Reverting, Open old version of Code with Git | Git Tag
Tanmay Khatri
Tanmay Khatri

Posted on

Without Reverting, Open old version of Code with Git | Git Tag

Was your current merge to production buggy?
Do you want to go back to the previous version of code without reverting the changes?

Switching to old version without Reverting

git checkout <commit id of the previous/old merge>
git checkout 893faa2a
Enter fullscreen mode Exit fullscreen mode

What this does is that it shifts the HEAD to the old version of the code stored in git without making any changes to the commit history.

Commit Checkout without Reverting

The HEAD is in detached mode so you can make changes and commit them but they will not affect the branch
To keep the changes and commits, you can create a new branch by :
git switch -c <new-branch-name>

This look cool but, the problem arises when you have to remember the commit ids for the merge commits and it is a tedious task.

Using Tags in Git

Git has a special feature of tagging the commits.
We can use this feature to tag our commits/merge commits as a version of the code.

To add a tag to the current commit

git tag <name-of-the-tag>
git tag v1.4.0
Enter fullscreen mode Exit fullscreen mode

To add a tag to an old commit

git tag <name-of-the-tag> <commit-id>
git tag v1.3.0 893faa2a
Enter fullscreen mode Exit fullscreen mode

To list all tags

We can also list all the available tags in ascending order.

git tag
Enter fullscreen mode Exit fullscreen mode

Listing Available tags with Git

Now that we have tagged our commits we can easily remember their names and checkout to them whenever we want without reverting the code.

Switching to old version without Reverting using Tags

git checkout <tag-name>
git checkout v1.3.0
Enter fullscreen mode Exit fullscreen mode

Switching to old version of code without Reverting using Tags in Git

For More Info Visit

Top comments (0)