Introduction to GitHub

An overview of GitHub as a platform for collaboration, code hosting, and issue tracking. Learn about repositories, forks, and pull requests.


Learn Git and GitHub

Making Changes: Committing and Branching

Git is a powerful version control system that allows you to track changes to your code, collaborate with others, and easily revert to previous versions. Central to Git's functionality are the concepts of committing and branching.

Committing Changes

A commit is a snapshot of your repository at a specific point in time. It represents a set of changes you've made to your files and are ready to save. Think of it as taking a photograph of your project at a particular stage of development. These snapshots are stored in a chronological order, creating a history of your project.

Branching

A branch is a pointer to a specific commit in your repository's history. Think of it as creating a parallel timeline or an alternative version of your project. Branching allows you to work on new features, bug fixes, or experimental changes without affecting the main codebase (usually the main or master branch). When you're happy with the changes on your branch, you can merge it back into the main branch.

Diving into the Essentials of Git Workflow

Staging Changes

Before you commit your changes, you need to stage them. Staging is the process of telling Git which files or modifications you want to include in the next commit. You can stage individual files, groups of files, or all modified files.

The command to stage changes is git add. For example, to stage a specific file:

git add my_file.txt

To stage all modified files:

git add .

Committing with Meaningful Messages

Once you've staged your changes, you can commit them with the git commit command. It's crucial to write clear and concise commit messages that explain the purpose of the changes. A good commit message should be a brief summary (less than 50 characters) followed by a more detailed explanation, if necessary.

git commit -m "Fix: Resolved issue with user authentication"

A longer, more descriptive commit message:

git commit -m "Fix: Resolved issue with user authentication\n\nThis commit fixes a bug that prevented users from logging in correctly.  The issue was caused by an incorrect validation check on the password field.  The validation has been updated to correctly check the password against the database."

Creating Branches for Parallel Development

Branching is a cornerstone of Git's collaborative power. It allows multiple developers to work on different features or bug fixes simultaneously without interfering with each other's work.

To create a new branch, use the git branch command:

git branch feature/new-login

This creates a new branch named feature/new-login. To switch to this branch, use the git checkout command:

git checkout feature/new-login

Alternatively, you can create and switch to a branch in one step using git checkout -b:

git checkout -b feature/new-login

Now, you're working on the feature/new-login branch. You can make changes, stage them, and commit them. These changes will only affect this branch, leaving the main (or master) branch untouched.

How Branching Isolates New Features and Bug Fixes

By creating branches, you isolate your changes from the main codebase. This allows you to experiment with new ideas, fix bugs, or develop new features without the risk of breaking the main application. Once the changes on your branch are tested and verified, you can merge them back into the main branch.

For example, if you're working on a new feature that might introduce bugs, you can create a separate branch for that feature. If the feature turns out to be problematic, you can simply discard the branch without affecting the main codebase. If the feature works well, you can merge it back into the main branch with confidence.