<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Git on Kiril Vladimirov</title>
    <link>https://vld.bg/tags/git/</link>
    <description>Recent content in Git on Kiril Vladimirov</description>
    <generator>Hugo -- 0.168.0</generator>
    <language>en</language>
    <lastBuildDate>Mon, 23 Oct 2023 11:27:01 +0300</lastBuildDate>
    <atom:link href="https://vld.bg/tags/git/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Open a pull request from command line</title>
      <link>https://vld.bg/articles/git-pull-request/</link>
      <pubDate>Mon, 23 Oct 2023 11:27:01 +0300</pubDate>
      <guid>https://vld.bg/articles/git-pull-request/</guid>
      <description>&lt;p&gt;When ready working on a &lt;a href=&#34;https://martinfowler.com/bliki/FeatureBranch.html&#34; target=&#34;_blank&#34;&gt;feature
branch&lt;/a&gt;
 in a project that
uses GitHub, GitLab or anything else like them, one generally do &lt;code&gt;git push -u origin &amp;lt;branch_name&amp;gt;&lt;/code&gt; and then goes to the web interface to open a pull
request. Pretty much all of these platforms provide the URL to go in order to
spare a few clicks. For instance:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#d0d0d0;background-color:#202020;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-plain&#34; data-lang=&#34;plain&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;❯ git push -u origin baba
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Enumerating objects: 1, done.
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Counting objects: 100% (1/1), done.
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Writing objects: 100% (1/1), 181 bytes | 181.00 KiB/s, done.
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;remote:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;remote: Create a pull request for &amp;#39;baba&amp;#39; on GitHub by visiting:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;remote:      https://github.com/user/project_name/pull/new/baba
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;remote:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;To github.com:user/project_name.git
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt; * [new branch]      baba -&amp;gt; baba
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;A few years ago &lt;a href=&#34;https://rstankov.com/&#34; target=&#34;_blank&#34;&gt;Rado Stankov&lt;/a&gt;
 wrote a great post about
a few git aliases and scripts he uses to automate things: &lt;a href=&#34;https://blog.rstankov.com/git-tricks/&#34; target=&#34;_blank&#34;&gt;Git
Tricks&lt;/a&gt;
. I took literally all of the
tricks he shows there in my &lt;a href=&#34;https://github.com/vladimiroff/dotfiles&#34; target=&#34;_blank&#34;&gt;dotfiles&lt;/a&gt;

and strongly suggest for you to do the same.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>When ready working on a <a href="https://martinfowler.com/bliki/FeatureBranch.html" target="_blank">feature
branch</a>
 in a project that
uses GitHub, GitLab or anything else like them, one generally do <code>git push -u origin &lt;branch_name&gt;</code> and then goes to the web interface to open a pull
request. Pretty much all of these platforms provide the URL to go in order to
spare a few clicks. For instance:</p>
<div class="highlight"><pre tabindex="0" style="color:#d0d0d0;background-color:#202020;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-plain" data-lang="plain"><span style="display:flex;"><span>❯ git push -u origin baba
</span></span><span style="display:flex;"><span>Enumerating objects: 1, done.
</span></span><span style="display:flex;"><span>Counting objects: 100% (1/1), done.
</span></span><span style="display:flex;"><span>Writing objects: 100% (1/1), 181 bytes | 181.00 KiB/s, done.
</span></span><span style="display:flex;"><span>Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
</span></span><span style="display:flex;"><span>remote:
</span></span><span style="display:flex;"><span>remote: Create a pull request for &#39;baba&#39; on GitHub by visiting:
</span></span><span style="display:flex;"><span>remote:      https://github.com/user/project_name/pull/new/baba
</span></span><span style="display:flex;"><span>remote:
</span></span><span style="display:flex;"><span>To github.com:user/project_name.git
</span></span><span style="display:flex;"><span> * [new branch]      baba -&gt; baba
</span></span></code></pre></div><p>A few years ago <a href="https://rstankov.com/" target="_blank">Rado Stankov</a>
 wrote a great post about
a few git aliases and scripts he uses to automate things: <a href="https://blog.rstankov.com/git-tricks/" target="_blank">Git
Tricks</a>
. I took literally all of the
tricks he shows there in my <a href="https://github.com/vladimiroff/dotfiles" target="_blank">dotfiles</a>

