Handy Git Commands v1

Git notes

I have a list of operations below I often find myself looking for online, collected here for a easy retrieval. Please don't take my word that these will work for you or are correct, test in a sandbox and use carefully! Git workflows are complex, I use these primarily on rebase based workflows. These may be less helpful for other ways of working with git.

Replace one branch with another

# Make sure your working tree is in a clean state
git status
git checkout some-branch
git reset --hard target-branch

Reset your local branch to main

Helpful for undoing weird local branch state. Assumes your local origin is correct, otherwise you will likely need to repair this using fetch first.

git reset --hard origin/main

Get rid of all unstaged changes

git restore .

Squash a number of commits into 1

git reset --soft HEAD~<number of commits>

Move last commit to git staging

git reset .