9

I did a git commit -am followed immediately by git stash and got the message
No local changes to save

When I run git status I get
Your branch is ahead of 'origin/master' by 3 commits.

Is this right?

I was working on some stuff and made some commits but haven't pushed the changes. Now I want to 'stash' them and go back to a clean version (my latest pushed changes - don't know how to refer to this)

How do I stash my work that I haven't pushed yet and revert to the latest pushed master branch?

Phil
  • 2,176
  • 10
  • 33
  • 56
  • 1
    It is right, you are tree commits ahead, not behind. What do you mean saying `How do I stash my work and go to the master`? Are you not in the `master` branch? Show the output `git branch` – neshkeev Sep 08 '15 at 15:15
  • 3
    What are you asking? What are you trying to do, exactly? – jub0bs Sep 08 '15 at 15:16
  • 1
    I think what you've done is confused branches and stashes. You want to put your local changes in the stash. If you want to go back to where origin/master is while saving your work, you can `git checkout origin/master`. You can also push your commits to master with `git push origin master` or you can save your work in a new branch, then check out origin/master. – Andy Sep 08 '15 at 15:18
  • @Andy, yes you are right, I thought `git stash` would stash committed changes that weren't pushed yet. I need to remove the commits and then stash? – Phil Sep 08 '15 at 15:23
  • @zaratustra Thanks, I updated my question. I am trying to save my work and start clean. I have run `git commit` but not `git push` – Phil Sep 08 '15 at 15:31
  • The key question is do you really want to "save" your current work? Or are you happy to lose it and get your latest pushed changes? – codemonkey Sep 08 '15 at 15:35
  • @codemonkey I want to save it in case I need to go back to it. – Phil Sep 08 '15 at 15:36
  • 1
    Have a look at this answer http://stackoverflow.com/questions/19859486/un-commit-last-un-pushed-git-commit-without-losing-the-changes which explains how to do a soft reset, which is what you want. – codemonkey Sep 08 '15 at 15:37

3 Answers3

16

If I understand if right, you need:

1) Save your local changes.

git add -A
git stash

2) Backup your commits

git branch my_master_backup

3) Reset your HEAD back to origin/master

git reset --hard origin/master

4) Do some work that you want to to with the "clean version"

5) Restore changes that you have made with your commits. Fix conflicts if any.

git merge my_master_backup

6) Drop the backup branch. If you can't delete it means that you haven't fully merged your backup branch and the master.

git branch -d my_master_backup

6) Restore your local changes from the step 1:

git stash pop

PS: you can avoid creating a backup branch and use reflog. When you want to restore your commits you need to execute git merge HEAD@{X} where X is the number of the desired commit from reflog.

neshkeev
  • 6,280
  • 3
  • 26
  • 47
  • I don't believe the OP has anything to actually stash. And he wants to retain his work, so a hard reset wouldn't give him what he wants. – Thomas Stringer Sep 08 '15 at 15:43
  • I suggested the OP to stash their local (uncommited) changes and make a backup branch before hard reseting. So those changes/commits will always be available to them. – neshkeev Sep 08 '15 at 15:45
6

Thanks for the help everyone. What I did was
git reset --soft origin/master git stash save 'stashing my unfinished changes'

This undid all my commits but left all my changed files. I was then able to stash the changes since they weren't committed yet. It automatically reset my local files to origin/master after I stashed.

Phil
  • 2,176
  • 10
  • 33
  • 56
1

From your question and the comments, it sounds like what you want to do is go back to the remote repository's master branch commit, but retain the changes you have made since you have done this.

What you can do is just create a new local branch where the remote (assuming it is called origin) branch is (assuming you are working with master).

$ git checkout -b original-master origin/master

This will create a new branch called original-master (and check out this branch, bringing your working directory back to where origin/master is pointing to) that will be at the same commit as the remote origin's master branch.

At this point, the 3 commits you have from origin/master to (local) master will be preserved, but you can start a different branch of development with a parent of origin/master (by having a new branch called original-master).

Thomas Stringer
  • 5,682
  • 3
  • 24
  • 40
  • I find it a little confusing, because the remote master now tracks two different local branches (`master` and `original-master`) – neshkeev Sep 08 '15 at 15:49
  • @zaratustra The OP is looking to preserve his changes on his local `master` branch. Without having these commits "orphaned" I don't see any way around that. As for the naming, OP can change the names of the branches if he so chooses. – Thomas Stringer Sep 08 '15 at 15:51
  • Good idea, let's see which solution fits better:) – neshkeev Sep 08 '15 at 15:55