Git#

Git is a distributed version control system that is widely used for source code management. It is designed to handle everything from small to very large projects with speed and efficiency. Git is essential for collaboration, version tracking, and maintaining project history.


Why Use Git?#

  • Collaboration: Multiple developers can work on the same project without overwriting each other’s changes.

  • Version Control: Git keeps a history of all changes, enabling you to revert to previous versions if needed.

  • Branching and Merging: You can work on new features or bug fixes in isolated branches and merge them back when complete.

  • Distributed Workflow: Unlike centralized version control systems, every developer has a full copy of the repository.


Installation#

To install Git:

  1. Visit the Git website and download the installer for your operating system.

  2. Run the downloaded installer and follow the installation instructions. During installation, you may be prompted to:

    • Set up your default text editor (e.g., VS Code).

    • Adjust your PATH environment variable (recommended).

Verify Installation#

After installation, verify that Git is installed correctly by running the following command in your terminal:

git --version

You should see the installed version of Git.


Configuration#

After installing Git, you need to configure some basic settings.

Set Username and Email#

Run these commands to configure your name and email (these will appear in your commits):

git config --global user.name "Your Name"
git config --global user.email "Your Email"

Check Your Configuration#

To view your current Git configuration, run:

git config --list

This will display all globally set options.


Basic Usage#

Initialize a New Repository#

To create a new Git repository:

  1. Navigate to your project directory:

    cd /path/to/your/project
    
  2. Initialize the repository:

    git init
    

    This creates a .git folder in your directory, which stores all version control information.

Add Files to Staging#

Add files to the staging area (prepare them for commit):

git add <file_name>  # Add a specific file
git add .            # Add all changes in the current directory

Commit Changes#

Create a snapshot of your staged changes:

git commit -m "Descriptive commit message"

Tip: Use clear, concise commit messages that describe what you changed.

Connect to a Remote Repository#

To link your local repository to a remote repository (e.g., on GitHub):

git remote add origin <repository_url>

Verify the remote connection:

git remote -v

Push Changes to a Remote Repository#

Push your commits to the remote repository:

git push -u origin main  # Push the current branch and set upstream

Pull Changes from a Remote Repository#

Update your local repository with the latest changes from the remote:

git pull origin main

Clone an Existing Repository#

Copy an existing repository to your local machine:

git clone <repository_url>

This creates a new directory containing the repository.


Branching and Merging#

Branches allow you to work on new features or fixes without affecting the main codebase.

Create a New Branch#

git checkout -b feature_branch

This creates and switches to a new branch named feature_branch.

Switch Between Branches#

git checkout main

Switch to an existing branch.

Merge Branches#

To merge changes from one branch into another:

  1. Switch to the target branch (e.g., main):

    git checkout main
    
  2. Merge the feature branch:

    git merge feature_branch
    

Tip: Resolve any merge conflicts manually, then stage and commit the resolved files.


Common Git Commands Cheat Sheet#

Command

Description

git status

Show the status of your working directory

git log

View commit history

git diff

Show changes not yet staged

git branch

List all branches in the repository

git stash

Temporarily save changes without committing

git rebase branch_name

Reapply commits from one branch onto another

git tag -a v1.0 -m "Tag msg"

Create an annotated tag


Best Practices#

  1. Commit Often: Make small, logical commits with meaningful messages.

  2. Branching: Use branches for features, fixes, and experiments. Keep main or master clean and deployable.

  3. Code Reviews: Push changes to remote branches and create pull requests for review.

  4. Document Workflow: Define naming conventions for branches (e.g., feature/, bugfix/) and commits.

  5. Avoid Large Commits: Commit only related changes in a single commit.

  6. Pull Before Push: Always pull the latest changes before pushing to avoid conflicts.


This guide covers essential Git concepts and commands to get you started with version control. With consistent practice and adherence to best practices, you can harness Git to streamline collaboration and maintain a robust project history.