如何重置Git Head?

Git version control tool provides reset functionality in order to clear all uncommitted changes and revert to the last commit. This action is called “reset head”. In this tutorial, we will learn how to Git reset head with different features and examples.

Git版本控制工具提供了重置功能,以清除所有未提交的更改并恢复到最后一次提交。 该动作称为“复位头”。 在本教程中,我们将学习如何使用不同的功能和示例来重置Git头。

列出上次提交的更改 (List Changes From The Last Commit)

Before resetting to the head we will list current changes in our active branch. We will use git status command which will list all tracked or untracked changes.

在重置为头部之前,我们将在活动分支中列出当前更改。 我们将使用git status命令,它将列出所有已跟踪或未跟踪的更改。

$ git status
List Changes From The Last Commit
List Changes From The Last Commit
列出上次提交的更改

硬重置到头部(Hard Reset To Head)

Now we will hard reset to the recent commit. The current branch most recent commit is named as HEAD. So this will be called “Hard Reset” to the Head. We will use git reset with the --hard option.

现在,我们将硬重置为最近的提交。 当前分支的最新提交被命名为HEAD 。 因此,这将称为“硬重置”。 我们将使用git reset--hard选项。

$ git reset --hard HEAD^
Hard Reset To Head
Hard Reset To Head
硬重置到头部

We can see that HEAD is now at 1ba011937 message means the current branch is hard reset to the latest commit. When we list tracked or untracked changes we can see that all tracked changes are removed and only untracked changes remain.

我们可以看到HEAD is now at 1ba011937消息,这意味着当前分支被硬重置为最新提交。 当我们列出跟踪或未跟踪的更改时,我们可以看到所有跟踪的更改都被删除,仅保留了未跟踪的更改。

软复位至头部 (Soft Reset To Head)

Soft reset will reset to the Head but resetting to the head will not delete tracked files. We will provide the --soft option to the reset command.

软重置将重置为磁头,但是重置为磁头将不会删除跟踪的文件。 我们将为reset命令提供--soft选项。

$ git reset --soft
Soft Reset To Head
Soft Reset To Head
软复位至头部

We can see that soft reset does not remove the files extension.c  and pass.c.

我们可以看到,软重置不会删除文件extension.cpass.c

重设头 (Reset Before To Head)

Up to now, we have reset to the last recent commit called HEAD. We can specify reset before the last commit or HEAD with the ^ .  For example HEAD^2 means reset to 2 commits before HEAD.

到现在为止,我们已重置为最近的最新提交,称为HEAD。 我们可以在最后一次提交之前指定reset,或者使用^指定HEAD。 例如HEAD^2意味着在HEAD之前重置为2次提交。

$ git reset --soft HEAD^2

重置为ID提交 (Reset To The Commit with ID)

We can also reset to the specified commit ID. This can be useful if we want to reset multiple intermediate commits. We need to provide the commit ID we want to reset. So first we will list commits by their ID’s.

我们还可以重置为指定的提交ID。 如果我们想重置多个中间提交,这将很有用。 我们需要提供要重置的提交ID。 因此,首先我们将按其ID列出提交。

$ git log
Reset To The Commit with ID
Reset To The Commit with ID
重置为ID提交

Then we will use commit id like below.

然后,我们将使用如下的提交ID。

$ git reset 350bbe0597d

通过合并重置为头部 (Reset To Head By Merging)

If we want to reset the index and update the files in the working tree that are different between commit and HEAD we can merge them. We will use --merge option like below.

如果我们想重置索引并更新工作树中的commit和HEAD之间的文件,则可以合并它们。 我们将使用--merge选项,如下所示。

$ git reset --merge

重置单个文件 (Reset A Single File)

We can also reset a single file to the HEAD. we will just provide the file name with the double dash -- option like below. In this example, we will reset the file named portlist.h.

我们还可以将单个文件重置为HEAD。 我们将为文件名提供双破折号--选项,如下所示。 在此示例中,我们将重置名为portlist.h的文件。

$ git reset -- portlist.h
LEARN MORE  How To Clone Git Branch Command Tutorial with Examples?
了解更多信息如何使用示例克隆Git分支命令教程?

翻译自: https://www.poftut.com/how-to-reset-git-head/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值