Useful Git Snippets

04-05-2021

Here are some useful Git snippets

Get a count number of the number of branches for a repository.

1
git branch | wc -l

Include the --all flag to get both local and remote branches

1
git branch --all | wc -l

List all branches not merged into another branch, i.e. main

1
git branch --no-merged main

Determine if a file is being tracked by GIT

1
git ls-files --error-unmatch <file name>

If a file s not tracked by GIT currently the command will exist with an error.

_Delete multiple branches, example use case: delete all branches like: PR_123, PR_124, …_

1
git branch -D `git branch | grep -E '^PR_*'`

NOTE The -D flag will force a delete, even if unmerged changes are present

Determine if a branch has been merged

From the branch you’re interested in, eg: git checkout develop

1
git branch --merged main

Determine what branches have NOT been merged

1
git branch --no-merged main

Compare two git branches

If you need to determine the differences between 2 branches, use the ... syntax:

1
git diff branch1...branch2