Git

Git, initially developed by Linus Torvalds for Linux development, is a distributed version control system that tracks file changes over time. It is essential for developers to collaborate effectively and maintain a history of their work.

Repository, Commit, and Remote

  • Use git init to create a new repository, which adds a .git folder to the current directory.

  • For existing repositories, clone with git clone git@github.com:<namespace>/<project>.git.

  • Run git status to check the status of files in the repository:

    • Untracked: New files not added yet.
    • Modified: Tracked files that have been altered but not staged.
    • Added: Files staged for commit.
    • Ignored: Files specified in .gitignore that won't show up in git status.
  • Use git add <file> to stage a specific file for commit, or git add . to stage all modified and untracked files.

  • Create a commit with git commit -m 'message', which cleans the workspace by committing staged changes.

  • Add a remote repository with git remote add origin git@github.com:<namespace>/<project>.git, designating it as "origin."

  • Push commits to the "main" branch with git push -u origin main, setting "origin" as the default remote.

  • Use git pull to fetch and merge changes from a remote repository into the current branch.

It is a tree

When you run git init, you create the initial node, or root of the tree.

  • Use git branch -b my-branch to create a new branch named "my-branch."

  • Each commit, executed with git commit -m 'message', adds a new node to that branch.

  • Branches can be created from existing branches.

  • Switch between branches with git checkout <branch>.

Merging

Typically, you develop code on a private branch. Once complete, push it to the remote and start a "Pull Request" to request merging into the main branch. Resolve any conflicts beforehand.

Conventional Commit

Conventional Commits is a standardized format for commit messages that helps convey the intent and impact of changes. This consistency aids developers in understanding commits during reviews or while analyzing project history.

<type>(<scope>): <subject>

<body>

Examples:

git commit -m \
  "feat(api): send an email to the customer when a product is shipped"

Commonly used types:

  • feat: A new feature
  • fix: A bug fix
  • refactor: A change that neither adds a feature nor fixes a bug
  • test: Adding or refactoring tests
  • docs: Documentation changes
  • chore: Other updates (e.g., tooling, configuration)

Git Is a Powerful Tool

Here is more to explore:

  • Aliases
  • Rebasing
  • Tags
  • Stashing
  • Cherry-picking
  • Workflows
  • Handling large files
  • Integration with IDEs