229

I did a git pull from a shared git repository, but something went really wrong, after I tried a git revert. Here is the situation now:

$ git stash
Source/MediaStorageAndFileFormat/gdcmImageCodec.cxx: needs merge
Source/MediaStorageAndFileFormat/gdcmJPEGLSCodec.cxx: needs merge
Source/MediaStorageAndFileFormat/gdcmPNMCodec.cxx: needs merge
Testing/Source/DataStructureAndEncodingDefinition/Cxx/TestDS.cxx: needs merge
Utilities/socketxx/socket++/sockstream.cpp: needs merge
Source/MediaStorageAndFileFormat/gdcmImageCodec.cxx: needs merge
Source/MediaStorageAndFileFormat/gdcmJPEGLSCodec.cxx: needs merge
Source/MediaStorageAndFileFormat/gdcmPNMCodec.cxx: needs merge
Testing/Source/DataStructureAndEncodingDefinition/Cxx/TestDS.cxx: needs merge
Utilities/socketxx/socket++/sockstream.cpp: needs merge
Source/MediaStorageAndFileFormat/gdcmImageCodec.cxx: unmerged (2aafac967c35fa4e77c3086b83a3c102939ad168)
Source/MediaStorageAndFileFormat/gdcmImageCodec.cxx: unmerged (78cc95e8bae85bf8345a7793676e878e83df167b)
Source/MediaStorageAndFileFormat/gdcmImageCodec.cxx: unmerged (2524db713fbde0d7ebd86bfe2afc4b4d7d48db33)
Source/MediaStorageAndFileFormat/gdcmJPEGLSCodec.cxx: unmerged (4bb4ba78973091eaa854b03c6ce24e8f4af9e7cc)
Source/MediaStorageAndFileFormat/gdcmJPEGLSCodec.cxx: unmerged (ad0982b8b8b4c4fef23e69bbb639ca6d0cd98dd8)
Source/MediaStorageAndFileFormat/gdcmJPEGLSCodec.cxx: unmerged (4868371b7218c6e007fb6c582ad4ab226167a80a)
Source/MediaStorageAndFileFormat/gdcmPNMCodec.cxx: unmerged (f7a1b386b5b13b8fa8b6a31ce1258d2d5e5b13c5)
Source/MediaStorageAndFileFormat/gdcmPNMCodec.cxx: unmerged (6ce299c416fbb3bb60e11ef1e54962ffd3449a4c)
Source/MediaStorageAndFileFormat/gdcmPNMCodec.cxx: unmerged (75c8043a60a56a1130a34cdbd91d130bc9343c1c)
Testing/Source/DataStructureAndEncodingDefinition/Cxx/TestDS.cxx: unmerged (79c2843f2649ea9c87fa57662dafd899a5fa39ee)
...
fatal: git-write-tree: error building trees
Cannot save the current index state

Is there a way to reset all that ?

Thanks

malat
  • 12,152
  • 13
  • 89
  • 158

7 Answers7

605

Use

git reset --mixed

instead of git reset --hard. You will not lose any changes.

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
heracek
  • 7,251
  • 3
  • 15
  • 10
  • 30
    Note that the `--mixed` argument is also `git reset`'s default behavior, when not given an argument. [manpage](https://www.kernel.org/pub/software/scm/git/docs/git-reset.html) for reference. – Christopher Jan 28 '14 at 17:11
  • 65
    Valid answer, but would be nice to have an explanation for what happened and why this was necessary. – mmigdol Jun 20 '14 at 18:18
  • 1
    @mmigdol Yeah - I'd like to see an explanation of why `git reset` with the (default) `--mixed` option is appropriate and effective here also. According to [merge - Git unmerged path issue](https://stackoverflow.com/questions/3207029/git-unmerged-path-issue): **This will switch to HEAD, and tell git to forget any merge conflicts, and leave the working directory as is.** heracek, is that what's going on? – nealmcb Jul 17 '17 at 00:41
  • Doesn't work. I get `git: 'reset--mixed' is not a git command. See 'git --help'.` – writofmandamus Sep 29 '17 at 23:50
  • 1
    @writofmandamus Months late, but note the space between reset and the hyphens. – Mike Ortiz Jan 30 '18 at 18:19
  • 3
    FWIW I ran into this issue when I was doing `git stash`. Doing `git reset --mixed` resolved it. – mfaani Oct 02 '18 at 14:23
