Git commands I find useful
One day I'll memorise these but until then, here are some basic Git commands:
Push changes to a remote repository
# Stage all changes
git add --all
# See staged changes
git status
# Commit staged changes
git commit -m "Your comment here"
# Push all local commits to the remote branch
git push
General
# Check out an existing local branch:
git checkout <local-branch>
# Compare local changes to the remote in a local branch
git diff origin/<MAIN-BRANCH-NAME>..HEAD
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>