Git commands to note
April 15, 2021
Undo git reset
$ git reset 'HEAD@{1}'Stash only necessary files
- Stage all your files you need to stash
- 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- 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 CaseSensitiveRemove untracked files, include .gitignore files:
$ git clean -X -dn // to check all files will be removed
$ git clean -X -fd // to confirm actionSSH: 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_rsaChange 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 -- --allRecover files from deleted/popped stashes:
ref: https://dev.to/meduzen/recover-a-lost-git-stash-in-two-steps-569
- List lost stashes
$ git fsck --unreachable | grep commit | cut -d ' ' -f3 | xargs git log --merges --no-walk- 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 --aggressiveApply .gitignore
$ git rm -r --cached .
$ git add .
$ git commit -m "Untrack files in .gitignore"Remove tags
- At local
$ git tag -d [tagname]- At remote
$ git push --delete origin [tagname]Reset login password
Authentication failed for 'https://______.git'
$ git config --global --unset user.passwordThen run your git command (ex. git push) and reenter username and password.