127

I am new to Git and I am using EGit eclipse plugin to commit.

I modified few files and I stashed the changes, then I did git pull in command line which pulled up all the latest commits. Then I did Apply stashed changes from EGit. Now it applied my changes and the changes which pulled from last commit of stashed files went out. I am not sure why it didn't ask me about merge conflicts and overwrote my changes and lost previous commits changes.

How to get those changes?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Lolly
  • 34,250
  • 42
  • 115
  • 150
  • It doesn't sound plausible. The stash apply either brings unconflicted changes or causes merge conflicts. I think you forgot to mention something. Also, you could repeat your steps, most probably your stash is still in the list, use `git stash list` to show it. – kan Sep 18 '12 at 11:49

3 Answers3

289

When you have changes on your working copy, from command line do:

git stash 

This will stash your changes and clear your status report

git pull

This will pull changes from upstream branch. Make sure it says fast-forward in the report. If it doesn't, you are probably doing an unintended merge

git stash pop

This will apply stashed changes back to working copy and remove the changes from stash unless you have conflicts. In the case of conflict, they will stay in stash so you can start over if needed.

if you need to see what is in your stash

git stash list
yilmazhuseyin
  • 6,442
  • 4
  • 34
  • 38
  • 1
    @java_newbie, If you want to do this with stash and pull no, there is no way. But you can achieve same result with rebase. Command 'git rebase origin/' should do the same thing. – yilmazhuseyin Sep 24 '14 at 13:41
  • 8
    I think this will do it : `git stash && git pull && git stash pop` – Fabrice Kabongo Sep 04 '15 at 12:17
  • @yilmazhuseyin What do you mean by 'same result' Can you given an anecdote if you can? Did you mean `git rebase origin/master` will produce same result as `git stash; git pull; git stash pop` ? – nehem Apr 19 '16 at 06:45
  • 1
    Does `--autostash` always work? For example, if I have untracked files that are already partially in Git? – qräbnö Jun 04 '19 at 17:22
  • @qräbnö No, it just doing like `git stash && git pull && git stash pop` if it has a conflict, it will auto stash for you. `Applying autostash resulted in conflicts. Your changes are safe in the stash. You can run "git stash pop" or "git stash drop" at any time.` – foxiris Dec 12 '19 at 03:39
13

Instead use:

git pull --autostash
leonheess
  • 16,068
  • 14
  • 77
  • 112
3

That's the first thing to do when you edit your .gitconfig

[pull]
  autostash = true
; and also
[rebase]
  autostash = true
[merge]
  autostash = true

Petur Subev
  • 19,983
  • 3
  • 52
  • 68