Undoing commits & changes - 14 days of Git
Tags: 14daysofgit, git, devops
Published: 2022-09-23 12:24:05

We are on day 7 of my 14 days of Git learning journey, so far, I have explored:

* [What is version control?](https://www.techielass.com/what-is-version-control)
* [What is Git?](https://www.techielass.com/what-is-git)
* [Installing Git](https://www.techielass.com/installing-git/)
* [Basic Git commands to get started](https://www.techielass.com/basic-git-commands-to-get-started)
* [Inspect a Git repository](https://www.techielass.com/inspect-a-git-respository)
* [Git File Operations](https://www.techielass.com/git-file-operations)

Now it's time to start looking at how you can undo commits and changes to your repository!

## **Undoing commits & changes**

There are times where you will be made a commit and realised that you want to undo that for whatever reason.

What's the best of doing that though? Deleting the file, you just created, deleting the line you wrote in a file?  What happens when you've made a ton of changes and don't remember all the bits that need undone now?

This is where git revert, and git reset can help.

## **Git Revert**

Git revert is a command that can remove all the changes a single commit made to your repository.  

When you revert a Git commit the changes from that commit are remove from your local workspace.  And a new commit is created to reflect the new state of your repository.

Let's show this in action.

I issue a command to create a new file, called _index.md_ and add and commit that file.

![Add a new file and commit to the repository](https://community.ops.io/remoteimages/uploads/articles/wrv143bz062b0ceoexqr.png)

If I want to revert that commit I first need to use the command _git reflog _to get more details about that commit.

![git reflog output](https://community.ops.io/remoteimages/uploads/articles/navvr63xol1x8xfqawt8.png)

The _git reflog_ command gives me the commit number that I need in order to revert that commit.

In this case I want to revert commit _93a79b1._

I can now issue the command **_git revert 93a79b1_**

Because this command is creating a new commit, I was asked what I wanted the new commit message to reflect.

It's important to remember with this command, you are only reverting the commit you aren't erasing the history of the commit.  So, any changes can still be referenced within the history of the repository.


## **Git Reset**

With Git revert we just undid changes from a specific commit.  However, there might be occasions where you want to revert every change that has happened since a given commit.

This is where the_ git reset_ command can be used.

The steps to use Git reset are:

1. Use _git reflog_ to get the commit number you wish to reset to
2. Issue the command _git reset number_
3. The repository will not reset your repository to the state it was at that choosen commit

Again, with the _git reset_ command remember that you are just reverting to a previous state, you aren't removing the history.  It will still be there to see and refer to.


## **14 days of Git**

It's been interesting today to look at how to undo changes or move to another time in history for the repository.  There is two clear use cases and differences for _git reset_ and _git revert_. Good to expand my knowledge and learn more commands from the Git portfolio.

The next step in my[ 14 days of Git](https://www.techielass.com/tag/14daysofgit) learning journey is to look rewriting history! Be sure to[ subscribe](https://www.techielass.com/newsletter) and join us for that step in the learning journey!

You can follow along here:[ https://github.com/weeyin83/14daysofgit](https://github.com/weeyin83/14daysofgit)