Rolling Back

How do we rollback to a previous commit? Well there are a few options. We will demo Revert and Reset.

Revert

Git Revert allows the undoing of a previous commit, but it also keeps all the commit history by only adding to the commit history.

Note

This is the preferred method to use.

Let’s create a file named example then make the following edits and commits

mkdir  resetlab
cd resetlab
git config --global init.defaultBranch main
git init
vim example

Now that the file is open in vim use i for insert and add the following line:

This is line one

:wq
git add example
git commit -m "one"

Let’s open the file in vim again and add the next line:

vim example

Add this line to the example file:

This is line two

And now let’s save our file with the new changes, then add to staging and commit

:wq
git add example
git commit -m "two"
git log --oneline

Adding the last line in:

vim example

This is line three

And now let’s save our file with the new changes, then add to staging and commit

:wq
git add example
git commit -m "three"
git log --oneline

Next you will need to push this file to our repository with our 3 commits

git push -u git@gitlab.com:<username>/resetlab.git main

We will now select the commit we wish to remove, in this example it’s commit three.

../_images/gitrevert1.png

Fig 19

From Fig 19 our command would be

git revert eda398b

Once you hit enter, you will be prompted for a revert message. Once you’ve added the revert message and saved :wq let’s review the git log

git log --oneline

Here we can see that instead of dropping off the third commit message like using reset would, with revert it actually adds an extra commit and keeps previous commit messages. This is why revert is the preferred rollback method.

../_images/gitlog_revert.png

Fig 20

Now you can push your reverted file

git push

Important

Please use the Cleanup section below if you want to do the Reset lab

Reset

Reset is a simple way to “rollback” to a previous commit. The down side of reset, it will remove all the commit history back to the restoral point. We will create a local repository and practice this.

mkdir  resetlab
cd resetlab
git init
vim example

Add this line to the newly created file

This is line one

:wq
git add
git commit
git log --oneline
../_images/gitlog.png

Fig 21

Now let’s edit the file example and add another line

vim example

Add this line to the example file:

This is line two

And now let’s save our file with the new changes, then add to staging and commit

:wq
git add example
git commit -m "two"
git log --oneline
../_images/gitlog2.png

Fig 22

Now let’s edit the file again and add another line

vim example

This is line three

And now let’s save our file with the new changes, then add to staging and commit

:wq
git add example
git commit -m "three"
git log --oneline
../_images/gitlog3.png

Fig 23

Now let’s rollback to our second commit. Using Fig 6 as a reference we will issue the command git reset --hard <hash> with the hash of our second commit

../_images/gitreset1.png

Fig 24

Now the git HEAD has been moved to our second commit and we have completed a rollback of our file. As you can see though, all commits prior are now removed.

../_images/gitlog_reset.png

Fig 25

Running the command cat example we can now see the third line has been removed.

In order to push this change to our remote, you must enable Allow Force Push as Gitlab will set this branch as protected, but again this is not a best practice method.

Under Settings select Repository, then by Protected Branches click Expand

../_images/protectedbranch1.png

Fig 26

Now you can toggle Allow force push

../_images/protectedbranch3.png

Fig 27

git push -f

Cleanup

If you want to remove a remote repository to do the reset section here are the steps

You will need to remove the files from the resetlab directory

rm -fr .git
rm -fr example

And delete the repository from Gitlab

Go to Settings and then General

../_images/deletegitrepo1.png

Fig 28

Scroll to bottom and find Advanced and click Expand

../_images/deletegitrepo2.png

Fig 29

Now click Delete project

../_images/deletegitrepo3.png

Fig 30

Return to Reset lab