patterngitMinor
What git command could be used to check when a remote branch was created?
Viewed 0 times
whatcreatedusedbranchcouldwasgitwhenremotecommand
Problem
Git is used at work. The number of branches is growing and growing. The aim is to remove branches that are older than X weeks.
Attempt 1
Running
Attempt 2
This post https://stackoverflow.com/questions/3184555/cleaning-up-old-
Attempt 1
Running
git branch -h will probably indicate what to run to check when a branch has been created.user@localhost $ git branch -h
usage: git branch [] [-r | -a] [--merged | --no-merged]
or: git branch [] [-l] [-f] []
or: git branch [] [-r] (-d | -D) ...
or: git branch [] (-m | -M) []
or: git branch [] [-r | -a] [--points-at]
Generic options
-v, --verbose show hash and subject, give twice for upstream branch
-q, --quiet suppress informational messages
-t, --track set up tracking mode (see git-pull(1))
--set-upstream change upstream info
-u, --set-upstream-to
change the upstream info
--unset-upstream Unset the upstream info
--color[=] use colored output
-r, --remotes act on remote-tracking branches
--contains print only branches that contain the commit
--abbrev[=] use digits to display SHA-1s
Specific git-branch actions:
-a, --all list both remote-tracking and local branches
-d, --delete delete fully merged branch
-D delete branch (even if not merged)
-m, --move move/rename a branch and its reflog
-M move/rename a branch, even if target exists
--list list branch names
-l, --create-reflog create the branch's reflog
--edit-description edit the description for the branch
-f, --force force creation, move/rename, deletion
--merged print only branches that are merged
--no-merged print only branches that are not merged
--column[=] list branches in columns
--sort field name to sort on
--points-at print only branches of the objectAttempt 2
This post https://stackoverflow.com/questions/3184555/cleaning-up-old-
Solution
Note: this answer shows how to find branches that have been updated a long time ago, not branches that have been created (i.e., spliced off of some parent) a long time ago. I believe this is what the OP actually wanted (as opposed to the title he chose for the question), as this would lead to good candidates for deletion.
You have to recall that a branch in git is nothing "physical". It is, by definition, and literally, only a simple text file in
So
The object type that has dates associated with it are commits, in git. So you have to list commits. This is done with
So a working command would be:
You can read up the details on https://www.git-scm.com/docs/git-log , but in short, it means
This will give you all branch heads that are older than your given date.
You have to recall that a branch in git is nothing "physical". It is, by definition, and literally, only a simple text file in
.git/refs which only has its file name (which is the branch name) and the hash of the tip/head commit. It has no date information or anything else.So
git branch will not help you here. The object type that has dates associated with it are commits, in git. So you have to list commits. This is done with
git log. So a working command would be:
git log --remotes --before 2018-01-01 --no-walk --decorateYou can read up the details on https://www.git-scm.com/docs/git-log , but in short, it means
--remotes: start with any and all remote branches (substitute--branchesfor local branches)
--before ...: only list commits older than the date given
--no-walk: only list the very first commit for each branch, don't walk back the history
--decorate: display all tag and branch names associated, just in case
This will give you all branch heads that are older than your given date.
Code Snippets
git log --remotes --before 2018-01-01 --no-walk --decorateContext
StackExchange DevOps Q#3321, answer score: 7
Revisions (0)
No revisions yet.