Skip to content

Files

Latest commit

 

History

History

Version Control and Git Basics

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Readme.md


Version Control and Git Basics

Introduction

Version control systems (VCS) are essential tools that allow developers to manage changes to source code over time. Git, one of the most widely used distributed version control systems, tracks project history, enables collaboration, and simplifies code version management. This documentation covers the fundamentals of version control and Git, providing the essential commands and workflows you need to get started.


Table of Contents

  1. What is Version Control?
  2. Types of Version Control Systems
  3. Introduction to Git
  4. Setting Up Git
  5. Basic Git Commands
    • git init
    • git clone
    • git add
    • git commit
    • git push
    • git pull
  6. Branching and Merging
  7. Undoing Changes
  8. Git Workflows
  9. Git Best Practices
  10. Conclusion

What is Version Control?

Version control is the process of tracking and managing changes to software projects, documents, or files. It allows multiple people to work on a project simultaneously, track changes, and revert to previous versions if necessary.

Key benefits include:

  • Collaboration: Multiple developers can work on the same project concurrently.
  • History: Provides a record of all changes made to a project over time.
  • Backup: If something breaks, you can easily revert to a previous stable version.
  • Branching: You can create isolated branches for different features or bug fixes, avoiding conflicts.

Types of Version Control Systems

  1. Local Version Control: Stores versions on a local machine. Example: Revision Control System (RCS).
  2. Centralized Version Control: A single server stores all versions, and clients check out files. Example: Subversion (SVN).
  3. Distributed Version Control: Every client has a full copy of the repository, enabling offline work and distributed collaboration. Example: Git.

Introduction to Git

Git is a distributed version control system created by Linus Torvalds in 2005. Git enables users to keep track of project history, collaborate with others, and manage code versions in a distributed manner.

Key features:

  • Distributed: Every developer has a full copy of the project history.
  • Branching: Lightweight and flexible branches allow for parallel development.
  • Speed: Git performs operations quickly, even with large repositories.
  • Integrity: Git ensures the integrity of the codebase using cryptographic hashes (SHA-1).

Setting Up Git

  1. Install Git:

    • On Linux:
      sudo apt install git
    • On MacOS:
      brew install git
    • On Windows: Download Git
  2. Configure Git: After installation, configure Git with your username and email:

    git config --global user.name "Your Name"
    git config --global user.email "your.email@example.com"
  3. Check Configuration: Verify your Git configuration:

    git config --list

Basic Git Commands

git init

Initializes a new Git repository in your project folder.

git init

git clone

Clones an existing Git repository from a remote server (e.g., GitHub, GitLab) to your local machine.

git clone https://github.com/username/repository.git

git add

Stages files for commit. Files added with git add are tracked by Git and included in the next commit.

git add file_name
git add .

git commit

Commits staged changes to the local repository with a message.

git commit -m "Commit message"

git push

Pushes your committed changes to a remote repository.

git push origin branch_name

git pull

Fetches and merges changes from a remote repository to your local repository.

git pull origin branch_name

Branching and Merging

Branches are used to create independent lines of development. They allow you to work on features, bug fixes, or experiments without affecting the main codebase.

Create a new branch:

git branch branch_name

Switch to a branch:

git checkout branch_name

Merge changes from one branch to another:

Switch to the target branch (e.g., main) and merge changes:

git checkout main
git merge branch_name

Undoing Changes

  1. Undoing staged changes: Unstage a file that was added to the staging area:

    git reset file_name
  2. Undoing committed changes: Use git reset to undo a commit and reset your working directory to a previous state:

    git reset --hard commit_hash
  3. Reverting a commit: Create a new commit that undoes the changes of an earlier commit:

    git revert commit_hash

Git Workflows

  1. Feature Branch Workflow:

    • Create a new branch for each feature you're working on.
    • Merge it back into the main or develop branch after review.
  2. Forking Workflow:

    • Fork a repository, work on it independently, and submit pull requests to propose changes.
  3. GitFlow Workflow:

    • Utilizes main, develop, feature, and release branches to maintain stable releases and ongoing development.

Git Best Practices

  1. Write Descriptive Commit Messages: Use clear, concise messages to describe the changes.
  2. Commit Frequently: Smaller, frequent commits make it easier to track progress and debug.
  3. Use Branches: Isolate development work on feature branches to keep the main codebase stable.
  4. Pull Before Pushing: Always git pull to synchronize your local changes with the remote repository before pushing.
  5. Review Before Merging: Use code reviews to ensure the quality and consistency of the code before merging branches.

Conclusion

Git is an essential tool for modern software development, offering powerful version control features that make collaboration easier and more effective. By mastering basic Git commands and workflows, you can confidently manage and track code changes in any project, whether working solo or in a team.