Get the highlights in your inbox every week.
Make advanced Git tasks simple with Lazygit | Opensource.com
Make advanced Git tasks simple with Lazygit
5 ways to harness the power of git with the Lazygit terminal UI.
If there's one word people use to describe Git, 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.
I made Lazygit, 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.
1. Stage files fast
Typing out git add this/is/my/really/long/filename.txt 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.
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 Files panel, meaning their changes are unstaged. Use the arrow keys to navigate between the files and hit the Space bar to toggle a file between staged and unstaged.
If you see a file that shouldn't be there, simply hit d, and a menu will come up for deleting the file.
If you want to stage just part of a file, hit Enter on the file, and you'll find yourself inside the Unstaged Changes panel. From there, you can hit Space on a line to stage just that line, moving it to the Staged Changes panel.
If you want to remove a line completely (e.g., if you accidentally left a console.log in the middle of your code), simply press d. You can swap between the Unstaged Changes and Staged Changes panels with Tab.
2. Easy, interactive rebasing
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.
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):
- Stash the changes you want to apply with git stash.
- Copy the SHA of the commit you want to amend.
- Begin the rebase with git rebase --interactive <commit-SHA>^.
- (This is where the screams start…) A TODO file opens in Vim, where you'll need to find your commit and replace pick on the line with edit.
- Save the file.
- Unstash your changes with git stash pop.
- Amend the commit with git commit --amend.
- Continue the rebase with git rebase --continue.
Just thinking back to the days of CLI terror gives me heart palpitations.
In Lazygit, all of the simple use cases for interactive rebases are supported with their own keybindings.
To amend an old commit with your current staged changes in Lazygit, simply navigate to the commit and press Shift+A. 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.
Likewise, you can squash a commit into the one below with s, rename a commit with Shift+R, and delete a commit with d.
Say you want to do something more complicated that involves multiple commits. If you press e on a commit, you'll start an interactive rebase, and all of the above commits will be shown with their rebase action (defaulting to pick). To change the action for a commit, just navigate to it and press the relevant letter: s for squash, p for pick, etc. You're effectively editing that TODO file through Lazygit, but everything requires only a single keypress.
3. Easy cherry-picking
Cherry-picking in Lazygit is done in a copy-and-paste fashion. To copy a commit, simply press c on it. Copied commits will appear with a blue background. To paste the commits onto another branch, go to the branch in the Branches panel and hit Space to check it out, then, back in the Commits panel, press v, and your copied commits will be cherry-picked (i.e., pasted) onto the new branch.
4. Search through commits
Each of the side panels can be expanded to half-screen mode and then full-screen mode using +. In full-screen, the Commits panel shows a bit more information, all of which you can search using /, and you can cycle through results with n and N. Once you've found what you're looking for, you can check out the commit with Space, revert it with t, and do a heap of other operations.
5. Open a PR with a single keypress
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 o. Your browser will open up to show:
Give it a go!
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 download and install, so give it a go!