Back to Git & Dev Tools
2026-01-159 min read

Official Git Tutorial (Git & Dev Tools)

Learn Official Git Tutorial (Git & Dev Tools) step by step with clear examples and exercises.

Title: Mastering Git and Developer Tools: An Official Guide

Why This Matters

Git is an indispensable tool for developers, offering a powerful version control system that facilitates efficient collaboration, error reduction, and project management. This guide aims to provide you with a comprehensive understanding of Git, preparing you for real-world scenarios and common challenges.

Prerequisites

Before diving into Git, it is advisable to have a basic grasp of:

  1. Command Line Interface (CLI) - Familiarity with navigating directories, creating files, and executing commands.
  2. Basic programming concepts - Understanding variables, functions, and control structures will help you better understand the concept of Git.
  3. Familiarity with a text editor like Visual Studio Code or Sublime Text for editing code files.
  4. A basic understanding of operating systems (Windows, macOS, Linux) as Git commands may vary slightly between different platforms.

Core Concept

Installation

Install Git on your system using the following command:

curl -LO https://github.com/git-for-windows/git/releases/download/v2.33.1/Git-2.33.1-preview.2.exe

Replace 2.33.1 with the latest version available at git-scm.com.

Initializing a Repository

Navigate to your project directory and initialize Git:

git init

This creates a hidden .git folder in your project directory, which contains all the necessary files for version control.

Committing Changes

Make changes to your code, then stage (prepare for commit) and commit those changes:

git add .
git commit -m "Initial commit"

The . symbol represents all files in the current directory, and the -m flag allows you to provide a commit message.

Viewing Project History

Use the following command to view the commit history:

git log

This will display a list of all commits, along with their respective commit IDs, author, date, and commit message.

Branching and Merging

Create a new branch called feature-branch and switch to it:

git checkout -b feature-branch

Merge changes from another branch into the current one using:

git merge <branch-name>

Remote Repositories

Create a remote repository on GitHub, then push your local repository to that remote repository:

git remote add origin https://github.com/yourusername/your-repo.git
git push -u origin master

Pull changes from the remote repository into your local one:

git pull origin master

Tagging Releases

Tagging a release allows you to mark specific commits as important milestones in your project's development. To create a tag, use:

git tag -a v1.0.0 -m "Version 1.0.0 Release"

Push the tag to the remote repository:

git push origin v1.0.0

GitHub Actions

GitHub Actions is a powerful continuous integration and deployment tool that can automate various tasks, such as testing, building, and deploying your project. To create a workflow file, create a new file in the .github/workflows directory called main.yml.

Git Large File Storage (LFS)

Git LFS is a Git extension that allows you to manage large binary files like images or videos more efficiently. Install Git LFS using:

curl -s https://raw.githubusercontent.com/git-lfs/git-lfs/master/install_script.sh | sh

Initialize your repository for Git LFS:

git lfs install

Add large files to the Git LFS tracking list using:

git lfs track "*.jpg"
git lfs track "*.png"

SSH Key Setup

Using SSH keys for authentication provides a more secure method of accessing remote repositories compared to HTTPS. To generate an SSH key pair, use:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Add the generated public key to your GitHub account:

  1. Copy the contents of the ~/.ssh/id_rsa.pub file.
  2. Go to GitHub, navigate to Settings > SSH and GPG keys > New SSH key.
  3. Paste the copied content into the "Key" field, then click "Add SSH key".

Ignoring Files

Create a .gitignore file in your project directory to specify which files should be ignored by Git. Commonly ignored files include:

node_modules/
.env
.DS_Store

Worked Example

Let's create a simple project and demonstrate Git's functionality:

  1. Create a new directory for your project:
mkdir my-project
cd my-project
  1. Initialize the Git repository:
git init
  1. Create a main.c file with some code and make changes to it:
#include <stdio.h>

int main() {
printf("Hello, World!\n");
return 0;
}
  1. Stage and commit the changes:
git add main.c
git commit -m "Initial commit with Hello, World!"
  1. Make additional changes to main.c:
#include <stdio.h>

int main() {
printf("Welcome to Git!\n");
return 0;
}
  1. Stage and commit the updated file:
git add main.c
git commit -m "Updated the message"
  1. View the project history:
git log

Create a new branch called feature-branch, make changes to main.c, and compare it with the master branch:

  1. Switch to the master branch:
git checkout master
  1. Create and switch to the feature branch:
git checkout -b feature-branch
  1. Make changes to main.c:
#include <stdio.h>

int main() {
printf("This is a change on feature-branch!\n");
return 0;
}
  1. Stage and commit the changes:
git add main.c
git commit -m "Changed message on feature-branch"
  1. Compare the two branches using:
git diff master feature-branch

