**Version Control with Git and Collaborative Development

This lesson dives into the world of Git, the industry-standard version control system, allowing you to track changes in your code, collaborate effectively, and manage projects efficiently. You'll learn the core concepts of Git, including creating repositories, committing changes, branching, merging, and handling conflicts, paving the way for collaborative development.

Learning Objectives

  • Create and initialize a Git repository.
  • Understand and apply the basic Git commands: `add`, `commit`, `push`, `pull`, and `checkout`.
  • Create, merge, and manage branches for feature development and bug fixes.
  • Resolve merge conflicts effectively.

Text-to-Speech

Listen to the lesson content

Lesson Content

Introduction to Version Control and Git

Version control is essential for software development. It allows you to track changes to your code over time, revert to previous versions, and collaborate seamlessly with others. Git is a distributed version control system, meaning that each developer has a complete copy of the repository, including the entire history. This makes it incredibly robust and flexible. Think of Git as a time machine for your code! Key benefits include:

  • Tracking Changes: See who made what changes and when.
  • Reverting to Previous Versions: Easily go back to a working state if something breaks.
  • Collaboration: Work with multiple developers on the same project without stepping on each other's toes.
  • Backups: Your code's history is distributed, offering built-in backups.

Before you begin, make sure you have Git installed on your system. You can usually download it from your operating system's package manager or from the official Git website (git-scm.com).

Initializing a Repository and Making Commits

Let's start by creating a Git repository. Navigate to your project directory in your terminal and type git init. This command initializes a new Git repository in that directory. A hidden .git folder is created, containing all the necessary metadata for Git to track your changes.

To track files, use git add <filename> to stage them. Staging essentially tells Git, "Hey, I want to track these changes." For all files in a directory, you can use git add .

Once you've staged the changes, commit them with git commit -m "Descriptive commit message". The commit message is crucial; it explains what changes you've made. Aim for clear, concise, and informative messages. Examples:

  • git init: Initializes an empty Git repository.
  • git add .: Stages all changes in the current directory.
  • git commit -m "Added feature X": Commits staged changes with a message describing the work.

Branches and Merging

Branches allow you to work on new features or bug fixes in isolation from the main codebase (usually the main or master branch). To create a new branch, use git branch <branch-name>. For example, git branch feature/new-login. To switch to a different branch, use git checkout <branch-name>. For example, git checkout feature/new-login.

You can combine these with git checkout -b <branch-name> which creates AND switches to a new branch in one step.

Once your feature is complete (and tested!), you'll merge your branch back into the main branch. Switch back to the main branch (git checkout main) and then use git merge <branch-name>. Example:

  1. git checkout -b feature/new-login: Creates and switches to a new branch.
  2. # Make changes to add the new login feature.
  3. git add . && git commit -m "Implemented new login feature"
  4. git checkout main: Switches back to the main branch.
  5. git merge feature/new-login: Merges the login feature branch into main.

Pushing and Pulling from Remote Repositories

Git repositories can be stored locally on your machine and remotely, on platforms like GitHub, GitLab, or Bitbucket. Remote repositories allow for collaboration and act as a backup. To link your local repository to a remote one (e.g., on GitHub), use git remote add origin <remote-repository-url>. You usually get the URL from the remote repository site.

To send your local commits to the remote repository, use git push -u origin main (or the name of your default branch). The -u flag sets up tracking, so you can just use git push in the future. To get the latest changes from the remote repository, use git pull origin main.

  • git remote add origin https://github.com/your-username/your-repository.git: Links local repo to remote.
  • git push -u origin main: Uploads local commits to the main branch.
  • git pull origin main: Downloads remote changes to your local machine.

Resolving Conflicts

Conflicts arise when Git can't automatically merge changes because two branches have modified the same lines in the same file. Git will mark the conflicting sections in the file. You'll need to manually edit the file, choose which changes to keep, and then remove the conflict markers (e.g., <<<<<<<, =======, >>>>>>>). Then, git add and git commit to finalize the merge.

Example conflict situation (in a file):

<<<<<<< HEAD
print("Hello from the main branch")
========
print("Hello from the feature branch")
>>>>>>> feature/add-greeting

You would edit this to keep one version or combine them (e.g., print("Hello from main and feature")), then stage and commit.

  • Conflict resolution requires careful attention to the intended changes and understanding the conflicting branches.
Progress
0%