and strongly suggest for you to do the same.</p>
<p>One of tricks there is a <code>git-pull-request</code> scripts which allows automating
that clicking on the provided link with a <code>git pull-request</code> command. Over time
however, I&rsquo;ve started using GitLab and this trick no longer works for that, as
it assumes you&rsquo;re pushing into a GitHub repository. So, I had to modify that
script in order to be agnostic of the hosting you&rsquo;re using and simply open
whatever URL the upstream provdes on push (which to be fair, might be a bit
risky, if you&rsquo;re pushing to a shady git hosting, so use it with care):</p>
<div class="highlight"><pre tabindex="0" style="color:#d0d0d0;background-color:#202020;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span><span style="color:#cd2828;font-weight:bold">#!/bin/sh
</span></span></span><span style="display:flex;"><span><span style="color:#40ffff">current_branch</span>=<span style="color:#6ab825;font-weight:bold">$(</span>git rev-parse --abbrev-ref HEAD<span style="color:#6ab825;font-weight:bold">)</span>
</span></span><span style="display:flex;"><span>git push -v --set-upstream origin <span style="color:#ed9d13">&#34;</span><span style="color:#40ffff">$current_branch</span><span style="color:#ed9d13">&#34;</span> <span style="color:#40ffff">$2</span> 2&gt;&amp;<span style="color:#3677a9">1</span> | <span style="color:#6ab825;font-weight:bold">while</span> <span style="color:#40ffff">IFS</span>= <span style="color:#24909d">read</span> -r line
</span></span><span style="display:flex;"><span><span style="color:#6ab825;font-weight:bold">do</span>
</span></span><span style="display:flex;"><span>	<span style="color:#24909d">echo</span> <span style="color:#ed9d13">&#34;</span><span style="color:#40ffff">$line</span><span style="color:#ed9d13">&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#40ffff">url</span>=<span style="color:#6ab825;font-weight:bold">$(</span><span style="color:#24909d">echo</span> <span style="color:#ed9d13">&#34;</span><span style="color:#40ffff">$line</span><span style="color:#ed9d13">&#34;</span> | grep <span style="color:#ed9d13">&#34;remote:&#34;</span> | grep -Eo <span style="color:#ed9d13">&#39;(http|https)://[^ ]+&#39;</span><span style="color:#6ab825;font-weight:bold">)</span>
</span></span><span style="display:flex;"><span>	<span style="color:#6ab825;font-weight:bold">if</span> [ -n <span style="color:#ed9d13">&#34;</span><span style="color:#40ffff">$url</span><span style="color:#ed9d13">&#34;</span> ]
</span></span><span style="display:flex;"><span>	<span style="color:#6ab825;font-weight:bold">then</span>
</span></span><span style="display:flex;"><span>		xdg-open <span style="color:#ed9d13">&#34;</span><span style="color:#40ffff">$url</span><span style="color:#ed9d13">&#34;</span> &gt; /dev/null
</span></span><span style="display:flex;"><span>	<span style="color:#6ab825;font-weight:bold">fi</span>
</span></span><span style="display:flex;"><span><span style="color:#6ab825;font-weight:bold">done</span>
</span></span></code></pre></div><p>Note that as a linux user, I&rsquo;ve changed <code>open</code> to <code>xdg-open</code>. If you&rsquo;re using
macOS, might what to do <code>s/xdg-open/open/</code> over this.</p>
<p>If this is saved in a file named <code>git-pull-request</code> with execute permissions
(<code>+x</code>) at some of the directories listed in your <code>$PATH</code>, running <code>git pull-request</code> will push your current branch upstream and output exactly the
same as above, but also open the provided link in your default web browser
(presumably in a new tab).</p>
]]></content:encoded>
    </item>
    <item>
      <title>git bisect</title>
      <link>https://vld.bg/tips/git-bisect/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://vld.bg/tips/git-bisect/</guid>
      <description>&lt;p&gt;&lt;a href=&#34;https://git-scm.com/docs/git-bisect&#34; target=&#34;_blank&#34;&gt;git bisect&lt;/a&gt;
 provides easy to use binary
search approach in order to find a bad commit. This tip assumes you know how it
works is in general, but always forget what the commands are precisely.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Prepare a single command that tries to reproduce the bug (e.g. script,
test) &lt;strong&gt;off tree&lt;/strong&gt;. Bisect is about to start jumping around, so it can&amp;rsquo;t be
in the tree.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;git bisect start&lt;/code&gt;: begin the bisect session.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;git bisect bad&lt;/code&gt;: mark current HEAD as a bad commit (i.e. the bug is
reproducible).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;git bisect good &amp;lt;commit-ref&amp;gt;&lt;/code&gt;: some known commit in the past, where the bug
does not exist.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;You get dropped at a given commit. Try the command/test/script from step 1
and:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;if the bug exists: &lt;code&gt;git bisect bad&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;if the bug does NOT exist: &lt;code&gt;git bisect good&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Repeat until the initial bad commit is found.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;git bisect reset&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;</description>
      <content:encoded><![CDATA[<p><a href="https://git-scm.com/docs/git-bisect" target="_blank">git bisect</a>
 provides easy to use binary
search approach in order to find a bad commit. This tip assumes you know how it
works is in general, but always forget what the commands are precisely.</p>
<ol>
<li>
<p>Prepare a single command that tries to reproduce the bug (e.g. script,
test) <strong>off tree</strong>. Bisect is about to start jumping around, so it can&rsquo;t be
in the tree.</p>
</li>
<li>
<p><code>git bisect start</code>: begin the bisect session.</p>
</li>
<li>
<p><code>git bisect bad</code>: mark current HEAD as a bad commit (i.e. the bug is
reproducible).</p>
</li>
<li>
<p><code>git bisect good &lt;commit-ref&gt;</code>: some known commit in the past, where the bug
does not exist.</p>
</li>
<li>
<p>You get dropped at a given commit. Try the command/test/script from step 1
and:</p>
<ul>
<li>if the bug exists: <code>git bisect bad</code></li>
<li>if the bug does NOT exist: <code>git bisect good</code></li>
</ul>
</li>
<li>
<p>Repeat until the initial bad commit is found.</p>
</li>
<li>
<p><code>git bisect reset</code>.</p>
</li>
</ol>
<h2 id="git-bisect-run">git bisect run</h2>
<p>Steps 5 and 6 can be automated with <code>git bisect run</code> if you can provide a
script that exits with status code 0 when the chosen revision is good and
something between 1 and 127 (except for 125) when the revision is bad.</p>
<p>The script should obviously be out of tree and executed with:</p>
<p><code>git bisect run &lt;script_name&gt; [&lt;arg&gt; ...]</code>.</p>
]]></content:encoded>
    </item>
  </channel>
</rss>
