Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in DevOps and Agile by (19.4k points)

I want to know how to resolve a conflicting git stash pop without adding all modifications to a commit (just like "git stash pop" without conflict does).

My current approach is very uncool because I do it this way:

git stash pop -> CONFLICT

git stash drop

[resolve conflict]

[add conflict files]

git reset HEAD <all files that are in commit-mode>

[Update] A way to reproduce it:

mkdir foo; cd foo; git init

echo "1" > one

echo "2" > two

git add -A; git commit -m "first"

echo "1.1" > one

echo "2.1" > two

git stash

echo "2.2" > two

git commit -a -m "second"

echo "Only this file would stay in HEAD without the conflict" > third

git add third

git stash pop

git status

2016-06-27: Added a new file called 'third' to the example to show that workarounds like the solution from scy only work for empty HEADs but don't fix the initial problem that the HEAD doesn't have the same content like for a git stash pop without a conflict.

1 Answer

0 votes
by (27.5k points)

Let us start with talking about the default behavior. There are two ways of marking conflicts as resolved: 

git add: It marks the conflicts as resolved, but keeps files in the index. 

git reset: It marks the conflicts as resolved and removes files from the index

Adding files to the index after a conflict is resolved is on purpose. This way you can differentiate the changes from the previous stash and changes you made after the conflict was resolved. If you don't like it, you can always use git reset to remove everything from the index.

The following solution is also suggested by the Git,  try to execute git status in the repository where you have a conflict:

Unmerged paths:

  (use "git reset HEAD <file>..." to unstage)

  (use "git add <file>..." to mark resolution)

Let us do what Git suggests without performing any useless commits:

  1. Resolve the conflict(s) manually or using some merge tool.
  2. Then use git reset to mark conflict(s) as resolved and unstage the changes. Also you can execute it without any parameters and Git will remove everything from the index. You don't have to execute git add before.
  3. Finally, remove the stash with git stash drop, because Git doesn't do that on conflict.

Translated to the command-line:

$ git stash pop

# ...resolve conflict(s)

$ git reset

$ git stash drop

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.2k users

Browse Categories

...