How can I quickly check which Git branch is the newest?

For example, if there are 4 branches on git:

branch1
branch2* (current branch)
branch3 (newest commits here)
master (oldest)

My questions:

  • How to check if my current branch matches the last from the git command line? If not, which branch is the most modern? I mean, which branch contains the latest commits?

  • How can I list all the commits in this git repository (in any branches) that are newer than my branch without disconnecting from the current branch?

+4
source share
2 answers

Change . In this GitHub repo you will find the appropriate shell scripts.

Which branch is the newest?

[...] which branch is the most modern? I mean, which branch contains the latest commits?

, ( , , ).

git for-each-ref ; - , .

Command

, ...

  • () .

    git for-each-ref --format='%(refname:short)' \
                     refs/heads
    
  • , ( -):

    git for-each-ref --sort='-committerdate'     \
                     --format='%(refname:short)' \
                     refs/heads
    
  • , , :

    git for-each-ref --count=1                   \
                     --sort='-committerdate'     \
                     --format='%(refname:short)' \
                     refs/heads
    

, , () ( ) : , . !

( newest-branch ) :

git config --global alias.newest-branch "for-each-ref --count=1 --sort='-committerdate' --format='%(refname:short)' refs/heads"

Git -project repo:

$ git branch
  maint
* master
  next
  pu
$ git newest-branch
pu

# Now print the list of all branches and the committer date of their tips
$ git for-each-ref --sort='-committerdate' \
                   --format="%(refname:short)%09%(committerdate)"
                   refs/heads
pu      Tue Mar 17 16:26:47 2015 -0700
next    Tue Mar 17 16:14:11 2015 -0700
master  Tue Mar 17 16:05:12 2015 -0700
maint   Fri Mar 13 22:57:25 2015 -0700

pu - . !


?

, , newest-branch; , , - . ( , ).

Command

()

$ git symbolic-ref --short HEAD
master

, yes, , no .

if [ $(git newest-branch) = $(git symbolic-ref --short HEAD) ]; then
    printf "yes\n"
else
    printf "no\n"
fi

! ( isnewest):

git config --global alias.isnewest '! if [ $(git newest-branch) = $(git rev-parse --abbrev-ref HEAD) ]; then printf "yes\n"; else printf "no\n"; fi'

( , , pu .)

$ git branch
  maint
* master
  next
  pu
$ git isnewest 
no
$ git checkout pu
Switched to branch 'pu'
Your branch is up-to-date with 'origin/pu'.
$ git isnewest
yes
$ git checkout maint
Switched to branch 'maint'
Your branch is up-to-date with 'origin/maint'.
$ git is-newest-branch 
no

, (•_•) ( •_•)>⌐■-■ (⌐■_■)


git ( ), , , ?

- , . DAG, git , . , :

git log --branches HEAD..

( --branches , refs/heads.) , , --oneline, --decorate ..

+5

:

git log --decorate --all --oneline --graph

: man git log

  • --decorate: .
  • --all: , refs refs/ <commit>.
  • --oneline: --pretty=oneline --abbrev-commit, .
  • --graph: .

, git fetch :

git fetch origin

, :

gitk

.

+3

All Articles