Git Tutorial
Git is an open-source distributed version control system that helps teams track and manage code changes, collaborate seamlessly, and work on projects of any size. It keeps a history of every change, allowing you to revisit or restore previous versions, and makes it easy to fix mistakes without losing progress.
Introduction to Git
Let's start with basics of Git here:
- Version Control System
- What is Git
- History of Git
- Git Features
- Git Repository
- Bare Repositories in Git
- Git Ignore
- Readme.md File
Before starting understanding Git practically, let us first install Git on our systems:
Working with Git
Working with Git means managing your project’s code using Git commands to track, save, and share changes:
- Git Environment Setup
- Using Git on CLI
- Working on Git Bash
- Git- Setting up a Repository
- Working with Git Repositories
- States of a File in Git Working Directory
Git Commands
In this section, we’ll learn essential Git commands with examples, covering add, commit, push, pull, merge, stash, rebase, clone, tags, branches, logs, debugging, error handling, and advanced operations like squashing, cherry-picking, and filtering history.
1. Getting Started
In this part, we’ll cover the fundamental commands to initialize a repository, add files, commit changes, check status, and save work in Git.
2. Remote Repositories
In this part, we’ll know commands for working with remote repositories, including adding remotes, cloning projects, pushing local commits, pulling updates, and setting upstream branches.
- Git Remote
- Git Origin Master
- Git Push
- Git Pull
- Git Pull and Fetch
- Set Upstream Branch on Git
- Clone a Repository
3. Branching & Merging
In this part, we’ll explore how to create and push branches to remote, delete them locally and remotely, switch between branches using checkout, and merge changes into the main codebase.
- Push Git BranchGit Branch to Remote
- Delete a Git Branch Locally and Remotely
- Git Checkout and Merge
- Git Merge
- Git Merge and Merge Conflicts
- Git Rebase
- Git Squash
- Git Fork
4. File & Change Management
In this part, we’ll see how to manage changes in Git — from comparing file differences with diff, cleaning up untracked files, renaming or moving files, and staging updates, to undoing commits, removing untracked files, and even adding empty directories to a repository.
- Git diff
- Git Clean
- Git Rename
- Git Move Files
- Git Undo Commit
- Git Undo
- Git Stage
- Remove Local Untracked Files
- Add an Empty Directory to a Git Repository
5. History & Debugging
In this part, we’ll learn how to review and debug a project’s history — using git log to view and format commits, exploring references with reflog, tracing changes with blame, and recovering lost commits
- Git Log
- Using Git Log to Format the Commit History
- Git Ref and Reflog
- Git Blame
- Recovering Lost Commits
- Git - git-show Command Line Utility
- Git – Working Tree
- Git Index
6. Advanced Commands
In this part, we’ll cover advanced Git features like aliases, submodules, subtrees, tags, hooks, patching, pruning, history rewriting, git add variations, debugging, and error handling.
- Git Alias
- Git Submodules
- Git Subtree
- Git Tags
- Git Hooks
- Patch Operation in Git
- Git Prune
- Git – Filtering the Commit History
- Git - Changing History
- Difference between “add -A”, “add -u”, “add .”, and “add *”
- Difference between “git add -A” and “git add”
- Debugging in a Git
- Error Searching and Handling in Git
7. Essential Git Commands
- Useful Git Commands and Basic Concepts
- Basic Git Commands with Examples
- Essential Git Commands
- Top 12 Most Used Git Commands For Developers
Git Advanced Usage
In this section, we’ll cover advanced Git concepts, including branching, exporting projects, handling errors, common issues, and integrating Git with development tools like RStudio and Eclipse.
1. Advanced Git Concepts
This section covers branching, exporting projects, commit management, Git objects, and handling large repositories.
- Introduction to Git Branch
- Export a Git Project
- Move the most recent commit(s) to a new branch with Git
- Git–Pack Objects
- Handle Big Repositories with Git
2. Error Handling & Troubleshooting
This section covers common Git errors, authentication issues, and solutions for repository lock problems.
- Common Git Problems and Their Fixes
- Git – How to Solve “remote: Invalid username or password. fatal: Authentication failed”
- Git – A lock file already exists in the repository, which blocks this operation from completing
3. Git Automation & Extensions
This section covers automating Git tasks, sending emails, and using Git in Google Colab.
- Send an Email using Git send-email via Gmail
- Automating some git commands with Python
- Install and Use Git in Google Colab
4. Alternative VCS & Comparisons
This section introduces alternative VCS tools like Bitbucket and Mercurial, and compares them with Git and SVN.
- Introduction to Bitbucket
- Working on Bitbucket using Git
- Introduction to Mercurial
- Difference between MERCURIAL and GIT
- Difference between Git and SVN
5. Git in CI/CD
This section covers Git integration in CI/CD pipelines, including Jenkins setup, GitLab workflows for different languages, and hosting private Git servers on Kubernetes.
- Jenkins and GIT Integration using SSH Key
- Add GIT Credentials in Jenkins
- CI/CD in .NET application Using Shell Executor on GitLab
- CI/CD in Java application(Linux) Using Shell and Docker Executor on GitLab
- CI/CD in C/C++ Application(Linux) Using Shell and Docker Executor on GitLab
- Installing Private Git Server on K8s Cluster with Gitea and AKS
Git Deployment
This section covers deploying static sites and Django applications to Heroku using Git.
- Deploy a Basic Static HTML Website to Heroku using Git
- Deploy a Django Application to Heroku with Git CLI
Git Collaborating
This section covers Git collaboration basics, forking workflows, pull requests, merge strategies, and conflict resolution.
- What is Git Collaboration
- Forking Workflow in Open Source Ecosystem
- Make your first Open Source Pull Request
- Merge Strategies in Git
- How to Handle Merge Conflicts
Git vs GitHub
The following table gives you a comparison between Git and Github:
| Git | GitHub |
|---|---|
| Git is a distributed version control system that tracks changes in the source code. | GitHub is a cloud-based Git repository hosting service that enhances collaboration and code management. |
| To manage versions of code and track changes locally. | To host Git repositories online and allow collaboration and version control via Git. |
| Git is a command-line tool installed locally on your machine. | GitHub is accessed via the web and requires no installation. |
| Maintained by the Linux community. | Maintained by Microsoft since 2018. |
| Git focuses on version control and managing code changes. | GitHub focuses on hosting, collaboration, and project management. |
| Git was released in 2005 by Linus Torvalds. | GitHub was launched in 2008 as a platform for Git repository hosting. |
| Git does not have built-in user management. | GitHub includes built-in user management, allowing for collaboration and permissions. |
| Git is open-source and free to use. | GitHub offers free-tier and paid plans with additional features. |
| Git has minimal external tool integration. | GitHub provides an active marketplace for integrations, including CI/CD, project management, and more. |
| Git provides the Git GUI for graphical management. | GitHub provides GitHub Desktop and web interfaces for managing repositories. |
| Git competes with CVS, Mercurial, and Subversion. | GitHub competes with GitLab, Bitbucket, and SourceForge. |
Common Git Problems and Their Fixes
Let's discuss some common git problems and how to fix it:
- Merge Conflicts: Always check for conflicting code before merging. Resolve conflicts manually and commit the resolved files.
- Detached HEAD State: When in a detached HEAD state, use
git checkout <branch_name>to switch back to your branch. - Commit Not Pushed: Ensure that you have pushed your commits to the remote repository using
git push.