strange behavior of branch remote HEAD

Sometimes I'm getting message that some branch is ahead of origin by # commits. Actually, I did not create those commits and they are pulled from origin.

$> git pull origin develop
Enter passphrase for key '###': 
remote: Counting objects: 7, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 2), reused 0 (delta 0)
Unpacking objects: 100% (4/4), done.
From ssh://git.#############
 * branch            develop    -> FETCH_HEAD
Updating bbe5577..71907bc
Fast-forward
 ############### |    1 -
 1 files changed, 0 insertions(+), 1 deletions(-)

And

$> git st
# On branch develop
# Your branch is ahead of 'origin/develop' by 1 commit.

tig's output:

{date} {author} [develop] {message1}
{date} {author} [origin/develop] {message2}

So, if I'm understanding all right, origin's HEAD is my local HEAD (or something like that) backed by 1. But nobody fix that origin's HEAD to -1 state.

Why did it happened?

Then I push to that branch and get "everything up-to date"

$> git push origin develop
Enter passphrase for key '###': 
Everything up-to-date

Can someone explain what's going on with that branch?

Upvotes: 3

Views: 671

Answers (1)

Joel
Joel

Reputation: 2966

The short answer is to use git fetch origin followed by git merge origin/develop from your local develop branch

git pull is like a git fetch followed by a git merge on your local branch develop. origin/develop in your current repository won't match your local develop branch though because your local copy is one commit ahead from your origin/develop HEAD. The call to git push origin develop updates your local origin/develop when git realizes the change is already on the remote git repo.

Here's what you see locally on your machine with no changes:

macbook:test joel$ git push origin master
Everything up-to-date
macbook:test joel$ git status
# On branch master
nothing to commit (working directory clean)
macbook:test joel$

On another machine another user makes a change, commits it, and pushes it to origin/master:

otherPerson-pc:test bob$ git status
# On branch master
nothing to commit (working directory clean)
otherPerson-pc:test bob$ echo "ddd" >> newfile.txt 
otherPerson-pc:test bob$ git add newfile.txt 
otherPerson-pc:test bob$ git commit --message "added to newfile in master remotely"
[master d14b77e] added to newfile in master remotely
 1 files changed, 1 insertions(+), 0 deletions(-)
otherPerson-pc:test bob$ git push origin master
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 314 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /Users/joel/Desktop/test/test.git
   2cfa7d4..d14b77e  master -> master
otherPerson-pc:test bob$

Back on my local copy I haven't made a change. I pull from origin master to get changes from others and appear one commit ahead!:

macbook:test joel$ git status
# On branch master
nothing to commit (working directory clean)
macbook:test joel$ git pull origin master
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /Users/joel/Desktop/test/test
 * branch            master     -> FETCH_HEAD
Updating 2cfa7d4..d14b77e
Fast-forward
 newfile.txt |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

macbook:test joel$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
nothing to commit (working directory clean)
macbook:test joel$

My local master has changes applied from the remote's master but my copy of origin/master is 1 commit behind so git says I'm one commit ahead locally even though I'm up to date. The git push origin master updates origin/master locally and the message goes away.

You can avoid this by using git fetch and git merge instead of git pull

The other developer might do this again:

otherPerson-pc:test bob$ git status
# On branch master
nothing to commit (working directory clean)
otherPerson-pc:test bob$ echo "gggg" >> newfile.txt 
otherPerson-pc:test bob$ git add newfile.txt 
otherPerson-pc:test bob$ git commit --message "added gggg newfile in master remotely"
[master 677d031] added eeee newfile in master remotely
 1 files changed, 1 insertions(+), 0 deletions(-)
otherPerson-pc:test bob$ git push origin master
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 321 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /Users/joel/Desktop/test/test.git
   d14b77e..677d031  master -> master
otherPerson-pc:test bob$

Locally I'll git fetch all of origin and then manually merge origin/master to my copy of merge.

macbook:test joel$ git status
# On branch master
nothing to commit (working directory clean)


macbook:test joel$ git fetch origin 
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /Users/joel/Desktop/test/test
   5e335fa..2dae61c  master     -> origin/master

macbook:test joel$ git status
# On branch master
# Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
#
nothing to commit (working directory clean)

macbook:test joel$ git merge origin/master
Updating 5e335fa..2dae61c
Fast-forward
 newfile.txt |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

macbook:test joel$ git status
# On branch master
nothing to commit (working directory clean)
macbook:test joel$

I usually do a fetch and then merge because you can check for conflicts before you you merge using git diff.

Upvotes: 2

Related Questions