From 00d959110a9d6e9d593dc53dc8ad0e294d584710 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Wed, 31 Mar 2021 17:39:15 -0500 Subject: [PATCH] Add Get The Name Of The Current Branch as a git til --- README.md | 3 ++- git/get-the-name-of-the-current-branch.md | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 git/get-the-name-of-the-current-branch.md diff --git a/README.md b/README.md index 432e6f9..3c0a0f6 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud). -_1103 TILs and counting..._ +_1104 TILs and counting..._ --- @@ -243,6 +243,7 @@ _1103 TILs and counting..._ - [Excluding Files Locally](git/excluding-files-locally.md) - [Find The Date That A File Was Added To The Repo](git/find-the-date-that-a-file-was-added-to-the-repo.md) - [Find The Initial Commit](git/find-the-initial-commit.md) +- [Get The Name Of The Current Branch](git/get-the-name-of-the-current-branch.md) - [Get The Short Version Of The Latest Commit](git/get-the-short-version-of-the-latest-commit.md) - [Grab A Single File From A Stash](git/grab-a-single-file-from-a-stash.md) - [Grep For A Pattern On Another Branch](git/grep-for-a-pattern-on-another-branch.md) diff --git a/git/get-the-name-of-the-current-branch.md b/git/get-the-name-of-the-current-branch.md new file mode 100644 index 0000000..275b2ad --- /dev/null +++ b/git/get-the-name-of-the-current-branch.md @@ -0,0 +1,20 @@ +# Get The Name Of The Current Branch + +There are a couple ways of doing this. If you are on Git 2.22+, then the +`git-branch` porcelain command now supports a flag for this. + +```bash +$ git branch --show-current +main +``` + +If you are on an older version of Git or looking for a plumbing command that is +more appropriate for scripting, then `git-rev-parse` is what you're looking +for. + +```bash +$ git rev-parse --abbrev-ref HEAD +main +``` + +[source](https://stackoverflow.com/questions/6245570/how-to-get-the-current-branch-name-in-git)