Git commands I find useful

I can never remember these so here they are:

General

# Check out an existing local branch:
git checkout <local-branch>

Branches

# Create a new branch from the currently checked out local branch and check it out:
git checkout -b <new-branch>
# Checkout a tagged branch and create a new branch from it.
# If a branch has been tagged you can see the code at a particular point:
git checkout tags/<tag-name> -b <new-branch-name>

Merging

# Merge changes from a local branch into a currently checked out branch:
git merge <existing-local-branch>
# Merge changes from a remote branch into a currently checked out branch:
git merge origin/<remote-branch>

Remote branches

# Switch to a new local branch, and pull changes from a specific remote branch:
git switch -c <new-local-branch> origin/<remote-branch>
# Same as above but without tracking the remote:
git switch --no-track -c <new-local-branch> origin/<remote-branch>
# Get the latest changes from a remote branch:
git pull origin <remote-branch>

Deleting

# Delete a local branch:
git branch --delete <local-branch>
# Delete a remote branch:
git push origin --delete <remote-branch>

Reverting changes

# Get your local branch to match your remote branch.
git reset --hard origin/<remote-branch>