Learn Version Control with Git
)}

git reset

git reset can also undo changes in your repository, but it does it in a destructive way. While Reverting creates a new commit by reversing changes. On the other hand, Resetting erases commits. It can be very dangerous to reset commits as it might cause changes to be completely removed.

There are three important flags for the git reset command: --mixed, --soft, --hard.

--mixed

git reset --mixed allows you to remove a commit, but still make it accessible in the working directory. That means that it's still available for you to modify, but will need to be staged and committed again.

let's add some content to our file:

echo "This is a git reset example" >> project-1.txt

let's stage and commit our new changes:

git add .
git commit -m "adding a reset example"

let's view our commit history:

git log

let's reset back to our previous commit using the --mixed flag:

git reset --mixed b88e516e9e3c57b5101cf611db6f8f4d9fd6a55a
>> Unstaged changes after reset:
   M	      project-1.txt

The SHA we used here belongs to the commit where we would like to reverse our changes too. We can go as far back as you like.

The output above tells us that the commit has been reset and all the changes have been unstaged. Let's check our history:

git log

Our commit is gone, and if you open the file, the changes from that commit should still be there. We can verify this with git status:

git status

Let's commit our changes again for the next example:

git add .
git commit -m "adding a reset example 2"

--soft

git reset --soft is similar to git reset --mixed , however, instead of unstaging the changes to the working directory, it will keep them staged.

Let's try it out:

git reset --soft b88e516e9e3c57b5101cf611db6f8f4d9fd6a55a

Our commit should be gone now:

git log

We can check the status of the reset to see what happened to our changes:

git status

Our commit was successfully removed and our changes have been staged.

Let's commit our changes again for the next example:

git add .
git commit -m "adding a reset example 3"

--hard

git reset --hard is much more destructive than the other two commands. This flag will roll you back to the specified commit but will send the reset commits to the trash.

Let's try it out:

git reset --hard b88e516e9e3c57b5101cf611db6f8f4d9fd6a55a

Our commit should be gone now:

git log

We can check the status of the reset to see what happened to our changes:

git status

Our working directory is clean. Our latest commit has been completely removed.

Feedback Summary
5.0
3 students
5

100%
4

0%
3

0%
2

0%
1

0%
Written Reviews
There are no written reviews yet.