Easy Tutorial
❮ Git Tag Git Workflow ❯

git reset Command

Git Basic Operations


The git reset command is used to revert to a previous version, allowing you to specify a particular commit to revert to.

The syntax for the git reset command is as follows:

git reset [--soft | --mixed | --hard] [HEAD]

--mixed is the default and does not need to be specified. It resets the staging area to match the last commit, while keeping the working directory unchanged.

git reset [HEAD]

Examples:

$ git reset HEAD^            # Revert all content to the previous version  
$ git reset HEAD^ hello.php  # Revert the hello.php file to the previous version  
$ git reset 052e             # Revert to the specified version

The --soft parameter is used to revert to a specific version:

git reset --soft HEAD

Example:

$ git reset --soft HEAD~3   # Revert to the version three commits ago

The --hard parameter discards all uncommitted changes in the working directory, resets both the staging area and the working directory to the last commit, and deletes all previous commit information:

git reset --hard HEAD

Example:

$ git reset --hard HEAD~3  # Revert to the version three commits ago  
$ git reset –hard bae128  # Revert to a specific version and delete all previous commit information. 
$ git reset --hard origin/master    # Reset the local state to match the remote repository

Note: Use the --hard parameter with caution, as it deletes all information before the revert point.

HEAD Explanation:

-

HEAD represents the current version.

-

HEAD^ represents the previous version.

-

HEAD^^ represents the version before the previous one.

-

HEAD^^^ represents the version three commits ago.

-

And so on...

-

HEAD~0 represents the current version.

-

HEAD~1 represents the previous version.

-

HEAD^2 represents the version before the previous one.

-

HEAD^3 represents the version three commits ago.

-

And so on...

git reset HEAD

The git reset HEAD command is used to unstage content.

Let's modify the README file with the following content:

# tutorialpro Git Test
# tutorialpro.org

And modify the hello.php file to:

<?php
echo 'tutorialpro.org: www.tutorialpro.org';
echo 'tutorialpro.org: www.tutorialpro.org';
echo 'tutorialpro.org: www.tutorialpro.org';
?>

Now, after both files have been modified and staged, we want to unstage one of them. The operation is as follows:

$ git status -s
    M README
    M hello.php
$ git add .
$ git status -s
M  README
M  hello.php
$ git reset HEAD hello.php 
Unstaged changes after reset:
M    hello.php
$ git status -s
M  README
    M hello.php

Now, when you execute git commit, only the changes to the README file will be committed, and hello.php will not be included.

$ git commit -m 'Modify'
[master f50cfda] Modify
    1 file changed, 1 insertion(+)
$ git status -s
    M hello.php

You can see that the modifications to the hello.php file were not committed.

At this point, we can use the following command to commit the changes to hello.php:

$ git commit -am 'Modify hello.php file'
[master 760f74d] Modify hello.php file
    1 file changed, 1 insertion(+)
$ git status
On branch master
nothing to commit, working directory clean

In summary, execute git reset HEAD to unstage changes that were previously added with git add, but do not want to be included in the next commit snapshot.


Git Basic Operations

❮ Git Tag Git Workflow ❯