Git
Config#
Sensible git
options to set.
git config --global push.autosetupremote truegit config --global core.excludeFiles ~/.gitignoregit config --global blame.ignoreRevsFile .git-blame-ignore-revsgit config --global pull.rebase falsegit config --global push.useForceIfIncludes true
.idea.DS_Storenode_modules
See more cool git
options explained here.
Usage#
Update branch#
Update my branch with the latest changes from main
.
git fetchgit 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.#
git switch -c $BRANCH_NAME
Go back to previous branch.#
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.
git push --force-with-lease
Pull#
git pull --rebase
Abort if there are conflicts
git rebase --abort
Pull with merge
to resolve conflict
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.
[blame] ignoreRevsFile = .git-blame-ignore-revs
Example of a .git-blame-ignore-revs
file.
# Enforced single quotes over double quotes7e9386274b3b969b70ae4beda1b3321812c1c512
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.