Skip to content

Instantly share code, notes, and snippets.

@M-Razavi
Last active October 25, 2024 10:15
Show Gist options
  • Save M-Razavi/3190bb770f3a66dee31d1d5875cf77ff to your computer and use it in GitHub Desktop.
Save M-Razavi/3190bb770f3a66dee31d1d5875cf77ff to your computer and use it in GitHub Desktop.
Git commands

!! be very careful with these !! you may end up deleting what you don't want to

remove untracked

git clean -f -d

CAUTION: as above but removes ignored files like config.

git clean -f -x -d

CAUTION: as above, but cleans untracked and ignored files through the entire repo (without :/, the operation affects only the current directory)

git clean -fxd :/

Delete all local branches but keeping others like "develop" and "master":

git branch | grep -v "develop" | grep -v "master" | grep -v "main" | xargs git branch -D

removes staged and working directory changes

git reset --hard

Setting your branch to exactly match the remote branch can be done in two steps:

git fetch origin git reset --hard origin/master

If you want to save your current branch's state before doing this (just in case), you can do:

git commit -a -m "Saving my work, just in case" git branch my-saved-work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment