Previously, we've discussed how to create a local repository, how to add files to it, and how to commit the changes. But what if there are a lot of changes and some of them turn out to be incorrect? In this case, you need to be able to monitor and fix them. Let's figure out how to do it.
Inspecting a commit
Each commit has a unique identifier in the form of a string of numbers and letters. To see a list of all commits and their IDs, you can use the git log command:
$ git log
commit ba25c0ff30e1b2f0259157b42b9f8f5d174d80d7
Author: Admin
Date: Fri Sep 18 13:47:20 2020 +0300
New feature complete
commit b10cc1238e355c02a044ef9f9860811ff605c9b4
Author: Admin
Date: Fri Sep 18 14:33:09 2020 +0300
Added content to my_file.txt
commit 09bd8cc171d7084e78e4d118a2346b7487dca059
Author: Admin
Date: Fri Sep 18 14:50:18 2020 +0300
Initial commitAs you can see, the identifiers are quite long and awkward, but you don't have to copy the whole string to work with them, the first few characters are enough. To see what's new in the commit, we can use the git show command:
$ git show b10cc123
commit b10cc1238e355c02a044ef9f9860811ff605c9b4
Author: Admin
Date: Fri Sep 18 14:33:09 2020 +0300
Added content to my_file.txt
diff --git a/my_file.txt b/my_file.txt
index e69de29..b546a21 100644
--- a/my_file.txt
+++ b/my_file.txt
@@ -0,0 +1 @@
+Hello, world!This example shows when the file was modified, by whom, and what kind of change it was: the new text is "Hello, world!". To see the difference between two commits and figure out where, when, and how it was changed, you can use the git diff command:
$ git diff 09bd8cc..ba25c0ff
diff --git a/file.txt b/file.txt
new file mode 100644
index 0000000..e69de29
diff --git a/my_file.txt b/my_file.txt
index e69de29..b546a21 100644
--- a/my_file.txt
+++ b/my_file.txt
@@ -0,0 +1 @@
+Hello, world!We've compared the first commit with the last one to see all the changes that were made.
Changing the commit
Now we can find the required commit and compare it with other commits. But what if the commit is wrong and needs to be fixed? There are two possibilities here. First, if you made a commit and immediately realized it was wrong, you can use the git commit --amend command. It adds everything from the last commit to the staging area and tries to make a new commit, so you can just type it after the git commit. This allows you to correct the comment or add missing files to the staging area.
If the mistake was made several commits ago and the changes were already sent to the server, then you need to use the git revert command. This command will create a commit that undoes the changes made in the commit with the given ID:
$ git revert b10cc123The most recent commit can be accessed by adding HEAD to the $ git revert without an ID:
$ git revert HEADWhen undoing old commits, be prepared for conflicts. They happen if the file was changed by another, newer commit, and now git cannot find the lines that need to be rolled back as they no longer exist. Please, try to take it into account.
Commit checkout
There is one more way to deal with erroneous commits, using the git checkout command. If you want to get a file from another commit, where an error didn't yet occur, or from the one where it's already fixed, then you can get it via git checkout. You need to write the command itself, the ID of the commit containing the good file, and the name of the file you want to restore changes from:
$ git checkout 09bd8cc1 my_file.txtIn the example above, the contents of the current version of my_file.txt will be replaced with the contents of my_file.txt from the commit 09bd8cc1. We'll come back to the git checkout command when we talk about branching, but there it will be used in a completely different context.
Conclusion
Let's sum up what we've learned in this topic:
To see what commits were made, you need to remember the following three commands:
git logto find out the commit identifier;git showto dive deeper into a specific commit via its identifier;git difffor comparing commits.
2. To modify commits use:
commit --amendif you noticed the mistake immediately after making a commit;git revert <commit ID>orgit revert HEADif changes are already pushed to the server;git checkoutto revert to a specific file version from a specific commit.