git reset command

Git Basic Operations


The git reset command is used to roll back versions, and you can specify a particular commit to roll back to.

The syntax format of the git reset command is as follows:

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

--mixedIt is the default, so this parameter can be omitted. It resets the files in the staging area to be consistent with the last commit, while the file contents in the working directory remain unchanged.

git reset  [HEAD] 

Example:

$ git reset HEAD^            # 回退所有内容到上一个版本  
$ git reset HEAD^ hello.php  # 回退 hello.php 文件的版本到上一个版本  
$ git  reset  052e           # 回退到指定版本

--softParameter used to roll back to a certain version:

git reset --soft HEAD

Example:

$ git reset --soft HEAD~3   # 回退上上上一个版本 

--hardThis parameter undoes all uncommitted modifications in the working directory, brings both the staging area and the working directory back to the previous version, and deletes all information from previous commits:

git reset --hard HEAD

Example:

$ git reset --hard HEAD~3  # 回退上上上一个版本  
$ git reset –hard bae128  # 回退到某个版本回退点之前的所有信息。 
$ git reset --hard origin/master    # 将本地的状态回退到和远程的一样 

Note:谨慎Usage–-hardThis parameter deletes all information before the rollback point.

HEAD Description:

  • HEAD represents the current version

  • HEAD^ is the previous version

  • HEAD^^ is the version two steps back

  • HEAD^^^ — the version three commits back

  • And so on...

Can be represented with ~number
  • HEAD~0 represents the current version

  • HEAD~1 is the previous version

  • HEAD^2 is the version two steps back

  • HEAD^3 is the version three steps back

  • And so on...

git reset HEAD

The git reset HEAD command is used to unstage cached content.

Let's first modify the README file, with the content as follows:

# Example Git 测试
# Example 

The hello.php file is modified to:

<?php
echo 'Example:www.example.com';
echo 'Example:www.example.com';
echo 'Example:www.example.com';
?>

Now that both files have been modified and added to the staging area, 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 if you execute git commit, only the changes to the README file will be committed, while hello.php will not be.

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

You can see that the modification to the hello.php file was not committed.

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

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

In short, execute git reset HEAD to unstage the files that were previously added with git add, but that you do not want to include in the next commit snapshot.



Git Basic Operations

other extensions