<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Rick McGavin]]></title><description><![CDATA[Rick McGavin]]></description><link>https://rickmcgavin.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Apr 2026 22:57:29 GMT</lastBuildDate><atom:link href="https://rickmcgavin.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Git Workflow Hack: A Bash Script for Pushing New Branches]]></title><description><![CDATA[In the software development world, laziness is often seen as a good thing, albeit jokingly. This stems from the developer's quest to avoid repetitive and tedious tasks by crafting clever, automated solutions. While this trait usually leads to increas...]]></description><link>https://rickmcgavin.dev/git-workflow-hack-a-bash-script-for-pushing-new-branches</link><guid isPermaLink="true">https://rickmcgavin.dev/git-workflow-hack-a-bash-script-for-pushing-new-branches</guid><category><![CDATA[Bash]]></category><category><![CDATA[Git]]></category><category><![CDATA[bash script]]></category><category><![CDATA[Productivity]]></category><category><![CDATA[automation]]></category><dc:creator><![CDATA[Rick McGavin]]></dc:creator><pubDate>Wed, 17 May 2023 12:00:39 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1684280345871/e1077857-a535-4a71-b016-e0ab27c6ffaf.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In the software development world, laziness is often seen as a good thing, albeit jokingly. This stems from the developer's quest to avoid repetitive and tedious tasks by crafting clever, automated solutions. While this trait usually leads to increased efficiency, there are instances when the time spent automating tasks surpasses the time required to complete them manually. Today, I'll share my experience with the laziest piece of code I've ever written: a single-line bash script that delicately toes the line between an efficient time saver and a complete waste of time.</p>
<p>Imagine this common development scenario: You've created a new branch, <code>feature/cool-new-thing</code>. After implementing some changes, staging, and committing them, you're ready to push your code. You run a <code>git push</code> command, only to be greeted by an error because your new branch lacks an upstream counterpart. The error message helpfully suggests running the <code>git push --set-upstream origin feature/cool-new-thing</code> command. You can copy and paste that, but ugh! The hassle of leaving the keyboard to use your mouse just to copy and paste some text is annoying!</p>
<p>I had grown tired of this minor inconvenience. I could never remember the full command with the appropriate flags. I would habitually run <code>git push</code> and subsequently copy and paste the error message that appeared, which drove me a little more nuts each time. So I wrote a bash script—an alias, to be precise—that you insert into your shell startup file. Here's the script:</p>
<pre><code class="lang-bash"><span class="hljs-built_in">alias</span> copyNewBranch=<span class="hljs-string">"git push 2&gt;&amp;1 | grep 'git push' | sed 's/^ *//g' | tr -d '\n' | pbcopy"</span>
</code></pre>
<p>Let's break this script down:</p>
<ul>
<li><code>git push 2&gt;&amp;1</code>: This attempts to run <code>git push</code> while redirecting STDERR to STDOUT.</li>
<li><code>grep 'git push'</code>: This filters the output to include only lines containing 'git push'.</li>
<li><code>sed 's/^ *//g'</code>: This removes any leading whitespace from the filtered output.</li>
<li><code>tr -d '\n'</code>: This deletes newline characters, consolidating the output into a single line.</li>
<li><code>pbcopy</code>: This copies the resulting output to the clipboard.</li>
</ul>
<p>When you want to push a new local branch, just run <code>copyNewBranch</code>. This command will execute <code>git push</code>, process the error message, and copy the correct command to your clipboard for easy pasting. Your hands never have to leave the keyboard!</p>
<p>Keep in mind that the <code>pbcopy</code> command is macOS-specific.</p>
<p>For Windows, try replacing <code>pbcopy</code> with <code>clip</code>:</p>
<pre><code class="lang-bash"><span class="hljs-built_in">alias</span> copyNewBranch=<span class="hljs-string">"git push 2&gt;&amp;1 | grep 'git push' | sed 's/^ *//g' | tr -d '\n' | clip"</span>
</code></pre>
<p>For Linux, try using <a target="_blank" href="https://github.com/astrand/xclip"><code>xclip</code></a> or <a target="_blank" href="https://github.com/kfish/xsel"><code>xsel</code></a>, depending on your system:</p>
<pre><code class="lang-bash"><span class="hljs-comment"># Using xclip</span>
<span class="hljs-built_in">alias</span> copyNewBranch=<span class="hljs-string">"git push 2&gt;&amp;1 | grep 'git push' | sed 's/^ *//g' | tr -d '\n' | xclip -selection clipboard"</span>

<span class="hljs-comment"># Using xsel</span>
<span class="hljs-built_in">alias</span> copyNewBranch=<span class="hljs-string">"git push 2&gt;&amp;1 | grep 'git push' | sed 's/^ *//g' | tr -d '\n' | xsel --clipboard --input"</span>
</code></pre>
<p>As an alternative, you could use the <a target="_blank" href="https://www.warp.dev/">warp terminal</a>. When you run a <code>git push</code> without an upstream branch, the warp terminal will autosuggest the corrected command! It's a convenient feature to have baked in.</p>
<p>Even though I currently use Warp, I will continue to use this bash script. Admittedly, it only saves me mere seconds each time I use it, but I've consistently relied on it since its creation to help me save a trip to the mouse. I expect it to remain a staple in my toolbox for years.</p>
]]></content:encoded></item></channel></rss>