1
 
 
Account
In your account you can view the status of your application, save incomplete applications and view current news and events
September 30, 2024

Developer Hacks – Modern Command Line Tools and Advanced Git Commands

What is the article about?

Working with the terminal and with Git are among the basic techniques for developers. This article presents a modern development setup, state-of-the-art alternatives to classic shell programs, and advanced git commands. Using these tools will help you navigate your projects with greater ease and speed, allowing you to focus on what truly matters: building great software.

Developer Hacks – Modern Command Line Tools and Advanced Git Commands

Terminal Setup for Mac

For Mac users, iTerm2 offers more features than the default Mac terminal app: split panes, hotkey windows, and extensive configurability. Personally, I use it with the Catppuccin Macchiato color theme and especially like its feature of having a scratchpad terminal quickly available via hotkey:

To further enhance productivity, consider using Rectangle for window management, Alt-Tab for window switching and Maccy for clipboard history.

Shell: zsh and others

Zsh, one of the most popular shells and the default on Mac, can be customized with oh-my-zsh, a framework that simplifies the management of zsh configurations. It is often used with the powerlevel10k prompt for an aesthetically pleasing and informative terminal prompt. Additionally, you can enhance your zsh experience with plugins like zsh-autosuggestions or zsh-syntax-highlighting.

zsh with transient prompt (hide full prompt on past commands), git infos, syntax highlighting (valid command shown green) and auto suggestions
zsh with transient prompt (hide full prompt on past commands), git infos, syntax highlighting (valid command shown green) and auto suggestions

Newer, but less common shells are nushell, fish, and xonsh. To me, xonsh looks particularly interesting as it allows mixing both Python and shell commands.

Modern Shell Commands

For many of the classic commands like ls, cat, find etc., there exist modern alternatives. Compared to their traditional counterparts, those modern shell commands are often faster (as they are often written in Rust), more colorful and more convenient, e.g. by respecting gitignore files.

ls alternatives: eza and lsd

eza and lsd both provide a better file listing with color highlighting, icons, and additional configurability.

Output comparison for ls, lsd, eza and eza with additional options
Output comparison for ls, lsd, eza and eza with additional options

bat: cat with wings

As text file viewer, bat uses line numbers, syntax highlighting and git changes. It uses paging for longer output (scroll text like with less), but defaults to plain printing when used for piping.

Viewing an HTML file with bat, including syntax highlighting and Git integration
Viewing an HTML file with bat, including syntax highlighting and Git integration

du to dust

For showing file and folder sizes, dust provides a better sorted and visualized output than the classic du:

Output of file and folder sizes in dust
Output of file and folder sizes in dust

fd: a better find

fd is a modern alternative to find to search for files, much much faster and with cleaner syntax: fd PATTERN instead of find -iname '*PATTERN*'.

RIP, grep: ripgrep

A tool for searching text in files. Using sensible default, ripgrep considers gitignore rules and ignores hidden and binary files. Several times faster than the original grep.

sed to sd

sed is typically used for replacing text. sd does this with an simpler syntax:

Replacing text with simpler syntax in sd
Replacing text with simpler syntax in sd

htop alternatives: bottom, glances and btop

For the classic htop tool to check system status, modern alternatives are for example bottom, glances or btop (there exist even more). Although they largely use the same system metrics, they differ in their customizability and how the display and visualize data:

htop (at the top, left), btop (at the top, right), glances (at the bottom, left), btm (at the bottom, right)
htop (at the top, left), btop (at the top, right), glances (at the bottom, left), btm (at the bottom, right)

fzf: Fuzzy finder

fzf searches text with a so-called fuzzy search: It will also find slightly different spellings, with characters omitted. Text files, shell history or the output of other commands.The fzf-zsh-plugin lets you automatically search your history with fzf when you type ^R.

What the fuck

The tool The Fuck auto-corrects typing errors in console commands. Instead of going back and manually fixing your typo, just enter fuck and it will suggest the correct command (that you can immediately execute with enter):

Suggestion of correct command
Suggestion of correct command

Advanced Git Commands

Apart from everyday’s commands – git init, clone, git pull, add, commit, git push, diff or show – there exist some git features which can make your life considerably easier in many situations. Let me give a git commands overview of these helpful features.

