Git and GitHub: How to Hard Reset Local and Remote Repository
Tags: git, github, beginners
Published: 2020-06-22 05:06:07

###Song of the Week
{% soundcloud https://soundcloud.com/potsupotsu/just-friends-2 %}

##Introduction
Before we go over how to do this, let's go over when you should and should not do this.

**Don't do a hard reset if you've already pushed the commits in question to a shared remote repository.** If another developer pulls and starts basing their work on the commits that the reset removes from the commit history, you're going to create huge problems.

**A hard reset can be done if you're the only one using the local and remote repository, or if the reset removes commits that have not yet been pushed to a shared remote.** In this case, you're the only one affected by the hard reset, so it's relatively safe to do. Just make sure you want drop the commits in question before you do the reset.

Also, just as a quick reminder, `git reset --hard <commit-hash>` will move the HEAD pointer back to the commit hash that was given, the Stage Index and the Working Directory will be reset back to the way they were at that commit, and all commits after the commit that was given will be dropped from the commit history. This means that **any uncommitted work in the Staging Index or Work Directory will be lost.**
  
##How to Hard Reset the Local Repository
To simplify the example, I created a repository with a README in it, and for each commit, I added a line that matches the commit message. So, after four commits I ended up with a README that looked like this:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mo3rvl0k90swovu9rnzi.png)

To reset your local repository, first you need to know which commit you want to reset to, and what the hash is for that commit. One way to do this is to run `git log`.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6wghcz23q4px9qhybv2i.png)

Let's say that after looking through this commit history you wanted to reset to the "second commit". Then, you would write down or copy the commit hash for that commit and run:

`git reset --hard <commit-hash>`

Based on the example, that command would look like this:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/i9a35h95dqo139xd7jgv.png)

After running the hard reset command, the Working Directory now looks like this:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kplejmwpx14nl21jrmi4.png)

You can see from `git log` and the Working Directory that we have rolled back to the second commit and dropped the third and fourth commits.

##How to Hard Reset the Remote Repository
Performing a hard reset of the remote repository is quite easy. After you've reset the local repository, simply do a force push with the follow git command:

`git push -f <remote> <branch-name>`

In the example we used above, that git command would look like this:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/tr8eyx6irw2d2gnuqc1t.png)

##Takeaways
* Only do a hard reset if you are the only using the remote repository, or if you haven't yet pushed the commits you're going to drop.
* Find the commit hash of the commit you want to reset to with `git log`.
* Perform the local hard reset by running `git reset --hard <commit-hash>`.
* To hard reset the remote repository, first hard reset the local and then run `git push -f <remote> <branch-name>`

##References
[Cover Image](https://www.sciencealert.com/if-a-nuclear-bomb-explodes-these-are-the-emergency-supplies-you-should-have)
[Reverting to Specifi Commit Based on Commit ID with Git - stackoverflow.com](https://stackoverflow.com/questions/3639115/reverting-to-a-specific-commit-based-on-commit-id-with-git?lq=1)
[Resetting Remote to a Certain Commit - stackoverflow.com](https://stackoverflow.com/questions/5816688/resetting-remote-to-a-certain-commit)
[Git Reset - Atlassian.com](https://www.atlassian.com/git/tutorials/undoing-changes/git-reset)
[Resetting, Checking Out, and Reverting - Atlassian.com](https://www.atlassian.com/git/tutorials/resetting-checking-out-and-reverting)