How to do git reset?

Hi There,

Looks like you are facing challenges and you want to reset your git repository.

Great. Let me help you here.

There are three scenarios where you would require to reset -

  1. Remove everything which is uncommitted right now
  2. Move back to a particular commit which is not pushed yet
  3. Start the module from scratch

Scenario 1

Let’s start the easy step first which is scenario 2.

Step 1 - First Check if you have anything uncommitted

git status

Step 2 - Everything that is uncommitted you want to delete it

git reset --hard HEAD

Step 3 - Everything uncommitted is gone. How to confirm it? Do git status again

git status

OnPaste.20200320-123546

Step 4 - Compare the first git status and current git status

git reset --hard HEAD
this command remove everything which is uncommitted and you will go back to the stage where the last committed codebase is present. Cool.

Scenario 2

Let’s say you have 5 commits present in your repository. Like

Commit 1, Commit 2, Commit 3 ……

Step 1 - Check the last commits log messages
If you want to find out last 5 commits then use this command
git log --oneline -5

OnPaste.20200320-123954

If you want to find out last 10 commits then use this command.

git log --oneline -10

You can check the commit messages in GitLab as well. How to check the commit history in GitLab? Good question, here is the way -

Step 2 - Open gitlab.crio.do and Open your repository. Go to commits of that repository

You can see the, there is a commit id and commit message.

Step 3 - If you can get back to commit to module 1 then use this command.

git reset --hard {commit id for module 1}

Now, your local repository is at module 1 commit and remote GitLab repository is also at module 1 commit.

Scenario 3

Let’s say you have just completed module 1. You have just pulled codebase for module 2. And you faced a merge conflict there. You have resolved it. You did something after that and if you want to start module 2 from scratch, which means the point from where you pulled the code for module 2 itself.

Step 1 - First, let’s understand where we are currently. For this check the commit logs by using this command.

git log --oneline -5

OnPaste.20200320-124602

You can see the above commits logs.

Commit 1 - module 1

Commit 2 - module 2

Commit 3 - module 3

Commit 4 - Successfully completed module 1

Commit 5 - wrong something

OnPaste.20200320-124602

Amongst about 5 commits, you know 2rd commit is the commit when you completed the module 2 successfully.

Step 2 - Open the GitLab’s commit history and see what’s there.

In GitLab, if you have only Commit 3 present and commit 4 and commit 5 are not present then simply do following

git reset --hard {commit of module 3}

OnPaste.20200320-124811

And then, execute the setup instructions for the module again which are present in the taskboard.

If you have commit 4 and commit 5 present in the GitLab commit history then, unfortunately, you can not go back to start of the previous module ( It’s not impossible, it is possible but this is not in the scope of this ME, it’s slightly advance which we do not recommend. )

This is a FAQ candidate query.

Closing this topic as your issue is resolved by the mentor. If it is still not resolved, Kindly un-mark the accepted solution or create a new topic and post this question as a reference link in the description of the new topic.

Closing this topic as your issue is resolved by the mentor. If it is still not resolved, Kindly un-mark the accepted solution or create a new topic and post this question as a reference link in the description of the new topic.