DEV Community

Cover image for Mastering Git for DevOps: A Comprehensive Guide
Pratik Nalawade
Pratik Nalawade

Posted on

Mastering Git for DevOps: A Comprehensive Guide

Welcome to our tech blog focused on mastering Git for DevOps interviews. In this guide, I’ll cover everything from the basics of Git to advanced topics, ensuring you’re well-prepared to ace any Git-related questions during your DevOps interview.

Understanding Version Control Systems

Difference between CVCS and DVCS

CVCS (Centralized Version Control System): In CVCS, there’s a central server that stores all versions of a project’s files. Users check out files from this central repository to work on them and then check them back in when done.
DVCS (Distributed Version Control System): Unlike CVCS, DVCS does not necessarily rely on a central server. Each user has a complete copy (clone) of the repository, including its full history. This allows for offline work and more flexibility in collaboration.
Importance of Git
Git has become the de facto standard for version control in modern software development due to its numerous advantages:

Distributed nature: Allows for flexible and decentralized workflows.
Branching and merging: Facilitates parallel development and easy integration of features.
Speed: Git is incredibly fast, making it ideal for both small and large projects.
Data integrity: Uses cryptographic hashing to ensure the integrity of your data.
Large community and ecosystem: There’s extensive documentation, support, and a plethora of third-party tools and integrations available.

Git Three-stage Architecture

Git has three main stages:

1.Working Directory: The directory where you do all your work.
2.Staging Area (Index): Acts as a buffer between the working directory and the repository. Files in the staging area are ready to be committed.
3.Repository (HEAD): The repository stores all committed changes and their history.
Understanding Key Git Concepts
Repository, Commit, Tags, Snapshots
Repository: A Git repository is a directory that contains all the files and folders of a project, along with metadata and version history.
Commit: A commit represents a snapshot of changes to the repository at a particular point in time.
Tags: Tags are pointers to specific commits, often used to mark release versions.
Snapshots: Git uses snapshots to store changes to files over time, rather than storing the changes themselves.
Push-Pull Mechanism and Branching Strategy
Push-Pull Mechanism: Git uses the push and pull commands to synchronize changes between your local repository and a remote repository.
push => code changes uploaded into remote repository
pull => code changes downloaded into local repository

Branching Strategy: Git’s branching model allows for parallel development by creating separate branches for features, bug fixes, etc. Common strategies include GitFlow and GitHub Flow.

Advanced Git Operations

Working with Git Stash and Git Pop
Git Stash: Stashing allows you to temporarily shelve changes in your working directory, allowing you to switch to a different branch or work on a different task.
Git Pop: Pop applies the most recently stashed changes back to your working directory.
Use Case: Suppose you’re working on a feature branch and need to quickly switch to another branch to address an urgent bug. However, your changes are not ready for commit. Here, Git stash comes in handy to temporarily store your changes.

example:


# Stash changes
git stash

# Switch to another branch
git checkout bug-fix

# Make necessary changes and commit

# Switch back to the feature branch
git checkout feature-branch

# Apply stashed changes
git stash pop

Enter fullscreen mode Exit fullscreen mode

Resolving Merge Conflicts in Git
Merge conflicts occur when Git is unable to automatically merge changes from different branches. To resolve conflicts, you need to manually edit the conflicting files to incorporate the desired changes.

Use Case: When you merge branches and Git encounters conflicting changes, it pauses the merge process and prompts you to resolve the conflicts manually.

example:

# Switch to the branch you want to merge into
git checkout main
Enter fullscreen mode Exit fullscreen mode

Merge the feature branch into main

git merge feature-branch
When conflicts occur, Git will display the conflicted files. Open the conflicted files in your code editor and resolve the conflicts. After resolving conflicts

# Add the resolved files
git add 
Enter fullscreen mode Exit fullscreen mode

.

Commit the merge

