We are on day 7 of my 14 days of Git learning journey, so far, I have explored:
- What is version control?
- What is Git?
- Installing Git
- Basic Git commands to get started
- Inspect a Git repository
- 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.
If I want to revert that commit I first need to use the command _git reflog _to get more details about that commit.
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:
- Use git reflog to get the commit number you wish to reset to
- Issue the command git reset number
- 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 learning journey is to look rewriting history! Be sure to subscribe and join us for that step in the learning journey!
You can follow along here: https://github.com/weeyin83/14daysofgit
Top comments (0)