James Raitsev
James Raitsev

Reputation: 96531

On diverged GIT branches

I have several questions on the following sequence of events.

There are 2 developers working on this code .. First and foremost, what causes branches to diverge on a first place?

11:05:08 ~/myApp $ git status
# On branch Dev
# Your branch and 'origin/Dev' have diverged,
# and have 1 and 3 different commit(s) each, respectively.
#
nothing to commit (working directory clean)
11:10:39 ~/myApp $ git push origin Dev:Dev
To ssh://git@mygitserver/myApp-web.git
 ! [rejected]        Dev -> Dev (non-fast-forward)

error: failed to push some refs to 'ssh://git@mygitserver/myApp-web.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

As Git suggested, i tried to do a pull from remote/Dev to local Dev, only to see:

11:10:51 ~/myApp $ git pull origin Dev:Dev
From ssh://mygitserver/myApp-web
 ! [rejected]        Dev        -> Dev  (non-fast-forward)

Git pull worked however. Why did git pull worked and git pull origin Dev:Dev failed?

11:13:05 ~/myApp $ git pull
Merge made by recursive.
 WebContent/BL/a.jsp               |   14 +++++-------
 WebContent/RJ/b.jsp               |    3 +-
 .../RJ/c.jsp                      |   22 ++++++++++----------
 WebContent/RJ/d.jsp               |   14 ++++++------
 WebContent/TM/e.jsp               |   12 ++++------
 5 files changed, 31 insertions(+), 34 deletions(-)

Subsequent git status and git push origin Dev:Dev worked without adventures.

Upvotes: 1

Views: 2419

Answers (2)

knittl
knittl

Reputation: 265864

Branches diverge, when changes happened on the remote AND on your local repositories, think of it as implicit branches: two (or more) commits with the same parent commit. Someone pushed new commits to the repository, while you have committed locally without synchronizing (merge or rebase)

As for git pull origin Dev:Dev not working:

git pulls manpage describes its usage as:

git pull [options] [<repository> [<refspec>...]]

the refspec part tells git which (remote) refs it should merge into local refs. As such, it only allows fast-forward updates, to prevent you from losing history.

You probably meant to update your remote tracking branch, and not your local one:

git pull origin dev:refs/remotes/origin/dev

A short-cut notation for your use case exists though:

git pull origin dev:

git pull without arguments will update all tracking branches for the configured remote (usually origin) and merges the first refspec into the current branch.

Upvotes: 1

Dmitry Ovsyanko
Dmitry Ovsyanko

Reputation: 1416

To visualize the divergence, you just had to compare git log Dev on both machines.

Upvotes: 1

Related Questions