Git

How to Resolve Git Stash Conflict Without Commit?

Multiple developers can work on a similar source code file and apply modifications to it. They can add changes immediately to the Git staging index. Additionally, developers can temporarily keep the modifications or changes through stashing operation. For this purpose, use the β€œgit stash apply” command when it is required to apply hold changes.

In such a situation, sometimes, developers encounter unmerged conflict because of multiple changes simultaneously and not merged. They can resolve this conflict without committing.

This guide will discuss the easiest solution to the Git stash conflict without commit.

How to Resolve Git Stash Conflict Without Commit?

Most of the time, Git stash unmerged conflicts encountered by developers when they are working on a similar project file and adding changes multiple times. To resolve this particular conflict, developers need to stage the modifications to the tracking index by executing the β€œgit add <filename>” command without committing.

Let’s move forward, check out when the conflict occurs, and resolve it.

Step 1: Switch to Git Repository

Execute the β€œcd” command and navigate to the desired Git local directory:

$ cd "C:\Users\nazma\Git\repo_16"

Step 2: Generate and Update File

Then, generate and update a new file by running the β€œecho” command:

$ echo "My text file" >> "file1.txt"

Step 3: Track File

Next, track a newly created file into the index through the following command:

$ git add file1.txt

Step 4: Push Changes to Git Repository

After that, run the provided command and push changes to the Git repository:

$ git commit -m "repo's first file added"

Step 5: Update File

Use the β€œecho” command and update the previously created file:

$ echo "text file" >> "file1.txt"

Step 6: Git Stash

Now, add all modifications to the temporary memory by executing the β€œgit stash” command:

$ git stash

Step 7: Modify Existing File

Similarly, update the existing file through the below-given command:

$ echo "file" >> "file1.txt"

Step 8: Staged Add Changes

Then, execute the β€œgit add .” command to add changes to the index:

$ git add .

Step 9: Apply Hold Changes

Next, apply the temporary keep changes on the Git repository through to the β€œgit stash apply” command:

$ git stash apply

According to the below-provided output, the hold changes are not applied, and a conflict occurred:

Note: Now, to resolve the above-stated conflict, the user needs to add the changes to the Git repository by specifying the particular file name.

Step 10: Add Changes

Now, execute the β€œgit add” command along with the particular file name in which changes are made:

$ git add file1.txt

Step 11: Apply Git Stash

Finally, apply the hold changes that are saved in the stash through the following command:

$ git stash apply

It can be observed that, the stash changes have been applied successfully:

That’s was all about resolving the stash conflict without commit in Git.

Conclusion

The unmerged conflict is encountered when multiple changes are performed by developers when they are working on a similar file. To resolve the Git stash conflict, they need to stage the added changes to the index without committing by running the β€œgit add <filename>” command. This guide illustrated the easiest solution to the Git stash conflict without commit.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.