Published on

Playing with Git & Lazygit

Authors

Playing with Git & Lazygit

Git is an essential tool for any developer, but remembering all those commands can be challenging. In this post, I'll share some useful Git commands and Lazygit tricks that have helped boost my productivity.

Useful Git Commands

Comparing Branches

To compare your current branch with another branch:

git diff ..branch_2

This command shows the differences between your checked out branch and branch_2.

Checking Out Remote Branches

Need to work on a remote branch? Use this command:

git checkout -t origin/HDPD-17944

The -t flag sets up tracking, making it easier to push and pull changes.

Investigating Line History

Ever wondered who changed a specific line and why? This command shows the history of a specific line in a file:

git log -L79,+1:'package.json'

This example checks the history of line 79 in package.json. The +1 indicates how many lines to include.

Working with Merge Requests

You can easily check out a remote merge request into your local branch:

git pull origin merge-requests/10/head

For example: git pull origin merge-requests/251/head

Removing Specific Commits

To remove a specific commit from your branch, use interactive rebase:

git rebase -i HEAD~x

Where x is the number of commits you want to go back. In the editor that opens, change pick to drop for the commit you want to remove, then save and exit.

Git Rebase Interactive Mode

Squashing Commits

When you want to merge a feature branch into master with all commits squashed into one:

git checkout master
git merge --squash bugfix
git commit

This takes all the commits from the bugfix branch, squashes them into a single commit, and stages it for your master branch. The final git commit creates the actual commit.

If you omit the -m parameter in the commit command, Git will open an editor with a draft message containing all the messages from your squashed commits.

Git Squash Workflow

Lazygit Tips & Tricks

Lazygit is a terminal UI for Git that makes many operations faster and more intuitive.

Lazygit Interface

Lazygit divides its interface into several panels:

  • Files: View and manage changed files
  • Branches: View, create, and manage branches
  • Commits: Browse and manipulate commit history
  • Stash: Manage your stashed changes
  • Status: See information about your repository

You can navigate between these panels using the tab key or by clicking on them.

Nuking a Work Tree

Press SHIFT-D to discard all changes in your working directory. Be careful with this one!

Lazygit Discard Changes

Copying and Pasting Commits

Lazygit allows you to copy and paste commits between branches:

  1. Navigate to the commits tab
  2. Select the commit(s) you want to copy
  3. Press c to mark the commit as copied
  4. Switch to your target branch
  5. Press V to paste (cherry-pick) the copied commits

You can press <esc> at any time to cancel the selection.

Lazygit Cherry-pick

Common Keyboard Shortcuts

Here are some other useful Lazygit keyboard shortcuts:

  • ?: Show help menu
  • q: Quit
  • PgUp/PgDown: Scroll through long lists
  • Space: Select item (in multi-select mode)
  • c: Commit changes
  • P: Push changes
  • p: Pull changes
  • f: Fetch changes
  • s: Stash changes
  • a: Stage all changes
  • A: Abort a merge/rebase

Conclusion

These Git commands and Lazygit tricks have significantly improved my workflow. Learning to use Git effectively is an ongoing process, but these tools should help make your development experience smoother and more efficient.

Do you have any favorite Git commands or tricks? Let me know in the comments!