Skip to content

Git

Config#

Sensible git options to set.

Terminal window
git config --global push.autosetupremote true
git config --global core.excludeFiles ~/.gitignore
git config --global blame.ignoreRevsFile .git-blame-ignore-revs
git config --global pull.rebase false
git config --global push.useForceIfIncludes true
~/.gitignore
.idea
.DS_Store
node_modules

See more cool git options explained here.

Usage#

Update branch#

Update my branch with the latest changes from main.

Terminal window
git fetch
git merge origin/main

git switch#

git switch is the new limited version of git checkout see this StackOverflow thread for more details.

Create a branch.#

Terminal window
git switch -c $BRANCH_NAME

Go back to previous branch.#

Terminal window
git switch -

Force push#

Force push only if your changes are the latest changes on the remote. This prevents you from overwriting someone else’s changes. See more details.

Terminal window
git push --force-with-lease

Pull#

Terminal window
git pull --rebase

Abort if there are conflicts

Terminal window
git rebase --abort

Pull with merge to resolve conflict

Terminal window
git pull --no-rebase # or use `git pull` with `git config --global pull.rebase false`

Ignore specific commits in git blame#

Sometimes, you will commit large formatting changes which makes git blame fairly useless because almost every line will have the formatting change as its most recent commit.

With git config blame.ignoreRevsFile .git-blame-ignore-revs, you can create a .git-blame-ignore-revs file containing commit SHAs that should be ignored by git blame in VSCode and GitHub. GitHub supports .git-blame-ignore-revs files natively without configuration.

~/.gitconfig
[blame]
ignoreRevsFile = .git-blame-ignore-revs

Example of a .git-blame-ignore-revs file.

.git-blame-ignore-revs
# Enforced single quotes over double quotes
7e9386274b3b969b70ae4beda1b3321812c1c512

Store large files in git#

git works best with text files and while you can version other file types with it, it is usually not advised. Instead, you can use Git LFS to manage these larger files.

Securing GitHub SSH Key#

It is a best practice to secure your SSH keys using a passphrase but it can be annoying to enter your passphrase every time you want to push / pull from GitHub.

You can reduce the number of times you have to enter your passphrase by adding your ssh key to the ssh-agent.

References#