📖 Using Git

Description

Git is a powerful version control system used by developers to manage changes to code over time. Whether you're working on a personal project or collaborating with a team, Git helps track changes, revert mistakes, and maintain an organized development workflow.

Getting Started

Git organizes your work into repositories, allowing you to save versions of your project as it evolves. This involves three key steps:

Modified
Files have been edited but are not yet tracked by Git.
Staged
Changes are marked for inclusion in the next commit.
Committed
Changes are saved to the repository as a new version.

Git is tightly integrated with VS Code, offering an intuitive GUI for managing your projects. Alternatively, you can use Git Bash for command-line interaction.

Create a Git Repository

VS Code menu showing tools and repository initialization button

Using VS Code

  1. Open a folder in VS Code where your project files are stored.
  2. Click the Source Control icon in the left-hand toolbar.
  3. Click Initialize Repository to create a new repository in the selected folder.
  4. Press Ctrl+Enter to create an initial commit with a required commit message, such as "init commit."
  5. Git is now tracking your project files.

Using Git Bash

  1. Open your project folder in VS Code and launch the terminal (Ctrl+`).
  2. Run the following commands:
    git init
    git add .
    git commit -m "init commit"
  3. Git will now track all files in the repository.

Note: The first commit skips the staging step for simplicity. Future commits allow more granular control over what changes to include.

Managing Content Changes

Git menu showing file update status

Using VS Code

To manage changes to your project content, Git needs you to initiate the tracking. You do this through a process of staging and committing changes. Git monitors your project for changes and can tell if a file is new (untracked) or modified. It won't save the changes to the version control system until you stage and/or commit the changes.

  1. Click the Source Control icon in the VS Code toolbar.
  2. Files with changes will appear under Changes, marked with:
    • U for untracked files
    • M for modified files
    • D for deleted files
  3. Hover over a file and click the + icon to stage it.
  4. Write a descriptive commit message and click Commit to save the changes.

Reviewing File Changes

In VS Code, you can preview changes by clicking on a file in the Changes list. A side-by-side view will display the old content (highlighted in red) and the new content (highlighted in green). This feature helps ensure accuracy before committing.

Git Review Changes in VS Code

Command Line Interface

Git menu showing file update status

While VS Code provides a GUI, learning Git's CLI commands can be invaluable for understanding its core operations. Here are some commonly used commands:

Check Git Status
git status
Stage Files
git add [file] or git add .
Commit Changes
git commit -m "message"
View Log
git log

Cheat Sheets

Mastering Git takes time, but cheat sheets can help streamline the learning process. Explore these resources: