Jesse Duffield at Opensource.comhttps://opensource.com/user_articles/370161
enMake advanced Git tasks simple with Lazygithttps://opensource.com/article/20/3/lazygit
<div class="field field-name-body field-type-text-with-summary field-label-hidden"><div class="field-items"><div class="field-item even" property="schema:articleBody content:encoded"><p>If there's one word people use to describe <a href="https://opensource.com/resources/what-is-git">Git</a>, it's "powerful." Nobody can deny that Git is indeed a powerful beast, but after months of struggling to do embarrassingly basic things in it, I realized that mere mortals like me were never going to wield that power through a command-line interface.</p>
<p>I made <a href="https://github.com/jesseduffield/lazygit" target="_blank">Lazygit</a>, a terminal UI for Git, to help me tame the beast and harness that power. With 15,000 stars on GitHub, it turns out I wasn't the only person struggling! If you've found yourself wrestling with Git's command-line interface or even one of the other Git GUIs out there, read on! You might come across a feature here that could save you time.</p>
<h2 id="stage-files-fast">1. Stage files fast</h2>
<p>Typing out <strong>git add this/is/my/really/long/filename.txt</strong> is not a fun time. Even if you copy and paste the filename, that's still nine keypresses. Lazygit lets you stage and unstage files with lightning speed.</p>
<p>Let's say you've just made some changes to some files, and now you want to commit your changes. When you open Lazygit, you'll find your modified files marked in red in the <strong>Files</strong> panel, meaning their changes are unstaged. Use the arrow keys to navigate between the files and hit the <strong>Space bar</strong> to toggle a file between staged and unstaged.</p>
<p></p><div class="media media-element-container media-default media-wysiwyg-align-center"><div id="file-468536" class="file file-image file-image-png">
<h2 class="element-invisible"><a href="/file/468536">lazygit_1.png</a></h2>
<div class="content">
<img alt="Staging in Lazygit" title="Staging in Lazygit" class="media-element file-default" data-delta="1" typeof="foaf:Image" src="https://opensource.com/sites/default/files/uploads/lazygit_1.png" width="600" height="448" /></div>
</div>
</div>
<p>If you see a file that shouldn't be there, simply hit <strong>d</strong>, and a menu will come up for deleting the file.</p>
<p>If you want to stage just part of a file, hit <strong>Enter</strong> on the file, and you'll find yourself inside the <strong>Unstaged Changes</strong> panel. From there, you can hit <strong>Space</strong> on a line to stage just that line, moving it to the <strong>Staged Changes</strong> panel.</p>
<p></p><div class="media media-element-container media-default media-wysiwyg-align-center"><div id="file-468541" class="file file-image file-image-png">
<h2 class="element-invisible"><a href="/file/468541">lazygit_2.png</a></h2>
<div class="content">
<img alt="Staging in Lazygit" title="Staging in Lazygit" class="media-element file-default" data-delta="2" typeof="foaf:Image" src="https://opensource.com/sites/default/files/uploads/lazygit_2.png" width="600" height="448" /></div>
</div>
</div>
<p>If you want to remove a line completely (e.g., if you accidentally left a <strong>console.log</strong> in the middle of your code), simply press <strong>d</strong>. You can swap between the <strong>Unstaged Changes</strong> and <strong>Staged Changes</strong> panels with <strong>Tab</strong>.</p>
<h2 id="easy-interactive-rebasing">2. Easy, interactive rebasing</h2>
<p>Say you're working on a feature branch, and three commits ago, you made a typo. You haven't yet put up a pull request (PR), and you want to maintain the logical separation between the commits for the sake of the reviewers' understanding. Wouldn't it be great to just amend the original commit with your fix? Enter interactive rebase.</p>
<p>The user experience of an interactive rebase on the Git command line is a horror story that belongs in a Stephen King novel. To do something as simple as amending an old commit requires following these steps (do not let your kids read this without parental guidance):</p>
<ol><li>Stash the changes you want to apply with <strong>git stash</strong>.</li>
<li>Copy the SHA of the commit you want to amend.</li>
<li>Begin the rebase with <strong>git rebase --interactive &lt;commit-SHA&gt;^</strong>.</li>
<li>(This is where the screams start…) A TODO file opens in Vim, where you'll need to find your commit and replace <strong>pick</strong> on the line with <strong>edit</strong>.</li>
<li>Save the file.</li>
<li>Unstash your changes with <strong>git stash pop</strong>.</li>
<li>Amend the commit with <strong>git commit --amend</strong>.</li>
<li>Continue the rebase with <strong>git rebase --continue</strong>.</li>
</ol><p>Just thinking back to the days of CLI terror gives me heart palpitations.</p>
<p>In Lazygit, all of the simple use cases for interactive rebases are supported with their own keybindings.</p>
<p>To amend an old commit with your current staged changes in Lazygit, simply navigate to the commit and press <strong>Shift+A</strong>. Just like that, you're done! If merge conflicts occur during the process, you'll be prompted to resolve them, and once they're resolved, you'll be prompted to continue the rebase.</p>
<p></p><div class="media media-element-container media-default media-wysiwyg-align-center"><div id="file-468546" class="file file-image file-image-png">
<h2 class="element-invisible"><a href="/file/468546">lazygit_3.png</a></h2>
<div class="content">
<img alt="Interactive rebasing in Lazygit" title="Interactive rebasing in Lazygit" class="media-element file-default" data-delta="3" typeof="foaf:Image" src="https://opensource.com/sites/default/files/uploads/lazygit_3.png" width="600" height="448" /></div>
</div>
</div>
<p>Likewise, you can squash a commit into the one below with <strong>s</strong>, rename a commit with <strong>Shift+R</strong>, and delete a commit with <strong>d</strong>.</p>
<p>Say you want to do something more complicated that involves multiple commits. If you press <strong>e</strong> on a commit, you'll start an interactive rebase, and all of the above commits will be shown with their rebase action (defaulting to <strong>pick</strong>). To change the action for a commit, just navigate to it and press the relevant letter: <strong>s</strong> for squash, <strong>p</strong> for pick, etc. You're effectively editing that TODO file through Lazygit, but everything requires only a single keypress.</p>
<p></p><div class="media media-element-container media-default media-wysiwyg-align-center"><div id="file-468551" class="file file-image file-image-png">
<h2 class="element-invisible"><a href="/file/468551">lazygit_4.png</a></h2>
<div class="content">
<img alt="Interactive rebasing in Lazygit" title="Interactive rebasing in Lazygit" class="media-element file-default" data-delta="4" typeof="foaf:Image" src="https://opensource.com/sites/default/files/uploads/lazygit_4.png" width="600" height="448" /></div>
</div>
</div>
<p>Then, to start the rebase, simply bring up the rebase options with <strong>m</strong> and hit <strong>Continue</strong>. This will move forward, dropping a commit, and then squashing the next three commits into the previous one. No more nightmares!</p>
<h2 id="easy-cherry-picking">3. Easy cherry-picking</h2>
<p>Cherry-picking in Lazygit is done in a copy-and-paste fashion. To copy a commit, simply press <strong>c</strong> on it. Copied commits will appear with a blue background. To paste the commits onto another branch, go to the branch in the <strong>Branches</strong> panel and hit <strong>Space</strong> to check it out, then, back in the <strong>Commits</strong> panel, press <strong>v</strong>, and your copied commits will be cherry-picked (i.e., pasted) onto the new branch.</p>
<p></p><div class="media media-element-container media-default media-wysiwyg-align-center"><div id="file-468556" class="file file-image file-image-png">
<h2 class="element-invisible"><a href="/file/468556">lazygit_5.png</a></h2>
<div class="content">
<img alt="Cherry-picking in Lazygit" title="Cherry-picking in Lazygit" class="media-element file-default" data-delta="5" typeof="foaf:Image" src="https://opensource.com/sites/default/files/uploads/lazygit_5.png" width="600" height="448" /></div>
</div>
</div>
<h2 id="search-through-commits">4. Search through commits</h2>
<p>Each of the side panels can be expanded to half-screen mode and then full-screen mode using <strong>+</strong>. In full-screen, the <strong>Commits</strong> panel shows a bit more information, all of which you can search using <strong>/</strong>, and you can cycle through results with <strong>n</strong> and <strong>N</strong>. Once you've found what you're looking for, you can check out the commit with <strong>Space</strong>, revert it with <strong>t</strong>, and do a heap of other operations.</p>
<p></p><div class="media media-element-container media-default media-wysiwyg-align-center"><div id="file-468561" class="file file-image file-image-png">
<h2 class="element-invisible"><a href="/file/468561">lazygit_6.png</a></h2>
<div class="content">
<img alt="Searching commits in Lazygit" title="Searching commits in Lazygit" class="media-element file-default" data-delta="6" typeof="foaf:Image" src="https://opensource.com/sites/default/files/uploads/lazygit_6.png" width="600" height="448" /></div>
</div>
</div>
<h2 id="open-a-pr-with-a-single-keypress">5. Open a PR with a single keypress</h2>
<p>What's the point of writing code if not to get it merged into the codebase? To open a pull request, simply navigate to the branch and press <strong>o</strong>. Your browser will open up to show:</p>
<p></p><div class="media media-element-container media-default media-wysiwyg-align-center"><div id="file-468566" class="file file-image file-image-png">
<h2 class="element-invisible"><a href="/file/468566">lazygit_7.png</a></h2>
<div class="content">
<img alt="Open a pull request in Lazygit" title="Open a pull request in Lazygit" class="media-element file-default" data-delta="7" typeof="foaf:Image" src="https://opensource.com/sites/default/files/uploads/lazygit_7.png" width="600" height="359" /></div>
</div>
</div>
<h2 id="give-it-a-go">Give it a go!</h2>
<p>If you've read this far, you're either interested enough in Lazygit to give it a try, or you're preparing a scathing rebuke that argues my keypress counts are less impressive when Git aliases are involved. Either way, I'd love to hear your feedback in the comments. If you're on the fence, Lazygit is free and easy to <a href="https://github.com/jesseduffield/lazygit#installation" target="_blank">download and install</a>, so give it a go!</p>
</div></div></div><div class="field field-name-field-lead-image field-type-image field-label-above"><div class="field-label">Lead Image:&nbsp;</div><div class="field-items"><div class="field-item even" rel="schema:contentUrl" resource="https://opensource.com/sites/default/files/lead-images/laptop_screen_desk_work_chat_text.png"><img typeof="foaf:Image" src="https://opensource.com/sites/default/files/lead-images/laptop_screen_desk_work_chat_text.png" width="1040" height="584" alt="Person using a laptop" title="Person using a laptop" /></div></div></div><div class="field field-name-field-tags field-type-taxonomy-term-reference field-label-above"><div class="field-label">Tags:&nbsp;</div><div class="field-items"><div class="field-item even" rel="schema:keywords"><a href="/tags/git" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">Git</a></div></div></div><div class="field field-name-field-article-type field-type-taxonomy-term-reference field-label-above"><div class="field-label">Article Type:&nbsp;</div><div class="field-items"><div class="field-item even" rel="schema:learningResourceType"><a href="/article-type/article" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">Article</a></div></div></div><div class="field field-name-field-default-license field-type-text field-label-above"><div class="field-label">Default CC License:&nbsp;</div><div class="field-items"><div class="field-item even" property="schema:license"><p>CC-BY-SA 4.0</p>
</div></div></div><div class="field field-name-field-article-subhead field-type-text-long field-label-above">
<div class="field-label">Subhead:&nbsp;</div>
<div class="field-items">
<h2>5 ways to harness the power of git with the Lazygit terminal UI.</h2>
</div>
</div>
<div class="field field-name-field-listacles field-type-entityreference field-label-above"><div class="field-label">Listicles:&nbsp;</div><div class="field-items"><div class="field-item even" property="">Make Git easy with Git Cola </div><div class="field-item odd" property="">6 handy Bash scripts for Git</div><div class="field-item even" property="">10 resources to boost your Git skills</div></div></div><div class="form-item form-type-item">
<label>Article Rate </label>
<div class="rate-widget-1 rate-widget clear-block rate-average rate-widget-thumbs_up rate-6e180cc68b686825535de0b4f547a270 rate-node-61436-1-1" id="rate-node-61436-1-1"><div class="rate-info">51</div><a class="rate-button rate-thumbs-up-btn-up" id="rate-button-1" rel="nofollow" href="/user/370161/feed?rate=hyFW_jI_SdbYx-KNQAMjLgtU1QBt0Z8llOnPts3aCbQ" title="up">up</a></div>
</div>
<span rel="schema:url" resource="/article/20/3/lazygit" class="rdf-meta element-hidden"></span><span property="schema:name" content="Make advanced Git tasks simple with Lazygit" class="rdf-meta element-hidden"></span>Wed, 18 Mar 2020 07:01:00 +0000Jesse Duffield61436 at https://opensource.com