Git Undo Merge: The Final Guide
Tags: git, codenewbie, beginners, tutorial
Published: 2021-08-25 08:48:51

![Git Undo Merge](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5vk5tncsobn2wdioa7i2.png)
## Introduction
You can undo a [Git](https://git-scm.com/) merge using the `git reset –merge` command. This command changes all files that are different between your current repository and a particular commit. There is no “git undo merge” command but the git reset command works well to undo a merge.

## How to Undo a Git Merge
Have you just merged two branches that you’re not ready to merge? Not to worry, Git has a solution for you. Developers merge branches all the time that should not be branched, so you’re definitely not alone in experiencing this issue.

In this tutorial, we’re going to talk about git merges and how to undo a git merge. We’ll walk through an example of two approaches you can use to undo a git merge. Let’s begin!

## Git Merges
Git relies heavily on branches. These are used to maintain separate lines of development inside a project. Branches allow you to work on multiple different versions of a repository at once. Merging combines two branches into one.

You can merge a branch into another branch whenever you are ready. This means that you can develop a feature or a bug fix on a separate branch. Then, you can make it part of your main project later.

## Git Undo Merge
To undo a git merge, you need to find the commit ID of your last commit. Then, you need to use the git reset command to reset your repository to its state in that commit. There is no “git revert merge” command.

The steps to revert a merge, in order, are:

`git log` OR `git reflog` (to find the last commit ID)
`git reset –merge` (to revert to the commit you specify)

Say we have accidentally merged two branches that should not be merged. We can undo these changes.

## Undo Merge Git Example
### Find the Commit ID
To start, we need to find out the commit ID of the commit before our merge on our remote repository. You can do this using *git reflog*:

```
git reflog
```
Let’s run this command on a Git repository:

```
ac7188c HEAD@{6}: commit: feat: Push example code for innerText and innerHTML tutorial
a9fdeb5 HEAD@{7}: commit (initial): feat: Merge dev-fix-7 into master
```
This command tells us that the last commit has the hash `a9fdeb5`.

Alternatively, you can use the `git log` command. But, the `reflog` command returns an output that is easier to read. This is why we opted to use `reflog` instead of the `log` command.

## Revert to the Commit
We can use this hash to revert the merge commit with the git reset –merge command:

```
git reset --merge a9fdeb5
```
This command resets our repository to the state it was at in the `a9fdeb5` commit on the master branch.

The `–merge` flag resets an index and updates all the files that are different between the current state of your repository and the HEAD.

This flag does not reset files with changes that have not been added to a commit. This makes it safer than using the oft-recommended `git reset –hard` command.

Once we have reset our repository, we can make the relevant changes to our code. Then, we can push them to a remote branch using the `git push` command.

## The HEAD Shorthand
The Git `HEAD` keyword refers to the latest commit in your repository. You can use the Git HEAD shorthand to undo a merge:
```
git reset --merge HEAD~1
```
This command reverts our repository to the last commit. `HEAD` refers to the current state of your repository; `HEAD~1` is the last commit in your repository.

## Additional Notes
If you need to push the branch to the remote, and you have already pushed the merge, you need to use:

```
git push -f origin myBranch 
```

to let Git force the push and overwrite the history of the branch.
**PAY ATTENTION**: this can be dangerous if `myBranch` was already fetched by others in their own repo. 


## Conclusion
The git reset command undoes a merge. The –merge flag resets a repository but keeps files to which changes have been made that have not been added to your repository.

______________________________________________________

*For More "The Final Guide" see the [Index Page](https://dev.to/zigrazor/the-final-guide-index-4954)*