Rahul
Rahul

Reputation: 181

Fetch all the branches in git and get Hashcode for all the branches

I am trying to get a command in git which can help me fetch list of all the branches or my code and also list down all the hashcode in front of same . As I want to delete all my stale branches and keep it safe for later retrieval.

To List all the branches following command is enough.

Git branch -r 

to get the hashcode for a branch

git rev-parse <BranchName>

can I get one command where it list all branches and hashcode in front of it.

Upvotes: 2

Views: 221

Answers (3)

MaNKuR
MaNKuR

Reputation: 2704

I used cut and while loops to reach to the solution:

git branch -r -l | cut -d " " -f 3 | while read i; do echo "$i: $(git rev-parse $i)"; done

Upvotes: 1

ElpieKay
ElpieKay

Reputation: 30878

 git for-each-ref refs/remotes --format="%(objectname) %(refname:lstrip=2)"

git for-each-ref outputs information on all local refs.

refs/remotes is the pattern for remote tracking branches. Only the refs matching the pattern will be printed.

--format formats the output. Here %(objectname) refers to the commit hash. A following space is literally a space. %(refname) refers to the ref name. The full name of a remote tracking branch is like refs/remotes/origin/foo. :lstrip=2 suppresses the left 2 parts refs and remotes.

See git for-each-ref for more.

Upvotes: 2

Rahul
Rahul

Reputation: 181

I got bit of answer but still not the concrete on:

Commands used : To List all branches :

git branch -r >>branchesList

List all Hashcode for branches :

for remote in git branch -r; do git rev-parse $remote ; done >> hascodeforbranches.txt*

Put it in an xl and u will have a mapping .

Upvotes: 0

Related Questions