Git maintenance advanced command

alex_ber
3 min readFeb 20, 2025

--

List of less frequently used Git commands

User name and email

To set them per Git repo:

git config user.name '<USER_NAME>'
git config user.email '<USER_EMAIL>'

To set them globally:

git config --global user.name '<USER_NAME>'
git config --global user.email '<USER_EMAIL>'

Discard all local changes

git reset --hard

Fetches updates from all remotes and cleans up local references to deleted remote branches

git fetch --all --prune

Force pull from another branch

git reset --hard origin/<ANOTHER_BRANCH>

Safely force-push local changes to the remote branch, but only if the remote branch hasn't been changed since your last fetch or pull

git push --force-with-lease origin <BRANCH>

Sync Git local branches with remotes

Step 1: Updates your local information about the remote branches.

--prune flag removes local references (remote-tracking branches)
for branches that no longer exist on the remote.

git fetch --prune

Step 2: Get a list of local branches that no longer have a corresponding remote branch

git branch -vv

Step 3: Delete local branches with missing remotes

git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -d

Step 4: If some branches are not fully merged and you still want to delete them, you can force deletion

git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -D

Note: It is recommended to also run maintenance commands (see below).

Maintenance

Step 1: Remove unreachable objects

git gc --prune=now --aggressive

Step 2: Prune remote tracking branches

git remote prune origin

Step 3: Pack and repack the repository

git repack -Ad

Step 4: Verify repository integrity

git fsck --full

Step 5: Push changes to the remote repository

git push origin --force --all
git push origin --force --tags

Why You Should Set Git’s core.autocrlf Globally

If you’ve worked on software across Windows, macOS, or Linux, you’ve likely seen Git diffs go haywire — entire files marked as changed over a single-line edit. The culprit? Inconsistent line endings.

This causes:

  • Noisy diffs obscuring real changes.
  • Accidental whitespace commits.
  • Broken scripts sensitive to line endings.
  • Frustrating merge conflicts based only on line endings.

Git’s core.autocrlf setting fixes this automatically.

Line Endings 101

  • Windows: Uses CRLF (Carriage Return + Line Feed)
  • Linux & macOS: Use LF (Line Feed)

Mixed endings confuse Git when collaborators use different systems.

How core.autocrlf Works

This setting tells Git how to handle line endings during commits and checkouts.

On Windows: Set it to true.

git config --global core.autocrlf true
  • On Commit: Converts your local CRLF to LF before storing in the repo.
  • On Checkout: Converts repo LF back to CRLF for your local files.
  • Result: Keeps the repository LF-consistent, while letting you work naturally on Windows.

On macOS & Linux: Set it to input.

git config --global core.autocrlf input
  • On Commit: Converts any accidental CRLF to LF before storing.
  • On Checkout: Makes no changes (expects/keeps LF).
  • Result: Prevents accidental CRLF commits while preserving native LF locally.

Why Use — global?

Setting this globally applies it to all your repositories, ensuring:

  • Consistency: Predictable behavior across all projects.
  • Prevention: Stops line ending issues before they pollute your history.
  • Collaboration: Simplifies teamwork by standardizing how your system handles endings.

Set it once with — global, and enjoy cleaner Git histories and smoother teamwork.

--

--

alex_ber
alex_ber

Written by alex_ber

Senior Software Engineer at Pursway

No responses yet