Git Basics Guide

website·Indexed 30 days ago

https://gongarce.github.io/git-basics

A beginner-friendly guide to version control and Git. Covers fundamental concepts including Version Control Systems (VCS), Distributed Version Control Systems (DVCS), basic configuration with git config, creating and cloning repositories, committing changes, and pushing and pulling from remote repositories.

Tokens
8.9K
Snippets
81
Records
96
Agent score
71%

What's inside Git Basics

  1. Understand Git branching concepts

    In Git, branches are lines of work that allow developers to make changes to a codebase without affecting the main branch or other team members' work. All changes are added sequentially to the version history.

    Key concepts include:

    • Isolation: Branches group related changes for a specific development task.
    • Merging: Once work on a branch is complete, it can be merged (merge) back into another branch (e.g., the main branch).
    • Lifecycle: Branches are typically short-lived; they are created from a specific point in history, used for a task, merged, and then deleted.
    • Reference Point: A new branch is created from the current point in the history, which serves as the reference point when merging back to the parent branch.
  2. Understand the purpose of Git branches

    Branches in Git act as independent lines of work, allowing developers to make changes to a codebase without affecting the main branch or other team members' work. This enables grouping all changes related to a specific feature into a single unit. New branches start from the current point of the repository. Once work on a branch is complete, it can be merged back into a parent branch (like the main branch) and then deleted.
  3. Update local branch from associated remote branch using git pull

    When a local branch is already associated with a remote branch, use git pull to download the change history since the last common commit. This command updates the local branch state and provides information about modified files and branch status.
    git pull
  4. Understand Distributed Version Control Systems (DVCS) concepts

    Distributed Version Control Systems (DVCS) differ from centralized systems by providing every collaborator with a full copy of the repository, including the entire project history, all revisions, and all branches. Key advantages include:

    • Full Local Backups: Every local clone acts as a complete backup of the project, mitigating data loss if a central server fails.
    • Offline Capability: Developers can perform commits, create branches, and merge changes without an internet connection.
    • Flexible Collaboration: Changes can be exchanged directly between local repositories or synchronized via a central server.
    • Efficient Branching and Merging: Local control over branches allows for experimentation and feature development without affecting other team members until the code is ready to be merged.

    Common examples of DVCS include Git and Mercurial, while SVN (Subversion) is a primary example of a centralized system.

  5. Understand the purpose and benefits of Version Control Systems (VCS)

    A Version Control System (VCS) is a tool used to maintain a detailed record of all changes made to a project's source code. It enables developers to track who made specific changes and when, revert to previous versions, and merge contributions from multiple collaborators. Using a VCS prevents common development issues such as accidentally overwriting a teammate's work, coordination difficulties during collaboration, and the loss of project history (the 'how' and 'why' behind changes).
  6. Understand Distributed Version Control Systems (DVCS) architecture

    Distributed Version Control Systems (DVCS) differ from centralized systems by providing every collaborator with a complete copy of the repository. This includes the full project history, all past changes, and all branches, rather than relying on a single central server for the history. This architecture ensures that each local copy acts as a full backup of the project.
  7. Understand the Git commit process

    A commit is a snapshot of the current version of a project. When changes are made locally, they reside in the working directory. To make these changes permanent in Git's history, they must be moved to the staging area (index) and then committed. Each commit is assigned a unique identifier called a hash, which allows developers to reference or revert to that specific state of the project.
  8. Understand the purpose of Version Control Systems (VCS)

    A Version Control System (VCS) is a tool used in software development to maintain a detailed record of all changes made to a project's source code. It allows developers to track who made a change, when it occurred, revert to previous versions, and merge contributions from multiple collaborators. VCS prevents accidental overwriting of work, simplifies collaboration among multiple developers, and provides a historical audit trail of why and how changes were implemented.
  9. Ignore files using .gitignore

    Prevent specific files or directories from being tracked by Git by adding them to a .gitignore file in the project root. Common patterns include build outputs, dependency folders, logs, and environment variables.
    # build output
    dist/
    # generated types
    .astro/
    
    # dependencies
    node_modules/
    
    # logs
    npm-debug.log*
    yarn-debug.log*
    yarn-error.log*
    pnpm-debug.log*
    
    # environment variables
    .env
    .env.production
    
    # macOS-specific files
    .DS_Store
  10. Clone a remote Git repository

    Cloning creates a local copy of a remote repository, including all files, directories, commit history, and branches. It also automatically configures the remote URL as a remote named origin in the local copy. You can clone into a folder named after the repository by providing the URL, or specify a custom directory name as a second argument.
    # Clone into a folder named after the repo
    git clone https://github.com/GonGarce/git-basics.git
    
    # Clone into a specific directory named 'my-repo'
    git clone https://github.com/GonGarce/git-basics.git my-repo
  11. Safely experiment with changes in Git

    To avoid breaking a stable codebase while experimenting, follow these safety practices:

    1. Create a new branch before making uncertain changes to ensure the source branch remains unaffected.
    2. Commit your changes and push them to a remote repository to create a safe backup.
    3. Leverage Git's distributed nature: if a local repository is accidentally broken, it can be recovered from the remote repository.