. Advertisement .
..3..
. Advertisement .
..4..
The aim of the git reset command is to move the current HEAD to the prefered commit. This post will introduce you to three common options to do this. Let’s see how to reset HEAD in Git.
What Is Git HEAD?
HEAD refers to the last commit in your current check-out branch. It indicates the current branch’s starting point. More specifically, this command is a moving pointer of the current commit.
How To Reset HEAD In Git
There are three options to reset files on Git: soft, hard, and mixed reset files.
Option 1: Soft reset files
This command in Git is used to reset the HEAD without affecting your working directory and index. You can use it to reset a local branch’s head.
$ git reset --soft HEAD (going back to HEAD)
$ git reset --soft HEAD^ (going back to the commit before HEAD)
$ git reset --soft HEAD~1 (equivalent to "^")
$ git reset --soft HEAD~2 (going back two commits before HEAD)
For example, let’s look at the following feature branch:
$ git log --oneline --graph
* 802a2ab (HEAD -> feature, origin/feature) feature commit
* 7a9ad7f (origin/master, master) version 2 commit
* 98a14be Version 2 commit
* 53a7dcf Version 1.0 commit
* 0a9e448 added files
* bd6903f first commit
To move the HEAD function, run the following command:
$ git reset --soft HEAD^ (or HEAD~1)
Let’s see how the branch has changed:
$ git status
On branch feature
Your branch is behind 'origin/feature' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: file-feature
Option 2: Hard Reset Files
This option is used to reset the files of the staging area or the index and the working directory. Yet, you will receive untracked files in your directory.
First, use the git log command to have a whole understanding of your current branch’s commits:
$ git log --oneline --graph
* 802a2ab (HEAD -> feature, origin/feature) feature commit
* 7a9ad7f (origin/master, master) version 2 commit
* 98a14be Version 2 commit
* 53a7dcf Version 1.0 commit
* 0a9e448 added files
* bd6903f first commit
Here is the code to reset to HEAD:
$ git reset --hard HEAD^
HEAD is now at 7a9ad7f version 2 commit
If you want to have reset to the commit before the HEAD:
$ git log --oneline --graph
* 7a9ad7f (HEAD -> feature, origin/master, master) version 2 commit
* 98a14be Version 2 commit
* 53a7dcf Version 1.0 commit
* 0a9e448 added files
* bd6903f first commit
Option 3: Mixed Reset Files
The mixed option is default in Git. That’s you will run the mixed model when writing the Git reset. This option moves the HEAD and updates the staging area.
You can use it to undo content by git commits. The syntax to use it is:
$ git reset --mixed or $ git reset
Conclusion
This tutorial has helped you know how to reset HEAD in Git. There are three options to choose and you should use the most suitable one for your instance.
Leave a comment