How to use Git Reset to HEAD
- Tram Ho
When working on a multi-member project, team members can create branches, add, edit, and delete files in the project. Then make commits to git when the code is done. However, in some cases, you may find that the changes you’ve made aren’t going so well.
You have modified some files, added and removed a lot of lines from your files, but you want to go back. Because of that, you want to “revert the changes” what you did and go back to the files you had originally.
This technique is called “reset to HEAD” and it is quite a powerful tool for developers to use.
In this article, I will show you how to use reset to HEAD on Git.
1. Git Hard Reset to HEAD
When resetting files on Git, we have 2 options: hard reset files and soft reset files.
In this section, I will describe how you can hard reset files on Git.
To factory reset files to HEAD on Git, use the “git reset” command with the “–hard” option and specify the HEAD.
1 2 3 4 5 | $ git reset --hard HEAD (going back to HEAD) $ git reset --hard HEAD^ (going back to the commit before HEAD) $ git reset --hard HEAD~1 (equivalent to "^") $ git reset --hard HEAD~2 (going back two commits before HEAD) |
Example of Hard Reset
To understand “factory reset” use cases, check out some quick examples below.
When trying to reset files, the first command you want to launch is the “git log” command.
Using the “git log” command, you will be able to get an overview of your current Git branches and its commits.
1 2 3 4 5 6 7 | $ git log --oneline –graph * 941c450 (HEAD -> develop, origin/develop) three commit * 8cfada9 second commit * 17c751f firt commit * 9fdeae3 (origin/main, origin/HEAD, main) Initial commit |
As you can see the commit with HEAD 941c450 is the last commit in branch develop
To factory reset before HEAD use “git reset” with “–hard” option and specify HEAD ^.
1 2 3 4 | $ git reset --hard HEAD^ HEAD is now at 8cfada9 second commit |
As you can see, the HEAD of the release branch is now pointing to the second commit: we’ve basically reset to the commit before HEAD.
1 2 3 4 5 6 7 | $ git log --oneline –graph * 8cfada9 (HEAD -> develop) second commit * 17c751f firt commit * 9fdeae3 (origin/main, origin/HEAD, main) Initial commit |
Factory reset for HEAD
To undo a factory reset on Git, use the “git reset” command with the “–hard” option and specify “HEAD @ {1}”
Using the example we used before, that will give us the following result
1 2 3 4 5 6 7 8 9 10 11 | $ git reset --hard HEAD@{1} HEAD is now at 941c450 three commit $ git log --oneline –graph * 941c450 (HEAD -> develop, origin/develop) three commit * 8cfada9 second commit * 17c751f firt commit * 9fdeae3 (origin/main, origin/HEAD, main) Initial commit |
2.Git Soft Reset to HEAD
To restore files to HEAD on Git, use the “git reset” command with the “–soft” option and specify the HEAD
1 2 3 4 5 6 | $ 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) |
In contrast to hard reset files, soft reset files will not change the working directory and index.
Therefore, changes made between the original HEAD and the current HEAD will be implemented.
Going back to the example we took earlier, let’s take a quick look at the “develop” branch.
1 2 3 4 5 6 7 8 | $ git log --oneline –graph * 941c450 (HEAD -> develop, origin/develop) three commit * 8cfada9 second commit * 17c751f firt commit * 9fdeae3 (origin/main, origin/HEAD, main) Initial commit |
To move HEAD to a previous commit, use the “git reset” command with the “–soft” option and specify “HEAD ^”
1 2 | $ git reset --soft HEAD^ (or HEAD~1) |
Translation results
1 2 3 4 5 6 7 | $ git status On branch develop Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: sample3.txt |
Combining commits using soft reset
A common use of the soft reset command is to combine many different commits into a single commit.
On your current branch, see all the commits that are currently made.
1 2 3 4 5 6 7 8 | $ git log --oneline –graph * 6039dc8 (HEAD -> develop) three commit * 8cfada9 second commit * 17c751f firt commit * 9fdeae3 (origin/main, origin/HEAD, main) Initial commit |
To combine the last three commits, move the HEAD using the “git reset” command with the “–soft” option.
1 2 3 4 5 6 7 8 9 10 11 | $ git reset --soft HEAD~3 $ git status On branch develop Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: sample1.txt new file: sample2.txt new file: sample3.txt |
Now that the commits are rolled back, commit the files with the “git commit” command.
1 2 3 4 5 6 7 | $ git commit -m "Combining commits using git reset" $ git log --oneline --graph * d88fc78 (HEAD -> develop) Combining commits using git reset * 9fdeae3 (origin/main, origin/HEAD, main) Initial commit |
Your commits are now combined in a single commit.
Note: In case you have made commits and pushed the code to the server, then I will execute the following commands in turn to update the commits on the server:
1 2 3 4 5 | 1. git reset --hard HEAD 2. git clean -f -d 3. git push -f <remote-name-origin> <branch-name-develop> |
Refer: https://devconnected.com/tag/git/
Hope this article will help you better understand how to use Git Reset HEAD!