20

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.

casperOne
  • 73,706
  • 19
  • 184
  • 253
  • For Git being such a simple tool, this is the most confusing part about it. I have the exact same problem as you. I never ever commit to my master branch. I always branch off and make my changes. When I do a 'git pull origin master' I get that it is 200 commits behind. Why? I never touched it. I wish they would clear this up. – Jeff Jan 21 '14 at 21:05

4 Answers4

35

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
  • 127,052
  • 24
  • 157
  • 134
  • Thanks araqnid. Just curious, what does git log origin/master.. do in this case? –  May 11 '09 at 23:36
  • 3
    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 May 12 '09 at 00:07
  • I keep seeing answers to git questions that say "just type this" without an explanation of what all the little flags and options mean. Thank you for writing that explanatory comment. – Tyler Nov 25 '10 at 04:42
4

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
  • 165,801
  • 69
  • 278
  • 343
0

You might need to make sure your origin/master is up to date with your repo version of master. Try running:

git fetch origin master

so your local origin/master is the same as the repo's master. Then when you run

git pull origin master

you should get an accurate display of how many commits ahead your local master is. After that you can run araqnid's command to see which commits actually are different.

Community
  • 1
  • 1
Lucy Bain
  • 2,496
  • 7
  • 30
  • 45
0

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
  • 1,228
  • 8
  • 18