Resetting HEAD in Git
Git helps us in many ways when working with shared repositories.
We can perform many functions using Git as we can create a new branch, merge a branch, delete a branch as per our requirements. These operations can be done using multiple Git commands.
The command git reset
is a composite and multi-source function used to undo changes. The command can be used with three main options: --soft
, --mixed
, --hard
.
These three parameters correspond to Git's management mechanism, called the commit tree ( HEAD
).
HEAD
Indicates the last commit when Git switched branches. In addition, we can also say Git HEAD
that points to the last commit of the current branch.
When we switch branches or create a new branch, Git HEAD
we transfer the latest commit to our local branch. More precisely, HEAD
a pointer that always points to the current commit, which may or may not belong to the current branch.
Git HEAD reset
When we have saved our changes to our desired repository, we should relax because we have already git reset
returned to our completed changes using the command, which will bring our current branch back to its original location before the command was run.
option HEAD
to publish a new branch. We can say that git reset-hard HEAD
what can be done is to clear out all the uncommitted changes we have made.
git reset
Commands can be combined with other commands:
-
Transfer to
HEAD
the content pointed by the branch. - Make it look like a tree object.
- Looks like the current working folder.
Moreover, there are two different categories git reset
.
Git Head Reset--soft
This Git command will reset HEAD
. But our index and working directory will not be affected in any way.
We can use the Git reset command option to reset the head of our local branch:
git reset --soft
Git Head Reset--mixed
git reset
The mix command will change the head position to the specified commit and further remove the changes from the staging area. Here is an example of undoing the changes.
So if we run the Git reset HEAD command, it will HEAD
move back to the first parent commit again. The syntax for the above case is as follows:
git reset --mixed or $ git reset
Git Head Reset--hard
This command can be a hindrance for us. We can use it only if we know exactly how to use it.
This was a problem for us when we used git reset-hard HEAD
revert to the last commit. Luckily, we developers have a better solution to correct it.
Be aware that git reset --hard
git status is a threatening command. It can smash all our uncommitted changes. We should check it first and make sure the Git status output is clean before processing it.
If we want to remove all commits in both the index and the staging area (we need to undo our last commit and the last commit before that), this command will remove the commits stored in the index. We can use --hard
the Git reset command with the -r option:
git reset --hard HEAD~2
This command will remove any commits from the index and staging area. It will also remove the commits from the history.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Git installation and establishment of local warehouse service
Publish Date:2025/04/05 Views:89 Category:Git
-
Git is a distributed version control system: the client does not only extract the latest version of the file snapshot, but also completely mirrors the original code repository. It has the following advantages: a. Since every extraction oper
git remote operation——multiple remote repositories for one project
Publish Date:2025/04/05 Views:131 Category:Git
-
Multiple remote repositories for a git project In our git project, the command to operate the remote repository information is $ git remote # 查看当前所有的远程仓库的名称 $ git remote -v # 查看远程仓库的名称和远程仓
Git cherry pick command usage
Publish Date:2025/04/05 Views:190 Category:Git
-
git cherry-pick is a powerful command that allows us to select an arbitrary Git commit by reference and attach it to the HEAD of the current working branch. Cherry picking is the act of picking a commit from one branch and applying it to an
Comparison between Git merge and Git rebase
Publish Date:2025/04/05 Views:171 Category:Git
-
The git rebase command may seem like Git wizardry to beginners, but if used carefully, it can actually make life easier for your development team. In this article, we compare git rebase with the related git merge command and identify all th
How to fix Git error Error: src refspec master does not match any
Publish Date:2025/04/05 Views:124 Category:Git
-
When using Git, we may encounter the error "src refspace master does not match any". Here's what the error means and how to fix it. What does src refspec master does not match any Mean in Git mean? We may encounter this error when we try to
Rebase local branch when pulling changes from remote repository branch in Git
Publish Date:2025/04/05 Views:144 Category:Git
-
This article will cover the basics of rebasing your local branch when pulling changes from a remote repository branch in Git. We use the version control system Git to track changes made to files. We commit changes in a local branch in our l
Undo Git Stash
Publish Date:2025/04/04 Views:187 Category:Git
-
This article explains how to make and save changes to a repository. Git allows you to save changes locally and push them to a server when needed. In Git, we don't use the term save , but commit . We use git add , git commit , and git stash
View a list of cache entries in Git
Publish Date:2025/04/04 Views:59 Category:Git
-
We often need to pause our work and focus on something else in our development environment. Therefore, we may need to temporarily save our current work and focus on a different one. We may want to resume our original work later. git stash T
Git stores specific files
Publish Date:2025/04/04 Views:115 Category:Git
-
This article will cover storing changes to only specific files in Git. In Git, when we make some changes in our working tree, we may have some changes which may or may not be staged in our local repo. We may now wish to save these changes f