Common Mistakes

  1. Forgotten Git Ignore: Failing to include a .gitignore file can lead to unnecessary files being added to the repository, causing bloat and potential security issues.
  2. Mixed Content: Committing binary files (like images or videos) without proper preparation can cause problems when collaborating with others.
  3. Incorrect Merge Conflicts: Improper handling of merge conflicts can result in losing valuable work or introducing bugs into the project.
  4. Unintended Deletions: Accidentally deleting files and committing the changes can lead to data loss. To recover deleted files, use git reflog.
  5. Misconfigured Git: Incorrect configuration settings in the global Git configuration file (usually found at ~/.gitconfig) can cause issues when working with multiple projects or users.
  6. ### Ignored Changes: Sometimes, developers may forget to stage changes before committing, leading to important modifications being lost.
  7. ### Merging Conflicts in Automated Workflows: When using automated workflows like GitHub Actions, improper handling of merge conflicts can halt the entire build process.
  8. ### Large File Management: Improper management of large binary files can lead to issues with collaboration and repository size.
  9. ### SSH Key Issues: Misconfigured SSH keys or incorrect authentication settings can prevent access to remote repositories.
  10. ### Incorrect Git LFS Configuration: Failing to properly configure Git LFS can result in large binary files being tracked as regular files, causing issues with collaboration and repository size.

Practice Questions

  1. How would you create a new branch called feature-branch and switch to it?
  2. What command would you use to view the differences between two commits?
  3. If you accidentally commit changes that should not have been committed, how can you undo them?
  4. How would you recover deleted files from your Git repository?
  5. What is the purpose of a .gitignore file and why is it important to include one in your projects?
  6. What is Git LFS and what are some common files that should be managed with Git LFS?
  7. How can you set up SSH keys for improved security when accessing remote repositories?
  8. What is the purpose of GitHub Actions and how can it help automate tasks in your development workflow?
  9. When working on a large project, what are some potential issues that may arise with managing binary files and how can they be addressed using Git LFS?
  10. In case of merge conflicts, what steps should you take to resolve them effectively and avoid losing valuable work or introducing bugs into the project?

FAQ

--

  1. What is a Git repository?

A Git repository is a local directory on your computer containing all the files needed for version control, along with metadata like commit history and branch information.

  1. How do I collaborate with others using Git?

You can share your repository with others by creating a remote repository on a service like GitHub or Bitbucket, then pushing your local repository to that remote repository.

  1. What is the difference between git add and git commit?

git add stages changes for committing, while git commit actually creates a new commit with those staged changes.

  1. How do I revert a specific commit in my Git history?

To revert a specific commit, use the following command:

git revert <commit-hash>

Replace `` with the hash of the commit you want to revert.

  1. What is the purpose of a merge conflict and how can I resolve it?

A merge conflict occurs when changes made in different branches cannot be automatically merged due to overlapping changes in the same file(s). To resolve a merge conflict, manually edit the conflicting files and mark them as resolved using:

git add <conflicting-file>

Finally, commit the changes with a descriptive message.

  1. What is Git LFS and what are some common files that should be managed with Git LFS?

Git LFS is a Git extension that allows you to manage large binary files like images or videos more efficiently. Commonly ignored files include media files, databases, and other large files that can cause issues when collaborating with others.

  1. How can I set up SSH keys for improved security when accessing remote repositories?

To generate an SSH key pair, use:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Add the generated public key to your GitHub account:

  1. Copy the contents of the ~/.ssh/id_rsa.pub file.
  2. Go to GitHub, navigate to Settings > SSH and GPG keys > New SSH key.
  3. Paste the copied content into the "Key" field, then click "Add SSH key".
  4. What is the purpose of GitHub Actions and how can it help automate tasks in your development workflow?

GitHub Actions is a powerful continuous integration and deployment tool that can automate various tasks, such as testing, building, and deploying your project. To create a workflow file, create a new file in the .github/workflows directory called main.yml.

  1. When working on a large project, what are some potential issues that may arise with managing binary files and how can they be addressed using Git LFS?

Large binary files can cause issues with collaboration, repository size, and performance when stored in a Git repository. Git LFS addresses these issues by allowing you to manage large binary files more efficiently, reducing the size of your repository and improving performance.

  1. In case of merge conflicts, what steps should you take to resolve them effectively and avoid losing valuable work or introducing bugs into the project?

To resolve a merge conflict, manually edit the conflicting files and mark them as resolved using:

git add <conflicting-file>

Ensure that all changes are properly merged and commit the resolved file with a descriptive message. If necessary, consult with team members to discuss potential solutions or seek assistance from more experienced developers.

Official Git Tutorial (Git & Dev Tools) | Git & Dev Tools | XQA Learn