Command line

Command line operators

Command line operators are special characters that help you control how commands work and interact with each other. Think of them as the "glue" that connects different commands together or directs where information should go. These operators solve common problems like saving command output to a file, running multiple commands in sequence, or using the output of one command as input for another.

Without operators, you'd have to manually copy and paste outputs between commands or run each command separately and check if it succeeded before running the next one. Operators make your workflow more efficient and help automate repetitive tasks.

Redirection operators (>, >>)

Take the output of a command and send it to a file or another destination instead of displaying it on the screen. This is useful for saving command results or logs.

Here's how to remember the difference:

  • >: 1 arrow = keep 1 thing = keep mine
  • >>: 2 arrows = keep 2 things = keep yours and mine

Write a piece of text to a file:

echo "Hello, World!" > hello.txt

Append last 5 commits to a changelog file:

git log -5 --oneline >> CHANGELOG.md

Pipe operator (|)

Takes the output from one command and sends it as input to another command. This creates a "pipeline" where data flows from one command to the next, like water flowing through connected pipes.

Select commits that contain the word "fix" in their message:

Linux/macOS:

git log --oneline | grep "fix" | sort

PowerShell:

git log --oneline | Select-String "fix" | Sort-Object

Logical operators (&&, ||)

Like operators in programming languages, logical operators control the flow of command execution based on the success or failure of previous commands.

  • &&: Runs the next command only if the previous command succeeded (exit code 0).
  • ||: Runs the next command only if the previous command failed (non-zero exit code).

Add files and commit only if add succeeds:

git add . && git commit -m "Update documentation"

Try to pull; if it fails, then just fetch:

git pull origin main || git fetch origin
© 2024-2025 GitByBit.All rights reserved.

Hi! I'm Alex, creator of GitByBit.

And this is Gitopedia—a reference that summarizes knowledge in my Git course.

Gitopedia is designed to be a personal knowledge base. When you progress through the main Git course in VS Code, you unlock all of these topics, one by one, bit by bit. This gives you time to digest the information and gradually build the mental model of Git. Here's an example of how it works:

Learning directly in VS Code lets you practice Git exactly as you would in real life, doing real jobs, writing real code. In addition, the course has access to your actual terminal, so it can point out mistakes, suggest workarounds, etc.

The course is FREE, there are no Ads or other bullshit. There are optional premium add-ons you can purchase, mainly to support my work (regrettably, I have to eat every day), but that's totally up to you.

Learn Git in VS Code