Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of final branches (which are final tips)

How can i get a list of branches which are only final branches (which are not parents of any other commits) - other words branches which are tips of commit tree (final nodes). I expected some option like --final or --tips in git branch command but there only --merged, --no-merged and --contains options.

like image 862
Perlover Avatar asked Feb 02 '13 15:02

Perlover


People also ask

How can I get a list of git branches ordered by most recent commit?

Use git branch --sort=-committerdate to display a list of all local branches and sort them based on the date of their last commit. Use arrow keys to navigate, press Q to exit.

Which command is used to see the last commit on each branch?

If you need to get latest commit on a branch through the REST api, try the "/commits" call with the "until" parameter set to the branch you are interested in, and limit=1 to get the single tip commit from the branch.

What is Reflog in git?

Git keeps track of updates to the tip of branches using a mechanism called reference logs, or "reflogs." Many Git commands accept a parameter for specifying a reference or "ref", which is a pointer to a commit. Common examples include: git checkout. git reset.

Which command is used to get the list of all the available branches?

To get a list of all branches from the remote, run this command: git pull.


1 Answers

There are some useful answers already but not giving what is asked for: a list or branches (to me that means branch names, not commit IDs).

Getting list of tip branch names

{
        echo "** Below is list of tip branches a.k.a. final nodes."
        echo "** These aren't reachable from any other branches."
        echo "** Removing them is potentially DANGEROUS: it would make some commit unreachable."
        TIPS=$( git rev-list --branches --children | grep -v ' ' | sort )
        { while read branchname commitid message
                do grep $commitid < <(echo $TIPS) -q && echo $branchname
                done
        } < <(git branch -v --no-abbrev | sed 's/\*//' )
        echo "** end of list"
}

Result looks like this:

** Below is list of tip branches a.k.a. final nodes.
** These aren't reachable from any other branches.
** Removing them is potentially DANGEROUS: it would make some commit unreachable.
feature-workingonit
dev
** end of list

Getting list of non-tip branch names

When tidying things up, you may want to see the non-tip branches. Here's a way to get a list of them. The only change is && becoming ||.

{
        echo "** Below is list of non-tip branches a.k.a. inner nodes."
        echo "** These are reachable from other branches."
        echo "** If you remove them, all commits would stay reachable, but be careful anyway."
        TIPS=$( git rev-list --branches --children | grep -v ' ' | sort )
        { while read branchname commitid message
                do grep $commitid < <(echo $TIPS) -q || echo $branchname
                done
        } < <(git branch -v --no-abbrev | sed 's/\*//' )
        echo "** end of list"
}

Result looks like this:

** Below is list of non-tip branches a.k.a. inner nodes.
** These are reachable from other branches.
** If you remove them, all commits would stay reachable, but be careful anyway.
feature-alreadymerged
master
** end of list

Getting list of non-tip branch names with commit ids and message

You can decorate as you wish, for example by changing:

echo $branchname

into

echo -e "$branchname\t$commitid\t$message"

Result then looks like this:

** Below is list of non-tip branches a.k.a. inner nodes.
** These are reachable from other branches.
** If you remove them, all commits would stay reachable, but be careful anyway.
feature-alreadymerged   426ac5186841876163970afdcc5774d46641abc4    Cool feature foo, ref task #1234
master  39148238924687239872eea425c0e837fafdcfd9    Some commit
** end of list

You can then review those branches in your favorite tool to see if it makes sense to remove them.

like image 179
Stéphane Gourichon Avatar answered Oct 07 '22 00:10

Stéphane Gourichon