Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git stash `No local changes` but git status `ahead of origin/master by 3 commits`

Tags:

git

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?

like image 875
Phil Avatar asked Sep 08 '15 15:09

Phil


People also ask

How do I fix my branch is ahead of origin master by three commits?

This message from git means that you have made three commits in your local repo, and have not published them to the master repository. The command to run for that is git push {local branch name} {remote branch name} .

Why is my master branch ahead?

The message you are seeing (your branch is ahead by one commit) means your native repository has one commit that hasn't been pushed yet. In other words: add and commit are local operations, push, pull and fetch are operations that interact with a remote.

What does your branch is ahead of origin master by 1 commit mean?

It means that you have some commits in your branch that weren't pushed to origin. To keep your local branch in sync with origin, you need to push your code to that frequently.


2 Answers

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.

like image 130
neshkeev Avatar answered Sep 20 '22 18:09

neshkeev


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.

like image 31
Phil Avatar answered Sep 19 '22 18:09

Phil