Chi-Sheng Liu
Posted on August 19, 2024
Introduction
As an engineer, Git is an essential tool, and Lazygit is a command-line Git tool written in Golang that can save you a significant amount of time by reducing the need to manually type Git commands. Saving a few seconds each time can add up to hours saved over the course of a year.
Don’t tell me you type commands quickly (you’re just too lazy to learn new tools), or that you use something like alias gs='git status'
to speed things up (you’re just afraid of learning a lot of shortcuts). Trust me, nothing can be faster than using this tool.
I won’t go into the installation steps here; please refer to the official documentation for that.
Interface
After installation, you can start Lazygit in a repository by executing lazygit
. I set up an alias alias lg='lazygit'
to help me launch it even faster.
The interface is divided into the left and right sides. The left side has five sections: Status, Files, Local branches, Commits, and Stash. Some sections also have tabs, such as the Files section, which has tabs for Worktrees and Submodules.
The right side has two areas: a detailed information preview and a list of the corresponding Git commands triggered by each shortcut.
Frequently Used Shortcuts
You can find the complete list of shortcuts in the official documentation.
Here are a few shortcuts I use the most:
- Global
-
h
andl
: Switch between the sections on the left side. -
k
andj
: Move the cursor up and down. -
/
: Search for a string. -
[
and]
: Switch between the tabs on the left side. -
Ctrl + o
: Copy the file name (in the Files section), branch name (in the Local branches section), or commit hash (in the commits section) to the clipboard. -
q
: Exit Lazygit.
-
- Left Side - Files Section
- Spacebar: Add or remove the selected file from the staging area.
-
a
: Add or remove all files from the staging area. - Enter: Collapse or expand folders when used on a folder, or enter the file preview area on the right side when used on a file.
-
C
: Open an external text editor to edit the commit message, then commit. -
A
: Amend the last commit. -
d
: Discard all changes to the selected file that have not been committed. Note that if the file is newly created, it will be deleted. -
D
: Discard all changes to all files that have not been committed. Use this when you’ve messed up and want to start over. -
S
: Open the stash options.
- Left Side - Local Branches Section
- Space: Switch to the selected branch.
-
n
: Checkout a new branch from the selected branch. -
f
: Fetch new commits for the selected branch from the remote. -
r
: Rebase onto the selected branch. -
R
: Rename the selected branch. -
p
: Pull the selected branch. -
P
: Push the selected branch. -
o
: Open the GitHub pull request page for the selected branch. - Enter: View the commits of the selected branch. Use Esc to go back.
- Left Side - Commits Section
- Enter: View the files changed in the selected commit. Use Esc to go back.
-
R
: Open an external editor to modify the commit message. -
d
: Delete the selected commit. -
g
: Open the reset options. -
F
: Create afixup!
commit for the selected commit. -
S
: Squash allfixup!
commits on top of the selected commit.
- Left Side - Stash Section
-
g
: Apply and delete the selected stash. - Space: Apply the selected stash without deleting it.
-
d
: Delete the selected stash.
-
- Right Side - Preview Area
-
v
: Enter multi-line selection mode. - Space: Add or remove the selected line or lines (when in multi-line selection mode) from the staging area.
- Esc: Return to the left side sections.
-
Practical Example
Let's combine this with the previous article on
The Ultimate Guide to Git for Open-Source Development
Chi-Sheng Liu ・ Aug 15
Creating a New Branch
First, make sure that the upstream-master
branch is in sync with the remote. Use h
and l
to switch to the Local branches section, then use k
and j
to navigate to the upstream-master
branch. Next, press f
to fetch the latest changes. Finally, press n
to create a new branch.
Syncing with Upstream
Again, navigate to the upstream-master
branch in the Local branches section, then press f
to fetch the latest changes and r
to rebase onto it.
Development and Pull Request
Navigate to the Files section and use the spacebar to add files to the staging area one by one, or use a
to add all files at once. Then, press C
to commit. Next, navigate to the branch you want to push in the Local branches section and press P
to push. Finally, press o
to open the GitHub pull request page.
Conclusion
At first, you might find it difficult to learn all the shortcuts, but with time, they will become muscle memory. The time saved is definitely worth it.
If you want to learn more about the features, please visit the official repository.
Posted on August 19, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.