Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

git reset command

git reset command

Git Basic OperationsGit Basic Operations


The git reset command is used to roll back versions and can specify a version to roll back to a certain commit.

The syntax of the git reset command is as follows:

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

--mixed is the default and can be omitted; it is used to reset the files in the staging area to be consistent with the last commit, while the working directory files remain unchanged.

git reset  [HEAD] 

Example:

$ git reset HEAD^            # 回退所有内容到上一个版本  

$ git reset HEAD^ hello.php  # 回退 hello.php 文件的版本到上一个版本  

$ git  reset  052e           # 回退到指定版本

--soft parameter is used to roll back to a certain version:

git reset --soft HEAD

Example:

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

--hard parameter discards all uncommitted changes in the working directory, returns both the staging area and working directory to the previous version, and deletes all previous commits:

git reset --hard HEAD

Example:

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

$ git reset –hard bae128  # 回退到某个版本回退点之前的所有信息。 

$ git reset --hard origin/master    # 将本地的状态回退到和远程的一样 

**Note:** Use with caution –-hard parameter, as it will delete all information before the rollback point.

HEAD description:

You can use ~number to represent

git reset HEAD

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

We first modify the README file, with the following content:



# Runoob Git Test

# Runoob Tutorial 

The hello.php file is modified to:





Now that both files have been modified and committed to the staging area, we want to unstage one of them 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 run git commit, only the changes to the README file will be committed, not hello.php.



$ git commit -m '修改'

[master f50cfda] 修改

    1 file changed, 1 insertion(+)

$ git status -s

    M hello.php

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

At this point, we can use the following command to commit the changes 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 content previously added by git add that you do not want to include in the next commit snapshot.


Git Basic OperationsGit Basic Operations