Tuesday, April 21, 2020

How to perform git revert - revert to previous Git commit - Git Revert Examples

Git is popular distributed and open source version control systems. It is very easy to use. Let us see how can we revert to previous commit.

In order to demo this, I have created a sample repo in GitHub. You can clone it without using ssh keys. You can also try in your Sample Repo as well.

git clone https://github.com/akannan1087/myRepoForJavaApp.git

cd myRepoForJavaApp

First code commit
Let us create a text file called hello.txt
sudo vi hello.txt
Add one line like below
First commit!

git add hello.txt
git commit -m "making our first commit"
git push

you will see all commits with latest commit in the top.

Second code commit
sudo vi hello.txt
Add one line like below
First commit!
Second commit!

git add hello.txt
git commit -m "making our second commit"
git push

third code commit

sudo vi hello.txt
Add one line like below
First commit!
Second commit!
Third commit!!!

git add hello.txt
git commit -m "making our third commit"
git push

Try to view all commits in one line
git log --oneline

547d163 making our third commit
1ec0921 making our second commit
550095c making our first commit
1bad8df Update index.jsp
c206c37 Update index.jsp
161a241 Update index.jsp
5b4e649 my first project setup in GitHub
b429bb6 Initial commit

How to revert to first commit
Now let us say we want to revert to first commit # 550095c making our first commit

git checkout  550095c hello.txt

type git status

git commit -m "reverting to first commit"
git push

cat hello.txt

Please click here if you want to learn more about Git.

No comments:

Post a Comment