56

This worked for me:

Do

$ git status

And check if you have Unmerged paths

# Unmerged paths:
#   (use "git reset HEAD <file>..." to unstage)
#   (use "git add <file>..." to mark resolution)
#
#   both modified:      app/assets/images/logo.png
#   both modified:      app/models/laundry.rb

Fix them with git add to each of them and try git stash again.

git add app/assets/images/logo.png
David Rz Ayala
  • 2,165
  • 1
  • 20
  • 22
  • 7
    This worked for me. git reset --mixed may be better than world peace, but without some explanation of what it does, I ain't touching it. In my case, there was a collision in an earlier pull that I didn't notice. The stash failed because of the unresolved pull. – Ian Ollmann Nov 08 '16 at 01:16
  • 1
    I don't understand this strategy any better than `git reset --mixed`, but it worked, and seemed like a better fit for my case because I got the error after `git stash` rather than `git revert`. – Mars Dec 24 '17 at 19:38
  • An *unmerged* path means you have run ```git merge```, or the equivalent of git merge, and it tried to merge two different sets of changes to that file, but failed. – siddhantsomani Jun 19 '19 at 20:09
  • This worked for me, too, after attempting to do a ```git stash```. I had to do a ```git add ``` first, then the stash succeeded. – Aquarelle Jan 07 '21 at 20:39
11

To follow up on malat's response, you can avoid losing changes by creating a patch and reapply it at a later time.

git diff --no-prefix > patch.txt
patch -p0 < patch.txt

Store your patch outside the repository folder for safety.

afilina
  • 878
  • 1
  • 11
  • 25
  • This gave me: SDGL132d9f4b4:glitch-common dstromberg$ patch -p0 < /tmp/patch.txt patch: **** Only garbage was found in the patch input. – dstromberg Apr 07 '15 at 18:11
  • I think you should `mv` the patch back again into the directory where you created it from. 1. `git diff` 2. `mv patch.txt /tmp`3. `git stash` 4. `mv /tmp/patch.txt .` 5. `patch -p0` – yunzen Jun 16 '16 at 08:21
10

I used:

 git reset --hard

I lost some changes, but this is ok.

malat
  • 12,152
  • 13
  • 89
  • 158
  • 7
    Just a note: [`git revert`](http://www.kernel.org/pub/software/scm/git/docs/git-revert.html) attempts to merge changes from the past; you have correctly used `git reset` to simply rewind the clock. It is unfortunate that `git stash` does not work with merge conflicts. – Josh Lee Mar 30 '11 at 12:33
  • 2
    If you lose changes, this is not a fix ;) – Pedro Magalhães Apr 23 '18 at 05:58
5

maybe there are some unmerged paths in your git repository that you have to resolve before stashing.

Peter Oram
  • 6,213
  • 2
  • 27
  • 40
npeters
  • 69
  • 1
  • 1
0

This happened to me when trying to merge another branch. The merge failed with fatal: git-write-tree: error building trees and complained about a different file that had nothing to do with the merge. My branch then contained the files it had tried to merge, as uncommitted changes.

I cleared the changes it had attempted to merge, then removed the problem file and rebuilt the hash:

git reset --hard;

git rm --cache problem_file.txt;

git hash-object -w problem_file.txt;

The merge then worked.

BadHorsie
  • 14,135
  • 30
  • 117
  • 191
-1

This happened for me when I was trying to stash my changes, but then my changes had conflicts with my branch's current state.

So I did git reset --mixed and then resolved the git conflict and stashed again.

mfaani
  • 33,269
  • 19
  • 164
  • 293