Run git log to Find Revert Commits
In this step, we will learn how to use git log
to find commits that have been reverted. Reverting a commit means creating a new commit that undoes the changes introduced by a previous commit. This is useful when you've made a mistake and want to easily undo it without losing the history of the original commit.
First, let's make sure we are in our project directory. Open your terminal and navigate to the my-time-machine
directory:
cd ~/project/my-time-machine
Now, let's create a few commits to simulate a project history, including a commit that we will later revert.
Create the first file:
echo "Initial content" > file1.txt
git add file1.txt
git commit -m "Add file1"
You should see output similar to this:
[master (root-commit) a1b2c3d] Add file1
1 file changed, 1 insertion(+)
create mode 100644 file1.txt
Now, let's add some more content and make another commit:
echo "Adding more content" >> file1.txt
git add file1.txt
git commit -m "Add more content to file1"
You should see output similar to this:
[master 4e5f6g7] Add more content to file1
1 file changed, 1 insertion(+)
Next, let's make a commit that we will revert later:
echo "This commit will be reverted" > file2.txt
git add file2.txt
git commit -m "Add file2 (will be reverted)"
You should see output similar to this:
[master 8h9i0j1] Add file2 (will be reverted)
1 file changed, 1 insertion(+)
create mode 100644 file2.txt
Now, let's revert the last commit. We can use git revert HEAD
to revert the most recent commit:
git revert HEAD --no-edit
The --no-edit
flag tells Git to automatically create the revert commit message without opening an editor. You should see output similar to this:
[master k2l3m4n] Revert "Add file2 (will be reverted)"
1 file changed, 1 deletion(-)
delete mode 100644 file2.txt
Great! We have now created a commit that reverts the changes from the "Add file2 (will be reverted)" commit.
Now, let's use git log
to view our commit history and find the revert commit:
git log --oneline
You should see output similar to this:
k2l3m4n (HEAD -> master) Revert "Add file2 (will be reverted)"
8h9i0j1 Add file2 (will be reverted)
4e5f6g7 Add more content to file1
a1b2c3d Add file1
Notice the commit message "Revert 'Add file2 (will be reverted)'". This clearly indicates that this commit is a revert of a previous commit. Using git log
with the --oneline
flag is a quick way to see a summary of your commit history and identify revert commits by their message.