git checkout HEAD -- my-file.txt
git reset --hard HEAD~1
git branch -d <branch-name>
or force delete with
git branch -D <branch-name>
git push origin --delete <remote-branch-name>
git log <SHA>..master --ancestry-path --merges
git log | grep <pattern>
git blame -L<line#>,+<offset> -- <filename>
For example, three lines starting from line 257 of main.cpp
git blame -L257,+3 -- main.cpp
git log --topo-order --graph -u -L <line-start>,<line-end>:<file>
For example, history of line 155 of main.cpp
git log --topo-order --graph -u -L 155,155:main.cpp
git diff ..<target-branch> <path-to-file>
or if difftool is configured
git difftool ..<target-branch> <path-to-file>
git checkout -b new-branch
modify...
commit...
...
git rebase -i master
(sometimes, I branch out of master for a clean branch and do a git rebase -i clean-branch)
git checkout master
git checkout -b clean
git merge --squash branch_to_merge_to_one_commit
git commit
(add commit message)
git checkout master
git merge clean
git checkout <commit_id> -- file/or/directory
Add to global .gitconfig
using git config --global alias.logp "..."
git log --pretty=format:'%Cred%h %C(yellow)%d%Creset %s %Cgreen(%cr|%ci) %C(bold blue)[%an]%Creset'