(Global) config options

A few helpful options are (set with git config –global):

  • merge.conflictstyle zdiff3: For merge conflicts, display the original lines in addition to the diverged versions
  • pull.rebase true: Always rebase your local branch if the upstream branch has additional commits
  • push.autoSetupRemote true: When running git push on a local branch, always set up the corresponding remote branch
  • core.pager delta: Use delta to display output of e.g. git diff


Nicer log with
git oneline

The output of git log can be shortened with git log --pretty=oneline and made even prettier with customformatting, e.g. (Source): 

git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

This can be set to an alias via git config --global alias.logline "log –graph …

Output of git log with custom formatting
Output of git log with custom formatting

Cleaning up a feature branch with interactive rebase

Git’s interactive rebase can be used edit commit history by merging commits, editing their messages or dropping some of them. This is convenient when e.g. cleaning up a feature branch before merging. git rebase -i opens a lists of commits in an editor where you can change the default “pick” to whatever command fits. See a detailed description here.

Hunting bugs with git bisect

Imagine the following scenario: Your code is not working properly, but you can’t figure out what’s wrong. You just know that a week ago, everything ran fine.

Git bisect can help you pinpoint the exact commit a bug was introduced. You run git bisect start, mark the good and bad commits (with git bisect good / git bisct bad <hash>). Git will automatically switch to the commit in the middle. You can mark it as good or bad and then git will switch again to commit in the middle between the good and bad commit, repeating this process until the guilty commit was found.

Starting with a bad (step 1) and good (step 2) commit, git bisect will jump to the commits in between (steps 3 and 4) to narrow down the commit where an error was introduced (step 5)
Starting with a bad (step 1) and good (step 2) commit, git bisect will jump to the commits in between (steps 3 and 4) to narrow down the commit where an error was introduced (step 5)

Finding lost stuff with git reflog

Accidentally deleted a branch or lost a commit? Don’t worry, these are still in git! You can use git reflog to get a list of previous commits/branches you were on and find the hash of the deleted object (and then use git checkout to).

List of previous commits/branches
List of previous commits/branches

Switching to another branch with git worktree

Let's say you're working on a feature, and you have many edited or new files. Suddenly, you have to fix a commit on the main branch. You’d typically either commit and stash your changes and switch to main or create another copy of the entire repository somewhere else to have a clean state.

With git worktree, you can create another worktree, something like “a copy of the repo inside your repo”, which you can delete after making changes:

git worktree add ./fix-critical-bug main
# go to the fix-critical-bug directory, commit and push solution on the main branch
git worktree remove ./fix-critical-bug
# return to working on your feature


In conclusion, by integrating these modern command line tools and advanced Git commands into your workflow, you can enhance your productivity and focus on crafting software that creates real business value.

Want to become part of the team?

14 people like this.

1Comments

  • 02.12.2024 08:23 Clock

    Comment editer by attacke

Write a comment
Answer to: Reply directly to the topic

Written by

Dominik Haitz
Dominik Haitz
Senior Data Scientist

Similar Articles

We want to improve out content with your feedback.

How interesting is this blogpost?

We have received your feedback.

Allow cookies?

OTTO and three partners need your consent (click on "OK") for individual data uses in order to store and/or retrieve information on your device (IP address, user ID, browser information).
Data is used for personalized ads and content, ad and content measurement, and to gain insights about target groups and product development. More information on consent can be found here at any time. You can refuse your consent at any time by clicking on the link "refuse cookies".

Data uses

OTTO works with partners who also process data retrieved from your end device (tracking data) for their own purposes (e.g. profiling) / for the purposes of third parties. Against this background, not only the collection of tracking data, but also its further processing by these providers requires consent. The tracking data will only be collected when you click on the "OK" button in the banner on otto.de. The partners are the following companies:
Google Ireland Limited, Meta Platforms Ireland Limited, LinkedIn Ireland Unlimited Company
For more information on the data processing by these partners, please see the privacy policy at otto.de/jobs. The information can also be accessed via a link in the banner.
You can also withdraw your consent at any time without giving any reason by clicking on the button 'Cookie Settings' in the footer of the website and 'Refuse Cookies'.