GIT Essentials

This tutorial covers the definition of Git, its basic commands and concepts to help you get started with version control. As you become more familiar with Git, you can explore more advanced features and workflows.

For detailed documentation and specific Git commands, you can use the git --help command or refer to the official Git documentation: Git Documentation.

What is Git?

Git is a distributed version control system that is widely used in software development to manage and track changes in code and project files. It was created by Linus Torvalds in 2005 and is known for its flexibility, speed, and robust branching and merging capabilities. Git is designed to help multiple collaborators work on the same project, enabling them to make changes independently and then integrate those changes seamlessly.

Git stores the entire project history as a series of snapshots or commits. Each commit represents a specific state of the project at a particular point in time. Developers can work on different branches, allowing for parallel development of features and bug fixes. Once work on a branch is complete, changes can be merged into the main project branch.

Using Git

Let's walk through a simple example to illustrate how Git works in a real-world scenario. We'll create a Git repository, make changes, and commit those changes.

Step 1: Initialize a Git Repository

  1. Create a new directory for your project and navigate to it in your terminal.

  2. Initialize a Git repository in the directory:

    git init

Step 2: Create and Edit Files

  1. Create a new file (e.g., about.html) in your project directory:

    touch about.html
  2. Edit the file using a text editor or code editor to add some content. Save the file. (optional)

Step 3: Stage and Commit Changes

  1. Check the status of your Git repository to see the untracked changes:

    git status
  2. Stage the changes for commit:

    git add about.html
  3. Commit the changes with a meaningful message:

    git commit -m "Added about.html with initial content"

Step 4: Viewing Commit History

You can view the commit history of your project:

git log

The git log command displays a list of commits, including commit messages, author information, and commit timestamps.

Step 5: Create a New Branch

Suppose you want to work on a new feature. Create a new branch for this feature:

git checkout -b feature-branch

Step 6: Make Changes and Commit

Edit the about.html file to make changes. Save the file and commit the changes to the feature branch:

git add about.html 
git commit -m "Added a new feature"

Step 7: Merge Branches

After completing the feature, you can merge the feature branch into the main branch (usually named "main" or "master"):

git checkout main 
git merge feature-branch

Step 8: Push to a Remote Repository

To collaborate with others, you can push your repository to a remote Git hosting service like GitHub:

  1. Create a repository on GitHub.

  2. Add the remote repository to your local Git repository:

    git remote add origin <remote-repo-url>
  3. Push your local repository to the remote:

    git push -u origin main

Set default upstream branch for a local branch if you cannot push your changes:

git push --set-upstream <reponame> main

This example demonstrates how Git can be used for tracking changes, branching, committing, and collaborating with others. Git's flexibility and powerful version control capabilities make it an essential tool for software development and collaborative projects.

Last updated