Among different ways to undo our previous git commits, git revert
is the safest way to do so. git revert
does not actually overwrite the existing commit. Instead, git revert
understands how to invert the changes introduced by the commit and appends a new commit with the inverted content. This operation is safe because it prevents git from losing git history.
We will go through a simple exercise to try to out git revert
. As long as you have Git CLI installed in your computer, you don’t need the internet connection.
NOTE: You can continue to use a same directory and a same script for other continuing exercises. However, the idea is to allow users to pick up from any step that they want to review later without depending on other steps. In addition, it is always easy to work with a clean slate. Because of this, you will see same or similar commands and instructions getting copied over multiple time.
This will depend on your operating system.
pwd
).Instructions after that should be same.
# Create a directory called `GitRevertDemo`
mkdir GitRevertDemo
# Go inside the directory
cd GitRevertDemo
NOTE: Again, you can continue using this same file for other exercises. It is up to you how you want to manage this.
Create a new file called generate-git-single-branch.sh
. You can do it with either by going inside a directory and create a file through a tool like Visual Studio Code. Or, you can use a tool like VIM to quickly create a file.
touch generate-git-single-branch.sh
chmod +x generate-git-single-branch.sh
vim generate-git-single-branch.sh
Then, copy-and-paste the content of the file with lines below.
#!/bin/bash
FOLDER_GIT=.git
README_FILE=README.md
# Making a new git directory
git init
# Creating a new branch called main
git checkout -b main
# main branch - Making a new file called README.md and commit
touch $README_FILE
git add $README_FILE && git commit -m "Making a new file called README.md"
# main branch - Adding a class to Main.java
echo "# Trying out Git Revert" >> $README_FILE
git add $README_FILE && git commit -m "Added a header to README.md"
# main branch - Adding some description
echo "" >> $README_FILE
echo "Welcome to Git Merge! Here, we will try out git revert exercise" >> $README_FILE
echo "" >> $README_FILE
git add $README_FILE && git commit -m "Adding some description"
# main branch - Adding some more description
echo "" >> $README_FILE
echo "**Git Merge** conference is going to be awesome." >> $README_FILE
echo "" >> $README_FILE
git add $README_FILE && git commit -m "Adding some more description"
Run the following command to execute the script
./generate-git-single-branch.sh
Then, run the following command to check a new file called README.md
and a .git
directory got created.
ls -la
Check the content of README.md. Try to add one new line change to README.md
file. Then, run the following command to commit.
git add README.md && git commit -m "Adding a new change"
If you type the following command, you should see the history of git.
git log --graph --all
Copy the top most commit SHA id. Then, you can type the following command to revert back to the last commit.
git revert <Copied SHA ID>
You can check the status by typing git log --graph --all
and the content of README.md
file again. Notice that your previously copied commit is still there, but there is a new commit that just reverts back your file change.
What will happen if you want to revert back to a previous point after some changes are already made? Let’s test it out! Make few more commits like last one. But in this time, don’t pick the very last commit id but a commit that happened little before. What happens now?