Git

Git is version control software. Use it to track changes to files and revert mistakes. Think of it like a Save Progress button.

Configure

Name and email

List your config.

git config -l

Configure your name and email.

git config --global user.name "Your Name"
git config --global user.email "youremail@yourdomain.com"

Your config is saved in ~/.gitconfig. Edit it with a text editor.

open ~/.gitconfig

Merge tool

Configure VS Code as merge and diff tools.

# Configure VS Code as git mergetool
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd 'code --wait --merge $REMOTE $LOCAL $BASE $MERGED'
git config --global mergetool.keepBackup false

# Configure VS Code as git difftool
git config --global diff.tool vscode
git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'

Tag

List available Git tags.

git tag

Push tag to a remote, e.g., origin.

git push origin ${TAG}

Best practices

  • The main branch is the one source of truth. Feature branches branch from main.
  • Use git rebase instead of git merge to enforce linear history. This makes commits easier to group and track.

Commit

Use Commitizen to write properly formatted commit messages: cz c

Commitizen

Use Commitizen with a semantic release tool ensures that your code has a unique version number. The version number is based on Git history.

Write commit messages to finish the sentence, "This commit will..."

Some examples:

  • "add a link to the contributing guide"
  • "create new file structure for packages"
  • "update broken URL"

Version and release

Snippets

Push a branch from one remote (i.e., origin) to another, preserving its name.

git push "${REMOTE_NAME}" "origin/${BRANCH_HAME}:refs/heads/${BRANCH_HAME}"

Get details about a commit, including its parent, the previous commit. This is useful for rebasing or working with Commitizen.

git show "${COMMIT_HASH}"

Get only the parent commit of a commit.

git rev-parse "${COMMIT_HASH}^"

Free up disk space by collecting garbage and compressing all repositories under the given directory DIR.

find "${DIR}" -type d -name ".git" -execdir git gc \;
Revert commits

Revert commits from the HEAD to a past commit.

git revert --no-commit ${COMMIT_HASH+1}..HEAD
git commit -m 'Revert to ${COMMIT_HASH}'

Resources

Git
Interactive graph
On this page
Configure
Name and email
Merge tool
Tag
Best practices
Commit
Version and release
Snippets
Resources