Git commands 🌲

🌲 = Evergreen note, updated regularly.

A collection of Git commands I use often but tend to forget.

Branches

Create a new branch:
git branch <branch-name>

Switch to a branch:
git switch <branch-name> or git checkout <branch-name>

Create a new branch and switch to it in one go:
git checkout -b <branch-name>

Merge a branch:
Switch to a branch that you want to merge into, then:
git merge <name-of-branch-to-merge>

Delete a branch locally:
git branch -d <name-of-branch-to-delete>

Delete a branch in remote repository:
git push origin --delete <name-of-branch-to-delete>

Remote repositories

List remote repositories:
git remote -v

Change remote repository URL:
git remote set-url origin <new-url>

Tags

Add a tag to a commit:
git tag <tag-name>

Push tags to remote repository (they are not pushed when you push your code):
git push origin --tags

Configuration

List current configurations:
git config -l

Other

Omit files from being checked for changes:
git update-index --assume-unchanged <path-to-file>

Be careful with this command. If you use it on files that you’ve made changes to, these files and changes will effectively become untracked and some operations might silently overwrite them or otherwise lead to the loss of changes. In other words, it is very easy to forget that you’ve set some files to be assumed unchanged and there are no warnings when these files are being affected by some processes. Your best bet is to write the files you’ve set to be assumed unchanged on a Post-It note and stick it on your computer monitor.

Undo the above:
git update-index --no-assume-unchanged <path-to-file>

List files marked with --assume-unchanged:
git ls-files -v