Git Tutorial 23 – Git Stash Apply – How To Solve Merge Conflict

Introduction

As a part of GIT Tutorials End To End, we will learn about Solving merge conflict during Git Stash apply command in this post.

Prerequisite posts

I will expect that you are aware of the basic concepts and commands of GIT. But if you are a beginner in GIT then I strongly recommend you to refer GIT Basic Commands and Concepts section on my blog first.

Did you know that I have started a YouTube channel as well and I need your support to make it successful. Please do watch content then comment, like, share, and obviously subscribe.

You must learn about the Git stash command from here.

Merge conflict during stash apply

Yes, we can get merge conflict while applying stash. Let’s see it in action.

Just initialized a git repository below-

Let’s create a file, add some content to it and commit it. Please note we must have a commit before we can use git stash the first time.

Now we have a clean working tree. We will perform some steps so that we will experience a merge conflict.

  1. Append contents in FileA.
  2. Stash changes.
  3. Append contents in FileA again
  4. Apply stash changes

All above steps are shown below-

We have uncommitted changes above so when we tried to apply git stash it gave an error shown above. In fact, when we try to apply a stash, a merge happens. Let’s commit changes and retry applying stash.

Now we see the merge conflict which is the topic of this post. The FileA has a text “Added line 3” at the second line which was added recently. While in stashed change, the FileA has a text “Added line 2” at the second line. Now when git tries to merge stashed changes with the current state of FileA, it confused which line to be kept at the second line or to keep both or to remove both. When git confuses then it will surrender and ask you to resolve it manually. Remember there is no magic to solve merge conflicts. We need to put effort manually which we will see now.

Let’s see what is the current content of FileA now.

Conflict changes will be shown as above. Contents are shown from current commit between <<<<<<< and ======= while contents shown between ======= and >>>>>>> are from stashed changes. Now you decide what contents you want to keep. You can keep either changes or both.

If you are good in Linux command to edit a file then use it or simply open the file in explorer and resolve.

Before

After – I just kept both changes. It really depends on what changes you want to keep.

We have successfully applied stash changes by solving merge conflicts.

Summary

  1. Appling stash is a king of merge operation.
  2. Git stash apply can have merge conflicts.
  3. Merge conflict happens when more than one developers changes the same file at same line. There might be many other reasons.
  4. Resolving merge colflicts may be complex if it is huge and you are not in sync with other’s changes.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe to my YouTube channel.
#ThanksForReading
#HappyLearning

Leave a Reply

Your email address will not be published. Required fields are marked *