USD ($)
$
United States Dollar
Euro Member Countries
India Rupee
د.إ
United Arab Emirates dirham
ر.س
Saudi Arabia Riyal

Git Basics

Lesson 6/17 | Study Time: 30 Min

Version Control with Git & GitHub


Version control is a critical skill in software development that allows developers to track changes in code, collaborate with teams, and manage multiple versions of a project efficiently. Git is a distributed version control system that is widely used for tracking code history, enabling collaboration, and ensuring project integrity. GitHub is a cloud-based platform that hosts Git repositories and provides tools for collaboration, issue tracking, and code review. Understanding Git and GitHub is essential for modern software development, DevOps workflows, and collaborative projects.




Git Basics


Git operates on a repository-based structure, where all project files, history, and changes are tracked. A Git repository can be local, stored on a developer’s machine, or remote, hosted on platforms like GitHub. The repository maintains a detailed history of all changes, allowing developers to revert to previous versions, compare modifications, and manage multiple development paths simultaneously.


Repository, Staging, and Commit


A repository is the core storage unit of Git where the project files and their change history are kept. A repository includes all source code, configuration files, and a .git directory, which stores metadata and version history. Developers work within the repository to add, modify, and track files.

The staging area is an intermediate layer between the working directory and the repository. When changes are made to files, they are first added to the staging area using the git add command. This allows developers to selectively prepare changes before committing them to the repository, ensuring that only the intended modifications are recorded.

A commit represents a snapshot of the project at a specific point in time. Each commit is accompanied by a unique identifier (hash) and a descriptive message explaining the changes. Committing ensures that modifications are permanently recorded in the repository’s history. Multiple commits create a detailed log of the project’s evolution, enabling traceability, debugging, and collaborative workflows.


Branching & Merging


Branching in Git allows developers to create separate lines of development within the same repository. Each branch is an independent workspace where features, bug fixes, or experiments can be developed without affecting the main project. For example, the main branch typically contains production-ready code, while feature branches are used for ongoing development. Branching supports parallel development, reduces conflicts, and improves team productivity.

Merging is the process of integrating changes from one branch into another. After completing a feature or fix on a separate branch, developers merge it into the main branch to incorporate the updates. Git intelligently handles merging by comparing the changes in each branch and combining them, while highlighting conflicts that need manual resolution. Merging ensures that collaborative development is seamless and that different workflows converge into a stable project.


Git Workflow Models


Git supports several workflow models that define how teams collaborate and manage changes in a project. These workflows provide structure to development and ensure smooth integration of changes:

Centralized Workflow mimics traditional version control systems. A single main repository acts as the central source of truth. Developers clone the repository, make changes locally, and push updates back to the central repository. This workflow is simple and suitable for small teams or projects.

Feature Branch Workflow encourages creating branches for every new feature or fix. Developers work independently on their branches and merge them into the main branch once complete. This workflow improves organization, reduces conflicts, and allows simultaneous development of multiple features.

Gitflow Workflow is a more structured model used in larger projects. It defines dedicated branches for features, releases, hotfixes, and the main production branch. Gitflow provides a clear strategy for managing version releases, bug fixes, and parallel development streams.

Forking Workflow is commonly used in open-source projects. Developers fork a central repository to create a personal copy, make changes, and submit pull requests to the original repository for review. This workflow ensures code quality and enables large-scale collaboration across multiple contributors.


Git and GitHub together provide powerful tools for version control, collaboration, and project management. Understanding repositories, staging, commits, branching, merging, and workflow models is essential for any developer or DevOps engineer, enabling structured development, traceability, and efficient team collaboration.3.2 Git Commands and Their Usage


Git Commands and Their Usage


Git provides a wide range of commands to manage repositories, track changes, and collaborate effectively. Understanding these commands for proper version control and efficient workflow management.


Git init


The git init command is used to initialize a new Git repository in a project directory. This command creates a hidden .git folder, which stores all metadata, configuration, and version history of the project. After initializing a repository, files and directories can be added, staged, committed, and tracked. This command marks the start of version control for a project, allowing developers to manage changes systematically.


Git clone


The git clone command is used to create a copy of an existing remote repository to the local machine. Cloning downloads all files, commits, branches, and history from the remote repository, making it ready for local development. This is essential for collaboration, as multiple developers can work on the same project independently while maintaining a connection to the central repository.


Git add


The git add command is used to add files to the staging area before committing. This command tells Git which changes should be included in the next commit. It allows developers to selectively stage changes, giving control over which modifications are recorded in the repository. For example, a developer can stage only specific files while leaving others for later.


Git commit


The git commit command is used to save changes from the staging area to the repository. Each commit creates a snapshot of the project, preserving the state of files at that point in time. Commits are accompanied by descriptive messages that explain the changes made. These messages provide a clear history of the project and are crucial for collaboration, debugging, and code review.


Git status


