Today I Learned

Git commands to note

April 15, 2021

Undo git reset

$ git reset 'HEAD@{1}'

Stash only necessary files

  1. Stage all your files you need to stash
  2. This command will create a stash with ALL of your changes (staged and unstaged), but will leave the staged changes in your working directory (still in state staged):
$ git stash --keep-index
  1. Run git stash save “good stash”, now your “good stash” has ONLY staged files.

Rename Case sensitive: from casesensitive to CaseSensitive, two steps:

$ git mv casesensitive tmp
$ git mv tmp CaseSensitive

Remove untracked files, include .gitignore files:

$ git clean -X -dn // to check all files will be removed
$ git clean -X -fd // to confirm action

SSH: Add multiple ssh keys

ref: https://docs.gitlab.com/ee/ssh/#configure-ssh-to-point-to-a-different-directory

SSH: Avoid enter passphrase every time

ref: https://stackoverflow.com/a/41145954/10649754

Configuration ~/.ssh/config

Host *
  UseKeychain yes
  AddKeysToAgent yes
  IdentityFile ~/.ssh/id_rsa

Change commit author

$ git config --global user.name "John Doe"
$ git config --global user.email "john@doe.org"

Assume unchanged:

$ git update-index --assume-unchanged [file_paths]

Undo assume unchanged:

$ git update-index --no-assume-unchanged [file_paths]

List all assume unchanged files:

$ git ls-files -v | grep "^[a-z]"

Completely remove a file from the history

$ git filter-branch --index-filter \
  'git rm --cached --ignore-unmatch path/to/mylarge_50mb_file' \
  --tag-name-filter cat -- --all

Recover files from deleted/popped stashes:

ref: https://dev.to/meduzen/recover-a-lost-git-stash-in-two-steps-569

  1. List lost stashes
$ git fsck --unreachable | grep commit | cut -d ' ' -f3 | xargs git log --merges --no-walk
  1. Send a lost stash back where it comes from
$ git update-ref refs/stash 4b3fc45c94caadcc87d783064624585c194f4be8 -m "My recovered stash"

Clean up git

$ git gc --prune=now --aggressive

Apply .gitignore

$ git rm -r --cached .
$ git add .
$ git commit -m "Untrack files in .gitignore"

Remove tags

  1. At local
$ git tag -d [tagname]
  1. At remote
$ git push --delete origin [tagname]

Reset login password

Authentication failed for 'https://______.git'

$ git config --global --unset user.password

Then run your git command (ex. git push) and reenter username and password.


© 2026 - Written by Vuong Vu. Connect with me on LinkedIn.