Mastering Git and GitHub II

Mastering Git and GitHub II

Getting Started with Git

This is the second article in a 3-part series. In the first article, I introduced version control, highlighting its importance in software development. I also gave a brief overview of Git, the most popular version control software and compared it to its alternatives. Read it here. To get the best out of this guide, I strongly recommend you read the first part before proceeding. Now you understand all of that fundamental stuff, let's continue.

Part 2 - Getting Started with Git

Installing Git and Configuring Settings:

  • Visit the official Git website and download the appropriate version of Git for your operating system. Read the documentation to get all the information you need.

  • Run the installation package and follow the on-screen instructions to complete the installation.

  • Once Git is installed, you would need to configure some basic settings. Open the Git Bash terminal or command prompt and enter the following commands:

      $ git config --global user.name "Your Name"
      $ git config --global user.email "youremail@example.com"
    

    Replace "Your Name" with your desired name and "" with your email address. These settings will be associated with your Git commits, making it easier to maintain a clear and organized commit history and track who made specific changes and when they were made.

Creating a local Git Repository

A local Git repository is a directory inside your project folder where Git tracks your project development and stores the history of your project's files and their changes. It serves as a complete snapshot of your project at a specific point in time. When you initialize a local Git repository in a directory, Git creates a hidden .git folder that contains all the necessary files and metadata to manage version control.

The local repository stores a full copy of your project's files, including all branches, commits, and version history. It allows you to make changes to your project's files, track those changes, and easily switch between different versions or branches of your codebase. It also helps you push your project files to a remote repository on platforms like GitHub. So to create one:

  • Open your project folder in the terminal or command prompt.

  • Run the following command to initialize a new Git repository:

    $ git init

  • This command creates an empty Git repository in your project folder, enabling version control for your files.

Understanding Git's Basic Workflow

  • Git operates based on a simple workflow involving three main components: the working directory, the staging area (also known as the index), and the repository.

  • The working directory is where you make changes to your files. Git monitors these changes and tracks the differences.

  • The staging area acts as a buffer between the working directory and the repository. It allows you to select specific changes or files to be included in the next commit.

  • The repository is where Git stores the complete history of your project, including all commits and changes made over time.

In summary, every time you type in one of your code files, Git notes that you have made a change to that file. It keeps track of these changes in a staging area but doesn't save them to the repository until you commit them. Committing is simply the process of saving the modified files to the repository. Note, committing doesn't just override the previous file, instead a newer version of your software is created and a snapshot of the older version is stored.

Git Fundamentals

Repository: Initialization, Cloning, and File Tracking

The git init command creates a new repository in your project directory, setting up the necessary Git structure. Alternatively, if you want to work with an existing repository, you can clone it using the

git clone <repository_url>

command, which creates a local copy of the remote repository on your machine. The URL can be obtained from the repository hosting platform, such as GitHub, GitLab, or Bitbucket. Once you have a repository set up, Git tracks changes to files automatically, allowing you to monitor modifications, additions, and deletions.

Commits: Making Changes, Staging, and Committing

The heart of Git's version control system lies in its commits. To make changes, you can modify files in your working directory. Once you are satisfied with the modifications, you can stage the changes using:

git add <filename.ext>

git add * -to stage all modified files since the last commit.

Staging allows you to select specific changes to include in the next commit, ensuring that only relevant modifications are saved. Finally, the git commit command permanently saves the staged changes as a new commit in the repository's history. Each commit represents a specific set of changes along with a descriptive commit message.

git commit -m "commit message"

Committing would save all the files you staged with git add since your last commit. Files you made changes to but did not add to the staging area will not be automatically saved with the commit until you add them.

Resolving Conflicts and Handling Common Challenges

In collaborative development environments, conflicts may arise when multiple developers make conflicting changes to the same file or line of code. Git provides tools to help resolve these conflicts. The git diff command allows you to identify conflicting changes, while the git status command provides an overview of the conflicted files.

You can manually edit the conflicting files in your working directory to resolve the differences, and then use git add and git commit to finalize the resolution. Additionally, Git offers other features and commands to address common challenges, such as reverting changes, cherry-picking commits, and managing ignored files.

Find a list of many more Git commands and what they do as well as a glossary of Git terms in this cheat sheet.

Collaborative Development with GitHub

GitHub, a widely-used platform for version control and collaboration, enhances the Git experience by providing additional features and a centralized hosting solution. GitHub's extensive collaboration features empower developers to work together seamlessly, improve code quality, and accelerate the development process.

Introduction to GitHub: Features and Benefits

GitHub offers a wide range of features designed to streamline collaborative development. These include issue tracking, project boards, wikis, and more. Development teams can efficiently manage projects, track issues, conduct code reviews, and foster collaboration across distributed environments. GitHub also provides a web-based interface for managing repositories, making it easy to navigate, explore, and contribute to projects.

Creating a Remote Repository on GitHub

To begin collaborating on GitHub, you'll need to create a remote repository on the platform (different from your local Git repo). This involves providing a name, description, and optionally, a license for your project. Once created, your remote repository acts as the central hub for collaboration, allowing team members to contribute code and track changes.

Forking, Cloning, and Syncing Repositories

GitHub introduces the concept of forking, which enables you to create a personal copy of a repository. This is different from cloning as cloning downloads the project files to your PC. Forking only creates a copy of the project repo in your Github account. Forking is often used when you want to contribute to someone else's project. You can make changes in your forked repository and later sync those changes with the original repository through a pull request.

Branch Management and Pull Requests for Collaboration

Branches in GitHub allow for isolated development and experimentation. You can create new branches, work on specific features or bug fixes, and later merge those changes into the main branch through a pull request. Pull requests provide a platform for discussion, feedback, and code reviews, ensuring the quality of the changes before merging. Read more about branches.

Code Reviews and Merging Changes

Code reviews are an essential part of the collaborative development process. GitHub provides a robust code review workflow, allowing team members to review, comment, and suggest changes on pull requests. Once the changes are reviewed and approved, they can be merged into the main branch, incorporating the contributions into the project. Read more about pull requests and code reviews.

Part 3: Advanced Git Features

Remember to subscribe to get my free newsletters and the concluding part in your mailbox.