
Let’s say you’re working locally and have made a couple of commits, only to realize then that you’ve been working from a bad premise or have injected an anti-pattern into your work. With all that being said, Git reset hard is actually a very handy tool to quickly go back to a previous state of your project. Performing a Git reset –hard when working on a shared branch with commits that can be accessed by other contributors can cause issues with your Git history. Git reset hard should be used with extreme caution and only for local changes you’re sure you want to eliminate. Unlike with the Git reset soft and mixed, Git reset hard carries some danger, as it will automatically discard all the changes made between HEAD and the specified commit.
#GIT RESET ORIGIN HOW TO#
In this article, we will go over the various options available with the Git reset command and how to perform them in the command line and GitKraken Client: Git can completely discard all of those changes, as you will see with the Git reset hard command, or it can preserve those changes in various states, as is the case with both the Git reset soft and Git reset mixed commands. Unlike checkout however, Git reset will effectively uncommit all the changes between your starting state and the specified commit. Git reset is similar to Git checkout as it allows you to move the HEAD to any previous commit in your history. One of the most powerful tools Git provides to change to a prior state is the Git reset command. Git provides several methods of going back to a prior commit and working from that point. Sometimes, when working with a Git repository, you realize that you don’t want to share, or even keep your changes, and you need a way to undo them, like undoing your last commit, for example. How do you perform an interactive rebase?.How do you Git push to a remote branch?.How do you create a GitHub pull request?.Can you cherry pick multiple commits in Git?.Can you cherry pick from another repository in Git?.How do you set an upstream branch in Git?.How do you delete a remote branch in Git?.How do you delete a local branch in Git?.How do you checkout a remote branch in Git?.Optionally, we can also clean up the untracked changes by executing the following command. Please note, this will remove all the local changes.Īll the changes/commits we have in the remote branch in the remote repository are present in the local branch of the local repository.

The second command, get reset, reset the current HEAD to the one on the remote branch. The first command, get fetch, downloads the objects and refs from origin the origin is an alias created by git for the remote URL of the remote repository. We will run the following command to reset the local branch viz. Additionally, git checkout removes the untracked files. We now will do a checkout to the local branch viz.

master used to track the remote branch with the same name in the remote repository. Using git reset to Reset the Local Branch to One in Remote Repository

We will now illustrate this with an example. Thus, we need to do a reset of the local branch in the local repository and synchronize with the one in the remote repository.

It usually happens when we are working in a collaborative environment and some other team member has done some changes (fixes, feature development, etc.) and pushed those into the remote branch (ex. Also, we need to fetch the changes that are in the remote repository. Typically, we have a local branch with some changes that are no longer required or out of date. Optionally, we can discard any untracked changes in the local repository. This tutorial will show how to reset a local branch in the local repository in git to be like the branch on the remote repository.
