1

Please note, before indicating the question as a duplicate, that I have read the answer here but, with the solution proposed, I will delete all the uncommitted changes.

Problem:

I have developed new features using a dedicated branch. In the meanwhile, the master branch continued to be developed by others. I decided to do a git pull in order to rebase my code to the new one. However, there were conflicts: somebody has improved the code on the remote repo.

Question:

  1. I have some uncommitted changes on my local repo. How can I bring the repo to the old state with the old uncommitted changes?
  2. Did I already lose forever all the uncommitted changes?
LeGEC
  • 29,595
  • 2
  • 37
  • 78
Leos313
  • 3,729
  • 4
  • 29
  • 61

2 Answers2

2

So you have the following situation:

  1. You have a branch locally
  2. You are working on this branch, meaning that you have uncommited changes in your working folder
  3. You have commits on this branch locally, that you haven't pushed
  4. Someone else have made commits on this branch, which they have pushed (which means your local branch is out of date)
  5. You did a git pull which attempted to merge the commits from the upstream with your local commits, but failed and left you with a merge conflict
  6. You now have a working folder that has both the changes you had not yet committed, as well as merge conflict changes from the failed merge

The thing is, if step 5 would've ended up touching files you had changed locally but not yet committed, the merge would never have been attempted. Let me rewrite that, since git pull attempted to merge, the changes from the commits that it tried to merge does not involve the files you have uncommitted changes to.

So all you have to do is this:

  1. Make a complete backup of your working folder and repository, and experiment on the copy (I don't want to be responsible for adding more problems)
  2. Execute this command:

    git merge --abort
    

This should restore your branch to its previous state, including the working folder. Any files you had uncommitted changes to before git pull should be left as-is, since the merge never involved them anyway.

Lasse V. Karlsen
  • 350,178
  • 94
  • 582
  • 779
2

If the rebase did start although you had local modifications, chances are you have the "autostash" option turned on.

Otherwise, the rebase would not have started at all.

Check the value of this config parameter :

git config --get rebase.autostash

You should see true


You can run git rebase --abort to cancel the rebase.

You should see a line :

Applied autostash.

which indicates that your unstaged changes have been restored.

LeGEC
  • 29,595
  • 2
  • 37
  • 78