{"id":3056,"date":"2026-02-16T14:34:42","date_gmt":"2026-02-16T14:34:42","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=3056"},"modified":"2026-02-16T14:36:36","modified_gmt":"2026-02-16T14:36:36","slug":"how-to-find-a-branch-parent-git","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/miscellaneou\/how-to-find-a-branch-parent-git\/","title":{"rendered":"How to find a branch parent in Git"},"content":{"rendered":"<p>You know that moment when you&#8217;re staring at a branch named <code>feature\/whatever<\/code> and thinking:<\/p>\n<blockquote>\n<p>&quot;Cool\u2026 but what was this branched off from?&quot;<\/p>\n<\/blockquote>\n<p>Maybe you&#8217;re cleaning up old branches, reviewing a PR, or trying to figure out why <code>feature\/foo<\/code> contains commits from three different universes. Either way: Git doesn&#8217;t explicitly store &quot;parent branch&quot; metadata (because branches are just pointers to commits). But we <em>can<\/em> usually infer it.<\/p>\n<p>In this post, we&#8217;ll use a practical one-liner that prints the most likely parent branch of your current branch.<\/p>\n<h2>What we&#8217;re building<\/h2>\n<p>A small command that you can run on any branch to get a best-guess &quot;parent branch&quot; name.<\/p>\n<p>Here&#8217;s the solution:<\/p>\n<pre><code class=\"language-bash\">git show-branch | sed &quot;s\/].*\/\/&quot; | grep &quot;\\*&quot; | grep -v &quot;$(git rev-parse --abbrev-ref HEAD)&quot; | head -n1 | sed &quot;s\/^.*\\[\/\/&quot;<\/code><\/pre>\n<p>If you like, you can wrap it in a function later so it&#8217;s easier to run. But first, let&#8217;s understand what&#8217;s going on.<\/p>\n<h2>The command (run it on the branch you&#8217;re investigating)<\/h2>\n<p>Step 1: checkout the branch:<\/p>\n<pre><code class=\"language-bash\">git checkout feature\/my-branch<\/code><\/pre>\n<p>Step 2: run the one-liner:<\/p>\n<pre><code class=\"language-bash\">git show-branch | sed &quot;s\/].*\/\/&quot; | grep &quot;\\*&quot; | grep -v &quot;$(git rev-parse --abbrev-ref HEAD)&quot; | head -n1 | sed &quot;s\/^.*\\[\/\/&quot;<\/code><\/pre>\n<h3>What you&#8217;ll get back<\/h3>\n<p>Something like:<\/p>\n<pre><code class=\"language-text\">main<\/code><\/pre>\n<p>Meaning: &quot;Given what I see in the commit graph, <code>main<\/code> looks like the most likely branch this one came from.&quot;<\/p>\n<p>And voil\u00e0, your repo just got a little less mysterious.<\/p>\n<h2>How it works (without turning this into a PhD)<\/h2>\n<p>This is a pipeline. Each part trims the output until we&#8217;re left with a single branch name.<\/p>\n<h3>1) <code>git show-branch<\/code><\/h3>\n<p>This command prints a compact view of commits and the branches they belong to. It&#8217;s useful when you want to compare branches and see where they intersect.<\/p>\n<p>The raw output is noisy, but it includes bracketed branch names like:<\/p>\n<pre><code>* [main] ...\n ! [feature\/my-branch] ...<\/code><\/pre>\n<h3>2) <code>sed &quot;s\/].*\/\/&quot;<\/code><\/h3>\n<p>This removes everything after the <code>]<\/code>, leaving just:<\/p>\n<pre><code>* [main\n ! [feature\/my-branch<\/code><\/pre>\n<p>We&#8217;re basically stripping commit messages and keeping only the branch label portion.<\/p>\n<h3>3) <code>grep &quot;\\*&quot;<\/code><\/h3>\n<p>This keeps only lines containing <code>*<\/code>.<\/p>\n<p>In <code>git show-branch<\/code> output, <code>*<\/code> is used to highlight a particular row of interest across branches (in practice, it helps narrow candidates to &quot;closest&quot; related branches).<\/p>\n<h3>4) <code>grep -v &quot;$(git rev-parse --abbrev-ref HEAD)&quot;<\/code><\/h3>\n<p>Now we remove the current branch name from the list.<\/p>\n<p>Because if Git returns your branch as its own parent, that&#8217;s not helpful (and slightly insulting).<\/p>\n<p><code>git rev-parse --abbrev-ref HEAD<\/code> prints the current branch name, so this part is a clean &quot;exclude me&quot;.<\/p>\n<h3>5) <code>head -n1<\/code><\/h3>\n<p>We take the first remaining match\u2014basically the best candidate from the top.<\/p>\n<p>This is one reason I call the output a &quot;best guess&quot;. It&#8217;s a very useful guess, but it&#8217;s still based on how <code>git show-branch<\/code> orders things.<\/p>\n<h3>6) <code>sed &quot;s\/^.*\\[\/\/&quot;<\/code><\/h3>\n<p>Finally, remove everything up to the <code>[<\/code>, so we&#8217;re left with just:<\/p>\n<pre><code>main<\/code><\/pre>\n<h2>Quick recap \u2705<\/h2>\n<p>At this point, you have a one-liner that prints the most likely parent branch, a quick way to sanity-check where a feature branch probably started, and a decent understanding of the pipe magic so it doesn&#8217;t feel like copy-paste sorcery.<\/p>\n<h2>Make it easier to use (optional but recommended)<\/h2>\n<p>If you find yourself using this often, add a helper function to your shell config (<code>~\/.zshrc<\/code> or <code>~\/.bashrc<\/code>):<\/p>\n<pre><code class=\"language-bash\">git-parent() {\n  git show-branch \\\n    | sed &quot;s\/].*\/\/&quot; \\\n    | grep &quot;\\*&quot; \\\n    | grep -v &quot;$(git rev-parse --abbrev-ref HEAD)&quot; \\\n    | head -n1 \\\n    | sed &quot;s\/^.*\\[\/\/&quot;\n}<\/code><\/pre>\n<p>Now you can just run:<\/p>\n<pre><code class=\"language-bash\">git-parent<\/code><\/pre>\n<p>Much nicer.<\/p>\n<h2>Caveats (because Git is Git)<\/h2>\n<p>This works best when your repo follows a normal branching model (feature branches off <code>main<\/code>\/<code>master<\/code>\/<code>develop<\/code>), when you have the relevant branches locally (run <code>git fetch --all<\/code> if needed), and when your history hasn&#8217;t been heavily rewritten in weird ways.<\/p>\n<p>Things that can confuse &quot;parent branch&quot; inference: rebasing after branching, lots of cherry-picks from multiple branches, branching off a feature branch and then merging from other places, or when the real parent branch was deleted.<\/p>\n<p>So treat the output as: <strong>&quot;most likely parent branch&quot;<\/strong>, not a guaranteed truth.<\/p>\n<h2>Want to read more?<\/h2>\n<p>If you want to go beyond &quot;best guess&quot; and explore exact branch points, merge-bases, fork-points, and different strategies, these two Stack Overflow threads are gold:<\/p>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/1527234\/finding-a-branch-point-with-git\">http:\/\/stackoverflow.com\/questions\/1527234\/finding-a-branch-point-with-git<\/a><\/p>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/3161204\/find-the-parent-branch-of-a-git-branch\">http:\/\/stackoverflow.com\/questions\/3161204\/find-the-parent-branch-of-a-git-branch<\/a><\/p>\n<h2>Final thoughts<\/h2>\n<p>Git doesn&#8217;t store a &quot;parent branch&quot;, but with the commit graph and a bit of filtering, you can usually infer it quickly:<\/p>\n<pre><code class=\"language-bash\">git show-branch | sed &quot;s\/].*\/\/&quot; | grep &quot;\\*&quot; | grep -v &quot;$(git rev-parse --abbrev-ref HEAD)&quot; | head -n1 | sed &quot;s\/^.*\\[\/\/&quot;<\/code><\/pre>\n<p>Next time someone asks &quot;where did this branch come from?&quot;, you&#8217;ll have an answer\u2014and you won&#8217;t even need to blame your coworker. \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You know that moment when you&#8217;re staring at a branch named feature\/whatever and thinking: &quot;Cool\u2026 but what was this branched off from?&quot; Maybe you&#8217;re cleaning up old branches,&hellip;<\/p>\n","protected":false},"author":1,"featured_media":4790,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[],"class_list":["post-3056","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-miscellaneou"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/3056","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=3056"}],"version-history":[{"count":2,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/3056\/revisions"}],"predecessor-version":[{"id":3058,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/3056\/revisions\/3058"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/4790"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=3056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=3056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=3056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}