Git creation date: update date:

Git Implementation Cheatsheet for Beginners

The first time I encountered Git, I immediately loved it. Before learning programming, I never imagined that files could be managed in such a systematic way, clearly showing the differences between versions and who wrote what. If you regret making changes, you can easily revert to previous versions.

With Git, version control and collaboration become much easier and clearer. I even think all non-engineers should learn to use such a version control system for file management.

This article aims to organize my current Git habits (workflows).

Although there are still many Git functionalities I don’t know how to use, I can share my typical usage patterns as a personal implementation cheat sheet, helping beginners quickly get started!

The content is mainly divided into two parts: Basic Workflow and Collaboration, with some additional explanations at the end.

Table of Contents

Basic Workflow

  1. git init

    Initialize a project (start version control in this folder)

  2. touch .gitignore

    Create a file for ignored files

  3. git add .

    After finishing a segment of code changes, add all files to the version control system

  4. git commit -m "[commit message]"

    Submit a change and write your commit message

    Recommended git commit message format (I referenced this article: Recommended git commit message format):

    • feat: Add/modify features
    • fix: Fix bugs
    • docs: Documentation
    • style: Formatting (changes that don’t affect code execution: white-space, formatting, missing semi-colons, etc.)
    • refactor: Refactoring (code changes that neither add features nor fix bugs)
    • perf: Improve performance
    • test: Add tests
    • chore: Changes to build processes or auxiliary tools
    • revert: Revert previous commits

Repeat steps 3 & 4 continuously

  1. GitHub + new repository

    Upload the project to GitHub (follow the instructions provided by GitHub)

  2. git push origin [branch name]

    After committing, push this branch to GitHub

Any time you want to update your code on GitHub to the latest version, execute step 6

Additional: What if you regret after committing (before pushing to GitHub)?

(For detailed information, see What if I regret my commit and want to undo it… - Learn Git for Yourself | Kaiying Lung)

Situation 1: You committed by mistake (but want to keep your code changes in the working directory)

Situation 2: You broke your code (and want to completely revert)

Collaboration

  1. git checkout -b [branch name]

    Create a new branch and switch to it

    Alternative approach:

    1. git branch [branch name]

      Create a new branch

    2. git checkout [branch name]

      Switch to the branch

  2. (Write code…repeating steps 3 & 4 of the basic workflow)

  3. git push origin [branch name]

    Push to GitHub

  4. Go to your repo, write & create a PR (Pull Request)

  5. Copy the PR link and give it to your colleagues (such as supervisors, coworkers, etc.)

  6. (After your colleagues review and merge) git checkout main

    Switch to main

  7. git pull origin main

    Pull the latest changes

    Alternative approach:

    1. git fetch origin

      Fetch remote updates

    2. git merge origin/main

      Merge remote updates

After developing or updating a feature, before creating a PR, it’s best to execute steps 6 & 7, then merge the latest main into your branch. If conflicts occur, it’s better to resolve them locally before creating the PR.

One of the biggest differences between collaboration and solo coding is the need to resolve conflicts, which requires more experience.

Additional: Reviewing others’ PRs

If you want to review locally, use steps 1 + 2:

  1. git fetch origin

    Fetch remote updates

  2. git checkout [branch name]

    Switch to the branch

  3. (After checking and approving) git checkout main

    Switch back to the main branch

  4. git merge [branch name]

    Merge the branch into main

  5. If there are no conflicts, the merge into main will proceed smoothly; if there are conflicts, they must be resolved before merging

    • How to reduce conflict situations?

      1. The art of work distribution
      2. Merge the latest main into your branch before creating a PR
    • Topic for future collaboration discussions: If there are conflicts, should the reviewer merge or the PR creator merge?

      Theoretically, the reviewer should merge into the main branch after checking. However, in case of conflicts, the PR creator might understand the code better (since they wrote it). Should the PR creator resolve conflicts in this case?

Other Knowledge

(I referenced mentor-program-5th/examples/week1)

Other Commands