Skip to content
clydeu edited this page Jun 29, 2020 · 13 revisions

Reindex all files

git rm -r --cached .

git add .

git commit -m <message>

Squash Commits

git rebase -i HEAD~<number of commits>

git push -f

Recover deleted files

git checkout <deleting_commit>^ -- <file_path>

Remove local remote branch (remote branches that have since been deleted from the server)

git remote prune origin --dry-run

git remote prune origin

delete merged branches except master

git branch --merged | egrep -v ^[*] | egrep -v master$ | xargs -r git branch -d

Reverse specific files to previous commit

git checkout <commit hash> -- file1/to/restore file2/to/restore

git checkout <commit hash>~<number of commits to rollback> -- file1/to/restore file2/to/restore

git checkout HEAD~<number of commits to rollback> -- file1/to/restore

Add an alias for git

%UserProfile%\.gitconfig

[alias]

cm = !sh -c 'git fetch && git checkout master && git pull'

gone = ! "git branch --merged | egrep -v ^[*] | egrep -v master$ | xargs -r git branch -D && git remote prune origin && git for-each-ref --format '%(refname:short) %(upstream:track)' | awk '$2 == \"[gone]\" {print $1}' | xargs git branch -D"

Git hooks

  • Use git hooks to run some scripts before actually committing.

https://githooks.com/

Example pre-commit script

#!/bin/sh

set -v

npm run lint-src-fix
if [ $? -ne 0 ]; then
  exit 1
fi

git add -u

--verbose
if [ $? -ne 0 ]; then
  exit 1
fi

Make rebase the default pull

git config --global pull.rebase true

Clone this wiki locally