2

I'm trying to figure out whether I can pull.

If my branch is based on a remote branch than he has remote tracking branch, so I can pull.

If my branch is based on a commit, it doesn't have a remote tracking branch thus pull would fail.

What I managed to find is that when using the command git branch -vv:

  1. If there's a remote tracking it will be shown in square brackets. Example: * origin/before-yo b0b97cf [remotes/origin/before-yo] Move to subfolder
  2. If the branch is based on a commit, there won't be any remote ref indication. Example: * 2fbe2ab473fe8f7aea2a36642aea1dc7d36add51 2fbe2ab Advance counter

Is there a better way to understand if there's a remote tracking branch connected to my current working branch?

Also, might git branch -vv not work in some cases?

Thanks.

AlikElzin-kilaka
  • 34,335
  • 35
  • 194
  • 277
  • 2
    Are you actually asking if your branch has a remote tracking branch? `git rev-parse --abbrev-ref --symbolic-full-name @{u}` should do that, it will display the remote tracking branch if there is one, or give an error message if not, "fatal: no upstream configured for branch 'XYZ'". – Lasse V. Karlsen Jun 18 '18 at 13:25
  • @LasseVågsætherKarlsen - Thanks. Want to add it as an answer? – AlikElzin-kilaka Jun 18 '18 at 14:22
  • What version of Git are you using? Modern ones say `* (HEAD detached at )` for the detached HEAD case that yours seems to be showing as `* 2fbe2ab...`, your case-2 above. – torek Jun 18 '18 at 19:24
  • @torek - git version `2.7.4` – AlikElzin-kilaka Jun 19 '18 at 04:57
  • Ah, well, 2.7.4 also prints `* (HEAD detached at ...)` so the only way you could get `* 2fbe2ab473fe8f7aea2a36642aea1dc7d36add51` is by creating a branch actually *named* `2fbe2ab473fe8f7aea2a36642aea1dc7d36add51`, e.g., `git checkout -b 2fbe2ab473fe8f7aea2a36642aea1dc7d36add51`. While Git allows this, it's not a good idea, as it now becomes ambiguous whether that name means the hash ID, or the branch name. – torek Jun 19 '18 at 06:26

1 Answers1

0

Here's a script which shows what you really want: after git fetch, it shows the difference between local and remote branches, thus you will know what you have to do (pull, push, rebase, etc.). Put the directory of it to somewhere on your PATH and you can use it as git fetch-log.

#!/bin/sh
# git-fetch-log: Ultimate git fetch-log
#
# It shows the diferences of `local...remote` if both side exists and there
# are differences. Useful to find out what you have to do: `merge`, `rebase`,
# `push`, etc. The arguments passed to log.
#
# Place it somewhere on Your `$PATH` and execute by the `git fetch-log`
# command.
#
#                               EXAMPLE OUTPUT
# ============================================================================
#
# ==== branch [behind 1]
#
# > commit 652b883 (origin/branch)
# | Author: BimbaLaszlo <bimbalaszlo@gmail.com>
# | Date:   2016-03-10 09:11:11 +0100
# |
# |     Commit on remote
# |
# o commit 2304667 (branch)
#   Author: BimbaLaszlo <bimbalaszlo@gmail.com>
#   Date:   2015-08-28 13:21:13 +0200
#
#       Commit on local
#
# ==== master [ahead 1]
#
# < commit 280ccf8 (master)
# | Author: BimbaLaszlo <bimbalaszlo@gmail.com>
# | Date:   2016-03-25 21:42:55 +0100
# |
# |     Commit on local
# |
# o commit 2369465 (origin/master, origin/HEAD)
#   Author: BimbaLaszlo <bimbalaszlo@gmail.com>
#   Date:   2016-03-10 09:02:52 +0100
#
#       Commit on remote
#
# ==== test [ahead 1, behind 1]
#
# < commit 83a3161 (test)
# | Author: BimbaLaszlo <bimbalaszlo@gmail.com>
# | Date:   2016-03-25 22:50:00 +0100
# |
# |     Diverged from remote
# |
# | > commit 4aafec7 (origin/test)
# |/  Author: BimbaLaszlo <bimbalaszlo@gmail.com>
# |   Date:   2016-03-14 10:34:28 +0100
# |
# |       Pushed remote
# |
# o commit 0fccef3
#   Author: BimbaLaszlo <bimbalaszlo@gmail.com>
#   Date:   2015-09-03 10:33:39 +0200
#
#       Last common commit

  fmt='ref=%(refname:short); up=%(upstream:short); t=%(upstream:track); ts=%(upstream:trackshort);'
  git for-each-ref --shell --format="$fmt" refs/heads | \
  while read entry; do
    eval "$entry"
    if test "z$ts" != 'z' && test "z$ts" != 'z='; then
      echo -e "\n==== $ref $t\n"
      git --no-pager log --graph --left-right --decorate --abbrev-commit --date-order --boundary $@ $ref...$up
    fi
  done

https://gitlab.com/bimlas/home/blob/3fb207cdfe57593a78abe30150a3bc40b242e26a/linux/home/.gitconfig_files/custom_commands/git-fetch-log

bimlas
  • 2,359
  • 1
  • 21
  • 29