Git Tricks
Table of Contents
Search the whole git history #
git grep "SEARCH_FOR" $(git rev-list --all)
Revert a file to its staged version (discarding only the unstaged edits) #
git restore PATH_TO_FILE
Restore a single file to its state from N commits ago #
git restore PATH_TO_FILE --source=HEAD~N
(available in Git 2.23+)
Remove untracked files #
git clean -i
GitHub Tricks #
GitHub web editor #
Hidden feature in GitHub web editor: you can move a line of code up or down by holding Option (on Mac) or Alt (on Windows/Linux) and pressing the Up or Down arrow keys!
Automatically sync code snippets in your README with GitHub Actions #
/r/TricksForGeeks/comments/1pk9wr6/automatically_sync_code_snippets_in_your_readme
Generate a license for your repo #
brew install gh
# This pulls the legal text from GitHub's API and saves it as a file
gh repo license view CC-BY-SA-4.0 > LICENSE
Cool stuff #
PS: Smile 🤓 #
Git typo of the day:
git rest --hard
Git Commit Email Privacy in 5 Minutes:
automatic no-reply email, useConfigOnly, and conditional includeIf #
Exposing your commit email is easy; rewriting Git history is hard.
But there's a set-and-forget solution to ensure your Git privacy.
The Core Principles #
- Private Commit Emails. Never commit with your personal or work email again! Both GitHub and GitLab provide automatic, unique no-reply commit email addresses that hide your identity while still correctly attributing contributions to your profile:
- Privacy Guardrail. Set
useConfigOnly = truein your Git configuration to prevent falling back to your system username/hostname (e.g.,user@laptop.local). If no email is set in the config, the commit will simply fail, prompting you to fix it. - Automatic Switching. Use the conditional
[includeIf]block with**/*hostname.com/**as a powerful glob pattern to match both HTTPS (https://) and SSH (git@) remote URLs for the respective hosts. This forces Git to use the correct no-reply email based purely on the repository's remote URL.
Final Config Files #
You'll need the following configuration files. Replace all PLACE_HOLDER values with your actual information.
_NOTE: You have to split the .gitconfig into multiple files to avoid issues with [includeIf], as explained in /a/74012889/5034198_
.gitconfig #
# ====================================================================
# Global Git Configuration
#
# To use this example:
# 1. Save this file as ~/.gitconfig (most common location)
# 2. Replace all PLACE_HOLDER values (e.g., YOUR_FULL_NAME)
# 3. Repeat for .gitconfig-github and .gitconfig-gitlab as necessary
# ====================================================================
[user]
# Set your default name for all commits.
name = YOUR_FULL_NAME
# CRITICAL: Prevents accidental exposure of system email if no
# specific email is found in the conditional blocks below.
useConfigOnly = true
# --------------------------------------------------------------------
# CONDITIONAL OVERRIDES
# These allow you to use different `user.email` based on the URL of
# the repository (e.g., work vs. personal, or GitHub vs. GitLab, etc.)
# --------------------------------------------------------------------
[includeIf "hasconfig:remote.*.url:**/*github.com/**"]
path = .gitconfig-github
[includeIf "hasconfig:remote.*.url:**/*gitlab.com/**"]
path = .gitconfig-gitlab
.gitconfig-github #
# ====================================================================
# GitHub-specific Git configuration
#
# To use this example:
# 1. Get your unique GitHub commit email: https://github.com/settings/emails#toggle_visibility_label
# 2. Copy this file next to your `~/.gitconfig` and replace email below
# ====================================================================
[user]
email = YOUR_GITHUB_ID+USERNAME@users.noreply.github.com
.gitconfig-gitlab #
# ====================================================================
# GitLab-specific Git configuration
#
# To use this example:
# 1. Get your unique GitLab commit email: https://gitlab.com/-/user_settings/profile#user_public_email
# 2. Copy this file next to your `~/.gitconfig` and replace email below
# ====================================================================
[user]
email = YOUR_GITLAB_ID-USERNAME@users.noreply.gitlab.com
How to Verify #
- Clone a repository from GitHub/GitLab.
- Run
git config user.email. It will show your respective GitHub/GitLab no-reply email.
This simple solution ensures your privacy is protected and your commits are correctly attributed, regardless of which hosting platform you're working on.
Shouldn't this be the default configuration for every developer?
Links #
/tricks/git-commit-email-privacy
/r/git/comments/1pf94nt/how_to_avoid_exposing_your_commit_email_private
Credits #
✨ if YOU found this useful — give a star on GitHub or simply join TricksForGeeks on Reddit for more ✨