Git Cheatsheet

To delete merged branches

git branch --merged | grep -Ev "(^\*|master|main|dev)" | xargs git branch -d

To retrieve commits between dates

git log --after="2020-03-17" --before="2020-03-19"

To retrieve full commit history of a file

Note: works even for deleted files

If you know path to the file.

git log --all --full-history -- <path-to-file>

If you do not know the path to the file.

git log --all --full-history -- "**/thefile.*"

Reset a file to it’s state in master

git checkout origin/master -- <path-to-file>

Script to pull latest changes from master and rebase the current branch

Filename: pull_and_rebase.sh

#! /bin/zsh

# Pull from latest master and rebase
# the current branch on master

current_branch=$(git rev-parse --abbrev-ref HEAD)
echo "### Rebasing $current_branch on master"
git checkout master
echo "### Pulling latest master"
git pull -r
echo "### Switching back to $current_branch"
git checkout $current_branch
git rebase master
echo "### Rebase complete"
chmod a+x pull_and_rebase.sh
echo "alias pull_and_rebase='./pull_and_rebase.sh'" >> ~/.zshrc
# reload zsh config
zsh -l

Remove ^M chars

In vim

:set ff=unix

Cleaning untracked files

git clean -n          # dry run: list what would be deleted
git clean -f          # delete untracked files
git clean -fd         # also remove untracked directories
git clean -fdx        # also remove ignored files (.gitignore'd — node_modules, .env, build artifacts)
git clean -fdX        # remove ONLY ignored files, keep other untracked ones
git clean -i          # interactive mode

git reset --hard HEAD && git clean -fd