views:

3100

answers:

4

Hi all,

I work 100% with a branch that I made off of the master branch. Now that a colleague has pushed back to origin/master, I tried to push those changes into my personal branch. When I do a 'git checkout master' followed by a 'git status' I get the following:

# Your branch is ahead of 'origin/master' by 2 commits.

How is a branch I never commit to ahead by 2 commits? What's the best way to find out what commits they are and essentially undo them? I don't wish to push anything back to origin/master as that might cause unknown conflicts.

Thank you for your solution(s).

A: 

See http://stackoverflow.com/questions/277077/why-is-git-telling-me-your-branch-is-ahead-of-origin-master-by-11-commits-and/277186 ?

Chealion
Hi Chealion. I saw that post, but it still doesn't answer my question as to how I'm ahead by 2 commits when I'm not working on that particular branch.
Even though you weren't working on that branch the steps in the link I gave should have at least shown why it was saying you were two commits ahead. araqnid answered the rest quite well however.
Chealion
+8  A: 

To see the commits you have in HEAD that are not in origin/master:

git log origin/master..

To blow them away and make your HEAD the same as origin/master:

git reset --hard origin/master

How did you push the changes to your own repository? I notice you mentioned "push"... Is origin a central repo? Your colleague's repo? I suspect what you actually wanted to do was pull your colleague's changes in, either directly or from a central staging point, rather than pushing. It might simply be that the 2 changes you have ahead of origin/master are in fact your colleague's changes, but the origin/master tracking branch is stale.

araqnid
Thanks araqnid. Just curious, what does git log origin/master.. do in this case?
With git commands that take a *range* of revisions, A..B is shorthand for "B ^A" or "B --not A". "A.." is itself shorthand for "HEAD --not A".git loads revision B, then walks back through the revision graph, stopping when it encounters commits that are reachable from A (often A itself).In the case of a simple revision history where the graph is just a line, this simplifies to showing the commits after A up to and including B.
araqnid
+1 Thank you. <3
Adam Backstrom
A: 

Have you done any git rebase'ing on the branch you are working on?

If not you could try copying your branch (git checkout -b testrebase) and issuing a git rebase master to see if that works. This will unravel all commits you made relative to master and then try to apply them back (makes the history make sense, basically). If it doesn't work just delete testrebase.

MighMoS
+4  A: 

You're working on a branch of master, and you've made two commits which are not in origin/master.

The message # Your branch is ahead of 'origin/master' by 2 commits. is saying:

# Your branch 'mybranch' has two commits not in 'origin/master'

Imagining git users SVN-like revision numbers, you're branch has commits 1, 2, 3, 4, 5 - but origin/master only has 1, 2, 3. So the revision history looks something like the following crappy ASCII diagram..

your branch                                   -- [commit 4]--[commit 5]
                                             /                    /\ HEAD
master --[commit 1]--[commit 2]--[commit 3]-/
                                       /\ origin/master

To display the last two commits in the log, you can do..

git log HEAD..HEAD~2
dbr