Think Like a Git, Commit Like a Boss: Reset and Amend
There are situations where you commit files to Git and discover that what you just did was very wr0ng.
There are also situations where your commit message is either wrong or has a typo.
Let’s see how we can handle such situations.
From our previous post, let’s go ahead and run a git status:
We should have something like this:
So we have just one commit:
Now, open the git-education folder, let’s add some information to the notes.txt file.
Add this:
1 |
This is the second lesson. |
Now the contents of notes.txt will be:
1 2 3 |
This is the beginning of my git education. Time to learn at goodheads.io This is the second lesson. |
Let’s save and commit:
1 2 3 |
git add . git commit -m "Add more informations" |
Now check the history, run git log, we’ll have something like this:
Now we have those,..but we just made a grammatical blunder by adding informations instead of information.
There are different ways to handle this, one way to this is to erase the last commit totally by doing this:
Reset your History
1 |
git reset --hard 7227fde4b8d386c3937ab29c967123306dd9cb22 |
7227fde4b8d386c3937ab29c967123306dd9cb22 refers to the commit hash
So this totally erases all the changes made after the commit at 7227fde4b8d386c3937ab29c967123306dd9cb22
Now, open the notes.txt file, you will discover our last change to the file is gone totally. I don’t recommend this method..because you don’t absolutely need to do that just because you want to change a commit message. Imagine if you worked on a feature for about 1 hour, you don’t want your efforts gone in a jiffy.
Although this command is also very useful, you might be working with a team of developers that have just done or committed very wrong things to the project. The command above comes in handy because it just totally erases all their changes and cleans the git history too to an earlier point in the course of the project.
Replace Last Commit
The best way to go about this is just running this command:
1 |
git commit -m --amend "Add more information" |
This checks git history for the most recent information and correct it to the commit message we have above. So simple!!!.
Go ahead and check the history using git log. You will discover that the history now contains the right commit message for the second commit.
Now, we know this, there’s more to be learned!. Stay tuned for more posts on this series.
Please let me know if you have any questions in the comments section
- How to build your own Youtube – Part 10 - August 1, 2016
- How to build your own Youtube – Part 9 - July 25, 2016
- How to build your own Youtube – Part 8 - July 23, 2016
- How to build your own Youtube – Part 6 - July 6, 2016
- Introducing Laravel Password v1.0 - July 3, 2016
- How to build your own Youtube – Part 5 - June 28, 2016
- How to build your own Youtube – Part 4 - June 23, 2016
- How to build your own Youtube – Part 3 - June 15, 2016
- How to build your own Youtube – Part 2 - June 8, 2016
- How to build your own Youtube – Part 1 - June 1, 2016