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
- 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.
- Commit: A snapshot of your repository at a specific point in time.
- Branch: A parallel version of your repository. It allows you to work on different features or bug fixes independently.
- Merge: The process of combining the changes from different branches.
- Clone: A copy of a repository that lives on your computer instead of a website's server.
- Pull: Fetching changes from a remote repository and merging them into your local repository.
- Push: Sending your committed changes to a remote repository.
Setting Up Git
Installing Git
- Windows: Download the Git installer from git-scm.com and follow the installation instructions.
- Mac: Use Homebrew to install Git by running
brew install git
in the terminal. - 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:
Cloning a Repository
To clone an existing repository:
Checking Repository Status
To see the status of your files in the working directory and staging area:
Adding Changes
To add changes to the staging area:
Committing Changes
To commit changes in the staging area to the repository:
Viewing Commit History
To view the commit history:
Branching and Merging
Creating a Branch
To create a new branch:
Switching Branches
To switch to a different branch:
Merging Branches
To merge a branch into the current branch:
Pushing and Pulling
Pushing Changes
To push your changes to a remote repository:
Pulling Changes
To pull changes from a remote repository:
Practical Exercise
Exercise: Basic Git Workflow
- Initialize a Git repository in a new directory.
- Create a new file named
index.html
and add some HTML content. - Add the file to the staging area.
- Commit the file to the repository with a meaningful message.
- Create a new branch named
feature
. - Switch to the new branch.
- Modify the
index.html
file by adding a new paragraph. - Add and commit the changes.
- Switch back to the main branch.
- Merge the
feature
branch into the main branch. - 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.
Angular 2+ Course
Module 1: Introduction to Angular
Module 2: TypeScript Basics
- Introduction to TypeScript
- TypeScript Variables and Data Types
- Functions and Arrow Functions
- Classes and Interfaces