Introduction

Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later. Git is a distributed version control system that allows multiple developers to work on a project simultaneously without overwriting each other's changes.

Key Concepts

  1. Repository (Repo): A directory which contains your project work, as well as a few files (hidden by default) which are used to communicate with Git.
  2. Commit: A snapshot of your repository at a specific point in time.
  3. Branch: A parallel version of your repository. It allows you to work on different features or bug fixes independently.
  4. Merge: The process of combining the changes from different branches.
  5. Clone: A copy of a repository that lives on your computer instead of a website's server.
  6. Pull: Fetching changes from a remote repository and merging them into your local repository.
  7. Push: Sending your committed changes to a remote repository.

Setting Up Git

Installing Git

  1. Windows: Download the Git installer from git-scm.com and follow the installation instructions.
  2. Mac: Use Homebrew to install Git by running brew install git in the terminal.
  3. Linux: Use the package manager for your distribution, e.g., sudo apt-get install git for Debian-based systems.

Configuring Git

After installing Git, you need to set up your username and email address. This information will be associated with your commits.

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

Basic Git Commands

Initializing a Repository

To start a new repository, navigate to your project directory and run:

git init

Cloning a Repository

To clone an existing repository:

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

Checking Repository Status

To see the status of your files in the working directory and staging area:

git status

Adding Changes

To add changes to the staging area:

git add <file>
# or to add all changes
git add .

Committing Changes

To commit changes in the staging area to the repository:

git commit -m "Your commit message"

Viewing Commit History

To view the commit history:

git log

Branching and Merging

Creating a Branch

To create a new branch:

git branch <branch-name>

Switching Branches

To switch to a different branch:

git checkout <branch-name>

Merging Branches

To merge a branch into the current branch:

git merge <branch-name>

Pushing and Pulling

Pushing Changes

To push your changes to a remote repository:

git push origin <branch-name>

Pulling Changes

To pull changes from a remote repository:

git pull origin <branch-name>

Practical Exercise

Exercise: Basic Git Workflow

  1. Initialize a Git repository in a new directory.
  2. Create a new file named index.html and add some HTML content.
  3. Add the file to the staging area.
  4. Commit the file to the repository with a meaningful message.
  5. Create a new branch named feature.
  6. Switch to the new branch.
  7. Modify the index.html file by adding a new paragraph.
  8. Add and commit the changes.
  9. Switch back to the main branch.
  10. Merge the feature branch into the main branch.
  11. Push the changes to a remote repository (if available).

Solution

# Step 1: Initialize a Git repository
git init

# Step 2: Create a new file
echo "<!DOCTYPE html><html><head><title>My First Page</title></head><body><h1>Hello, World!</h1></body></html>" > index.html

# Step 3: Add the file to the staging area
git add index.html

# Step 4: Commit the file to the repository
git commit -m "Initial commit with index.html"

# Step 5: Create a new branch
git branch feature

# Step 6: Switch to the new branch
git checkout feature

# Step 7: Modify the index.html file
echo "<p>This is a new paragraph added in the feature branch.</p>" >> index.html

# Step 8: Add and commit the changes
git add index.html
git commit -m "Added a new paragraph in the feature branch"

# Step 9: Switch back to the main branch
git checkout main

# Step 10: Merge the feature branch into the main branch
git merge feature

# Step 11: Push the changes to a remote repository (if available)
git push origin main

Common Mistakes and Tips

  • Forgetting to stage changes: Always remember to use git add before committing.
  • Commit messages: Use clear and descriptive commit messages to make it easier to understand the history of changes.
  • Branch management: Regularly merge branches to avoid large merge conflicts.
  • Pull before push: Always pull the latest changes from the remote repository before pushing your changes to avoid conflicts.

Conclusion

In this section, you learned the basics of Git, including how to set up a repository, track changes, and collaborate with others. Understanding these concepts is crucial for managing your Angular projects efficiently. In the next section, we will cover best practices for building and deploying Angular applications.

© Copyright 2024. All rights reserved