The git status command is used to check the current state of the working directory and staging area. It shows which files are staged, modified, or untracked. This command helps developers keep track of their changes, identify uncommitted files, and ensure that nothing important is left out before a commit.


Git log


The git log command displays the commit history of the repository, showing each commit’s unique identifier (hash), author, date, and message. This command is useful for tracking changes over time, reviewing project evolution, and finding previous versions or fixes. It provides a chronological view of development and helps understand the context of changes.


Git branch


The git branch command is used to create, list, or delete branches in a repository. Branches allow developers to work on different features or fixes independently from the main branch. By using branches, multiple parallel workflows can occur without disrupting the stable code in the main branch. This command is central to managing development in feature-driven workflows.


Git checkout


The git checkout command is used to switch between branches or restore files to a previous state. Developers use it to navigate between different lines of development or revert specific files to an earlier version. In combination with branches, git checkout enables safe experimentation without affecting the main project.


Git merge


The git merge command is used to integrate changes from one branch into another. After completing work on a feature branch, it can be merged into the main branch to incorporate the new functionality. Git intelligently combines changes from both branches, highlighting conflicts that require manual resolution. Merging ensures that collaborative development is coordinated and integrated seamlessly.


Git pull


The git pull command is used to fetch changes from a remote repository and merge them into the local repository. This command keeps the local copy updated with the latest changes from other developers. It is a combination of git fetch (download changes) and git merge (integrate them), making it a convenient way to synchronize repositories.


Git push


The git push command is used to upload local commits to a remote repository. By pushing changes, developers share their work with others and update the central repository. This command is crucial for collaboration, ensuring that all team members have access to the latest project version. Pushing is typically done after committing local changes and resolving any conflicts.


Git remote


The git remote command is used to view and manage remote repositories linked to the local repository. Remote repositories are hosted on platforms like GitHub, GitLab, or Bitbucket. Using git remote add, developers can link a new remote repository, while git remote -v shows existing connections and URLs. This enables seamless collaboration and integration with cloud-hosted repositories.


Git reset


The git reset command is used to undo changes in the staging area or working directory. It allows developers to unstage files (git reset <file>) or move the HEAD pointer to a previous commit (git reset --hard <commit>), effectively reverting the repository to an earlier state. This command provides flexibility in correcting mistakes before they are permanently committed.


Git revert


The git revert command is used to create a new commit that undoes changes made by a previous commit. Unlike git reset, which rewrites history, git revert safely preserves the project history while reversing specific changes. This is particularly useful in collaborative projects to undo errors without affecting other developers’ work.


Git stash


The git stash command allows developers to temporarily save uncommitted changes and clean the working directory. Stashed changes can be reapplied later using git stash apply or git stash pop. This command is useful when switching branches without committing incomplete work, maintaining workflow continuity.


These Git commands form the foundation of version control workflows. Mastering them allows developers to track changes, collaborate efficiently, manage multiple branches, and maintain a clean project history. Together with GitHub, these commands enable teams to work on the same project simultaneously, review code, and deploy updates safely.


Advanced and Important Git Commands


Git has a wide range of commands beyond the basics, which are essential for collaborative workflows, branch management, and history tracking. These commands help developers handle complex projects, resolve conflicts, and maintain clean version histories.


Git fetch


The git fetch command is used to download updates from a remote repository without automatically merging them into the local branch. Unlike git pull, git fetch allows developers to review incoming changes before integrating them. This is particularly useful when working in large teams, as it ensures that updates can be inspected and conflicts identified before merging.


Git diff


The git diff command is used to compare changes between files, commits, or branches. It shows line-by-line differences, highlighting additions, deletions, and modifications. Developers use this command to inspect changes before committing or merging, making it easier to review code and prevent unintended modifications from being introduced into the repository.


Git tag


The git tag command allows developers to mark specific points in the repository’s history as important, such as release versions or milestones. Tags can be lightweight (simple pointers to commits) or annotated (with metadata, message, and author). Using tags helps track versions, making it easier to roll back, deploy, or reference stable points in the project.


Git show


The git show command displays detailed information about a specific commit, tag, or object. It shows the changes made, author, date, and commit message. This command is useful for reviewing individual commits in detail, understanding project history, and debugging code changes.


Git cherry-pick


The git cherry-pick command allows developers to apply a specific commit from one branch to another without merging the entire branch. This is useful for selectively applying bug fixes or features from a different development branch into the main branch, maintaining precise control over changes included in the project.


Git rebase


The git rebase command is used to reapply commits from one branch onto another, effectively rewriting the commit history. Rebase is often used to maintain a clean, linear history and to integrate changes from the main branch into a feature branch before merging. While powerful, rebase must be used carefully in collaborative projects to avoid conflicts and overwriting others’ work.

Sales Campaign

Sales Campaign

We have a sales campaign on our promoted courses and products. You can purchase 1 products at a discounted price up to 15% discount.