DEV Community

David Garcia
David Garcia

Posted on

💡 5 little-known Git commands that will improve your workflow.

There's more to Git than commit, merge and checkout. Here are 5 commands that can make your life easier:

1️⃣ git switch - Switch branches without risk.

Before Git 2.23, git checkout did too many things: switch branches, restore files and more. Now, git switch is a clearer and safer option:

git switch feature-branch # Switch to an existing branch
git switch -c new-branch # Creates and switches to a new branch
Enter fullscreen mode Exit fullscreen mode

No more fear of overwriting files by mistake.

2️⃣ git restore - Revert changes without hassles

To undo changes without modifying the branch or history:

git restore file.js # Discards uncommitted changes  
git restore --staged file.js # Removes changes from the staging area  
Enter fullscreen mode Exit fullscreen mode

Avoid errors with checkout and reset, better separating operations.

3️⃣ git maintenance - Keep your repo fast and clean

Large repos can get slow. git maintenance automates cleanup:

git maintenance start # Triggers automatic maintenance  
git maintenance run # Runs optimization tasks  
Enter fullscreen mode Exit fullscreen mode

Reduces git status, fetch and log time effortlessly.

4️⃣ git sparse-checkout - Work in monorepos without downloading everything

If you only need certain folders from a giant repo:

git sparse-checkout init  
git sparse-checkout set directory1/ directory2/  
Enter fullscreen mode Exit fullscreen mode

Download only what you need and save disk space.

5️⃣ git worktree - Work on several branches without changing directory

If you need to work on different branches without stashing or changing folders:

git worktree add ../feature-branch feature-branch  
cd ../feature-branch  
Enter fullscreen mode Exit fullscreen mode

Ideal for revising code without affecting your main branch.

Translated with www.DeepL.com/Translator (free version)

Top comments (0)