git commit -m "Merge feature-branch into main, resolved conflicts"
Git Revert and Reset (Reset vs Revert)
Git Revert: Revert undoes a previous commit by creating a new commit that undoes the changes introduced by the original commit.
Git Reset: Reset moves the HEAD and current branch pointer to a specified commit, optionally modifying the working directory and staging area.
Use Case:

Git Revert: Let’s say you’ve pushed a commit containing a bug, and you want to undo the changes introduced by that commit without altering the commit history. Revert creates a new commit that undoes the changes.
Git Reset: Suppose you’ve made local commits that you want to discard entirely, resetting the branch to a previous state. Reset moves the HEAD and branch pointers to a specified commit.
Syntax:

Revert a commit

git revert

Reset to a previous commit

git reset [--soft | --mixed | --hard]
Working with Git Squash
Squashing commits combines multiple commits into a single commit. This is often done to clean up the commit history before merging a feature branch into the main branch.

Use Case: Before merging a feature branch into the main branch, you want to clean up the commit history by combining multiple small, related commits into a single, meaningful commit.

example:

Start an interactive rebase

git rebase -i HEAD~3 # Squash last 3 commits

In the interactive rebase editor, change 'pick' to 'squash' for commits you want to squash

What is Git Fork?
A Git fork is a copy of a repository that allows you to freely experiment with changes without affecting the original repository. Forks are commonly used in open-source collaboration on platforms like GitHub.

git rebase, git merge, and git cherry-pick:

  1. Git Merge Use Case:

Merge is a straightforward way to integrate changes from one branch into another.
It creates a new commit that combines the commit histories of the merged branches, preserving the history of each branch.
Syntax:

Merge feature-branch into main

git checkout main
git merge feature-branch
Pros:

Preserves the history of both branches.
Easy to understand and use.
Cons:

Can result in a cluttered commit history, especially in long-running projects with many branches.

  1. Git Rebase Use Case:

Rebase is used to incorporate changes from one branch into another by reapplying commits on top of another branch.
It creates a linear history, making the commit history cleaner and easier to follow.
Syntax:

Rebase feature-branch onto main

git checkout feature-branch
git rebase main
Pros:

Creates a cleaner, linear commit history.
Simplifies the process of integrating changes from feature branches into the main branch.
Cons:

Rewrites commit history, which can lead to conflicts and potential loss of work if not used carefully.
Should not be used on shared branches as it alters commit history.

  1. Git Cherry-pick Use Case:

Cherry-pick allows you to select specific commits from one branch and apply them to another branch.
Useful for applying individual commits, such as bug fixes or specific features, to another branch without merging the entire branch.
Syntax:

Cherry-pick a commit from feature-branch into main

git checkout main
git cherry-pick
Pros:

Allows for selective integration of commits.
Useful for applying critical bug fixes or specific features to release branches.
Cons:

Can lead to disjointed commit histories if used excessively.
May introduce conflicts that need to be resolved manually.
Choosing the Right Approach
Merge: Use when you want to preserve the commit history of both branches and don’t mind a potentially cluttered history.
Rebase: Ideal for creating a clean, linear commit history and integrating changes from feature branches into the main branch.
Cherry-pick: Useful for selectively applying individual commits to another branch without merging the entire branch.
Each approach has its strengths and weaknesses, so it’s essential to understand when to use each one based on your project’s needs and collaboration workflow.

Git Integration on VScode, Git Authentication with GitHub via SSH and HTTPS Protocol
Visual Studio Code (VS Code) has built-in Git integration, allowing you to perform most Git operations directly within the editor. You can authenticate with GitHub using SSH keys or HTTPS credentials for secure access to repositories.

GitHub Integration and Collaboration
GitHub Introduction, Creating Repositories, PR’s
GitHub is a popular platform for hosting Git repositories and collaborating on projects. You can create repositories, open pull requests (PRs) to propose changes, review code, and merge changes into the main branch.

Thus, with this comprehensive guide, you should now have a solid understanding of Git, from basic version control concepts to advanced operations and collaboration on platforms like GitHub.

Top comments (0)