How to Abort Git Merge

Ashok Chapagai Feb 02, 2024
How to Abort Git Merge

For example, Mario wants to pull the latest changes to his repository named save-princess from the remote repository. Mario uses git pull origin branch1 but is thrown a merge conflict error because the locally changed file was modified in the remote repository save-princess by Luigi.

Merge conflict also occurs, if for example, Luigi edited the same file on the same line as Mario, or Luigi deleted the file Mario was editing, or even if Mario tries to add the same file with the same name that is already added by Luigi. Git will show a message if merge conflict has occurred during git pull . In simple words, merge conflict occurs when two changes affect the same file on the same line.

Below are the few ways to resolve merge conflicts,

Resolve Merge Conflict by Stashing Changes in Git

One way to resolve conflicts is to stash the changes made locally and then again restore the stashed changes after the merge conflict is gone. That can be achieved through:

  • Stash the changes
    git stash
    
  • Fetch changes from remote repository.
    git fetch
    
    git pull origin save-princess
    

    Here, save-princess is the name of the branch present in both local and remote repositories.

  • Pop the stashed changes
    git stash pop
    

If any problems arise after the merge, you can always revert the present changes before merging.

To abort the merge, you can use:

git merge --abort

The above command is similar to reset, which resets our commit to the present version before the merge.

git reset --hard
Ashok Chapagai avatar Ashok Chapagai avatar

Ashok is an avid learner and senior software engineer with a keen interest in cyber security. He loves articulating his experience with words to wider audience.

LinkedIn GitHub

Related Article - Git Merge