Useful Git Commands

(Last edited: October 29, 2019)

Simple Commands

Command Description
git alias Display a list of available aliases
git clean -fd Delete untracked files and folders
git commit --allow-empty Allow empty commit with no change
git push origin --delete <BRANCH_NAME> Delete remote branch
git rebase -i HEAD~<N> Squash multiple (N) commits into a potentially single commit
git reset -- <DIRECTORY_FILE_PATH> Unstage a file/directory and its contents
git reset --hard origin/<BRANCH_NAME> Reset local branch to remote branch
git stash apply <N> Apply a specific <N> stash which can be chosen from git stash list

Steps

Rename Git Branch Locally and Remotely

Command Description
git branch -m <OLD_BRANCH> <NEW_BRANCH> Rename branch locally
git push origin :<OLD_BRANCH> Delete old remote branch
git push --set-upstream origin <NEW_BRANCH> Push the new branch and set local branch to track the new remote branch

Squash Last N Commits into 1

Command Description
git reset --soft HEAD~<N> Soft reset the current branch head back N commits
git commit Record changes
git push --force-with-lease Force push only if the remote refs have their current value to be the same as the remote-tracking branch

Helpful Links: