Git

Git is a distributed version control system widely used for tracking changes in source code during software development. It provides a reliable and efficient way to manage and collaborate on projects, allowing multiple developers to work on the same codebase simultaneously. With Git, developers can create branches, make changes, merge code, and track the history of commits. It offers features like branching and merging, conflict resolution, tagging, and remote repository management.

Module 1: Git Basics

Introduction to Git

Git is a distributed version control system, created by Linus Torvalds in 2005. It allows multiple developers to work on a project without overwriting each other's changes. The 'distributed' part means that everyone who contributes to a project gets a complete copy of the project history on their local machine. This makes it possible to work offline, and also means that there is no single point of failure.

Installing and Setting Up Git

To start using Git, you need to install it on your machine. This is a straightforward process and differs slightly depending on your operating system. For Windows, you can download the official Git client from the Git website. For Linux, you can usually install Git from your distribution's package manager. After installing Git, you should configure it. This involves setting your name and email address, which Git will use to mark your commits.

Creating a New Repository

Once you have Git installed and configured, you can start using it. The first step is to create a new repository. A repository, or 'repo', is a directory where Git has been initialized to start version tracking. To create a new repository, navigate to the directory where you want the repo to live, and run 'git init'. This command creates a new subdirectory named .git that contains all the necessary Git metadata for the new repository.

Making Your First Commit

With a Git repository initialized, you can start tracking versions of your project. First, create a new file in your repository. Then, add this file to the staging area with 'git add'. The staging area is where Git keeps track of changes that you want to include in your next commit. After staging your changes, you can commit them to the project history with 'git commit -m 'your message here''. It's important to write clear, descriptive commit messages so you and others can understand what changes each commit introduces.

Module 2: Git Branching and Merging

What is a Branch?

In Git, a branch is essentially a unique set of code changes with a unique name. The default branch name in Git is 'master'. It's best practice to create new branches for each new feature or bug fix you're working on, and not to mix up code from different features or fixes in the same branch. This keeps your changes organized and separated from the master branch, allowing you to submit and review changes in a clean, isolated manner.

Creating a New Branch

To create a new branch in Git, you use the command 'git branch your-branch-name'. This will create a new branch that's a copy of the branch you're currently on. To switch to this new branch, you use the command 'git checkout your-branch-name'. Now you're on your new branch and ready to start making changes!

Merging Changes

Once you've finished making changes on a branch, you can merge those changes back into the 'master' branch, or any other branch. To do this, you use the 'git merge' command. This command takes the contents of the branch you're merging from and integrates them with the branch you're currently on.

Resolving Merge Conflicts

Sometimes, when you try to merge two branches, Git won't be able to figure out how to integrate the changes. This is called a merge conflict. When a conflict happens, Git pauses the merge and asks you to resolve the conflict before continuing. To resolve a merge conflict, you need to edit the files that have conflicts, choose which changes to keep, and then add the resolved files to the staging area with 'git add'. Once you've staged all the resolved files, you can continue the merge with 'git commit'.

Module 3: Working with Remote Repositories

What is a Remote Repository?

A remote repository in Git is a version of your project that is hosted on the internet or network somewhere. Remote repositories make it possible for you to collaborate with others on a project. You can have several of them, each of which generally is either read-only or read/write for you.

Cloning a Repository

If you want to get a copy of an existing Git repository, you do so by 'cloning' it. Cloning a repository pulls down a full copy of all the repository data that GitHub has at that point in time, including all versions of every file and folder for the project. You can clone a repository with 'git clone url-of-the-repository'.

Pushing Changes

Once you've made your changes in your local repository and you're happy with them, you can upload those changes to the remote repository. This is known as 'pushing'. To push your changes, use the command 'git push origin your-branch-name', where 'origin' is the alias of your remote repository, and 'your-branch-name' is the name of your local branch that you want to push.

Pulling Changes

If someone else has made changes to the remote repository, you can 'pull' those changes into your local repository with 'git pull'. This command fetches the changes from the remote repository and merges them into your current branch.

Module 4: Advanced Git Topics

Rebasing

'Rebase' is another way to integrate changes from one branch into another. Rebase compresses all the changes into a single 'patch'. Then it integrates the patch onto the target branch. Unlike merging, rebasing discards the identity of the commit on the current branch and creates new ones. This can make for a much cleaner project history. To use it, you would use 'git rebase' instead of 'git merge'.

Interactive Rebase and Squashing Commits

Git also provides an 'interactive' mode for rebasing, which gives you more control over the process. One of the things you can do with interactive rebase is squashing commits. Squashing is the act of compressing many commits into a single one. This is useful when you have a bunch of 'work in progress' commits that you want to combine into a single commit. You can use the command 'git rebase -i' to start an interactive rebase.

Reverting and Resetting

Sometimes, you might make changes that you want to undo. Git provides a couple of ways to undo changes: 'git revert' and 'git reset'. The 'git revert' command can undo any commit. It does this by generating a new commit that undoes all the changes made in the specified commit. On the other hand, 'git reset' moves the HEAD and, optionally, the staging index back to a specified commit. This can be used to discard commits in a private branch or to discard uncommitted changes.

Working with Git Remotes

Working with remotes gives you the ability to collaborate with others on a project. You can add a remote repository using the command 'git remote add'. You can then push your changes to the remote repository with 'git push'. To get updates from the remote repository, you use 'git pull'. You can also fetch the changes without merging them into your current branch with 'git fetch'.