Day-11 | Git Interview Q&A and Commands for DevOps | Real World Example |#devops #github #git #2023
Introduction to Git Commands for DevOps
Overview of the Course
- Abhishek introduces himself and apologizes for not posting a video the previous day due to personal reasons.
- The course is structured over 45 days, with prior coverage from Day 0 to Day 10 focusing on fundamentals like Linux scripting and basics of Git.
Purpose of Today's Session
- The session aims to discuss useful Git commands specifically for DevOps engineers but also applicable to software engineers in general.
- A practical example will be used: writing a calculator functionality in a file named
calculator.sh.
Creating and Managing a Git Repository
Using the User Interface (UI)
- Abhishek explains how to create a repository using the UI on platforms like GitHub, emphasizing its straightforward nature without command-line involvement.
- Steps include clicking "New," providing repository details, and selecting public or private options before creating it.
Initializing a Repository via Command Line
- To initialize a local repository through CLI, the command
git initis used. This creates an empty Git repository in the current directory.
- After running
git init, users can check their directory with commands likels -ato see that a.gitfolder has been created.
Understanding .git Directory
Importance of .git Folder
- The
.gitfolder is crucial as it tracks changes, logs history, and manages credentials for repositories.
- Developers must be cautious about sensitive information; accidental pushes can occur if passwords are included in scripts.
Preventing Sensitive Data Exposure
- To prevent sensitive data from being pushed accidentally, developers can use pre-commit hooks located within the
.git/hooksdirectory.
Tracking Changes with Git
Initializing Tracking
- After initializing a repository with
git init, no files are tracked until explicitly added. Runninggit statusshows untracked files likecalculator.sh.
Version Control Benefits
Understanding Git Basics
Introduction to Git Tracking
- The speaker introduces the concept of using Git for tracking changes in code, emphasizing its importance when users forget to undo changes made in files.
- Demonstrates how to track a file named
calculator.shby adding it to Git with the commandgit add calculator.sh, which prepares it for version control.
Viewing Changes and Managing Files
- The speaker shows how to edit the file by adding a line of code (e.g.,
X = 1 + 2) and explains how to view changes usinggit diff.
- Discusses options for managing changes, including removing unwanted edits or confirming them with
git add.
Purpose of git add and Version Control
- Explains that the primary purpose of
git addis to track changes or new files within a repository, highlighting its role in version control.
- Emphasizes that without using
git add, any modifications will not be recognized by the.gitdirectory, making it crucial for active development.
Committing Changes
- After staging files with
git add, the next step is committing those changes usinggit commit -m "message", which records a snapshot of your project at that point in time.
- The speaker clarifies what "changes to be committed" means, indicating that these are staged but not yet finalized until committed.
Importance of Commit Messages
- Highlights the significance of providing meaningful commit messages as they help identify who made specific changes and why, especially in collaborative environments.
- Discusses scenarios where tracking contributions becomes essential, such as identifying problematic code introduced by other developers.
Navigating Commit History
- Introduces the use of commands like
git logto view commit history, allowing developers to trace back through previous versions if issues arise after new commits.
- Stresses the need for a robust mechanism for tracking file changes over time so teams can revert back if necessary.
Final Steps: Pushing Changes
- Concludes with an explanation of pushing local commits to a remote repository (e.g., GitHub), ensuring team members have access to updated codebases.
Understanding Git Workflow
Introduction to Git and Remote Repositories
- The importance of a distributed system is highlighted, allowing everyone access to code. Using the command
git push, developers can upload their entire code to various repositories like GitHub or Bitbucket.
Common Git Commands
- Interviewers often ask about the git workflow used in organizations. Key commands include:
git add
git commit -m "commit message"
git push
Understanding Remote Repositories
- If you execute
git pushwithout a remote repository configured, changes won't be pushed. A remote repository (e.g., on GitHub) is necessary for this action.
Cloning Repositories
- Developers typically create repositories through UI and use cloning mechanisms. For example, using the URL from a project like Argo CD allows local downloading via
git clone.
Local vs Remote Repository Issues
- When making changes locally and pushing them, if there’s no remote reference set up, the command will not work as intended.
Configuring Remote References
- To resolve issues with
git push, ensure that a remote reference exists. Use the command:
git remote add <repository-url>
Verifying Remote Configuration
- You can check your remote references using:
git remote -v
This shows whether your local repository is linked to any remote location.
Adding a Remote Repository
- If no remote exists, you must add one using:
git remote add <remote-name> <repository-url>
Summary of Cloning Process
What is Git Clone and How to Use It?
Understanding Git Clone
- Definition: Git clone is a process used to pull code from repositories like GitHub or Bitbucket.
- Methods of Cloning: There are multiple ways to clone code, including HTTPS, SSH, and GitHub CLI. The focus here will be on HTTPS and SSH.
Cloning via HTTPS
- Authentication: When using HTTPS, you authenticate with your GitHub password. Creating an account on GitHub or similar platforms is necessary for this method.
Cloning via SSH
- Public/Private Key Concept: In contrast to HTTPS, SSH uses public/private key authentication. Users must generate a public key if they don't already have one.
- Generating Keys: To create a new public/private key pair, use the command
ssh-keygen -t rsa. This generates keys stored in the.sshdirectory.
Setting Up SSH Keys
- Locating Public Key: After generating keys, locate the public key file (usually named
id_rsa.pub) in the.sshfolder within your home directory.
- Adding Public Key to GitHub: Copy the contents of your public key and add it under settings in your GitHub account by selecting "Add a new SSH key."
Differences Between Clone and Fork
- Cloning vs. Forking: Cloning downloads a repository's current state while forking creates an independent copy that does not automatically update with changes made in the original repository.
- Distributed Version Control System: Git allows users to create multiple copies of repositories for collaboration without affecting each other's work unless explicitly updated.
Understanding Git: Forks, Clones, and Branches
The Difference Between Forking and Cloning
- Fork vs. Clone: Forking creates a copy of a repository for collaboration, while cloning downloads a specific repository to your local machine.
Key Concepts in Git
- Distributed Version Control: Git's fundamental advantage lies in its distributed version control system, which is enhanced through the concept of forking.
Overview of Git Lifecycle
- Git Lifecycle Understanding: The discussion covers how to create repositories locally and remotely, push files, and download files using HTTPS and SSH mechanisms.
Importance of Branching in Development
- Branch Creation: By default, a new repository has only one main branch. Creating additional branches allows developers to work on features without affecting the main codebase.
- Use Case for Branching: In organizations, branching is crucial when developing large features (e.g., adding advanced functionalities to an application), preventing disruptions to existing functionality.
Managing Large Features with Branches
- Real-world Example: When introducing significant changes (like adding carpenter services on Amazon), developers should work on separate branches to avoid breaking existing applications during development.
- Testing Changes Safely: Developers can test their changes in isolated branches before merging them into the main branch once they are confident about the stability of their code.
Practical Steps for Creating Branches
- Adding Functionality: An example is given where a new feature (subtraction functionality in a calculator app) is added before creating a new branch for further development.
Branching and Merging in Git
Understanding Branch Creation
- A new branch called "division" is created from the main branch using
git checkout -b division.
- The user adds a calculator function to the division branch, indicating ongoing development.
- Changes are committed with
git add calculator.shandgit commit -m "add division".
Exploring Commit History
- Using
git log, the user observes that commits from both the main and division branches are visible, highlighting how branching works.
- The purpose of branching is emphasized: it allows developers to isolate different development activities without affecting the main codebase.
Merging Branches
- To integrate changes from one branch into another, merging is necessary. This can be done through three methods:
git merge,git rebase, orgit cherry-pick.
- Cherry-picking allows for selective integration of specific commits from one branch to another.
Using Cherry-Pick
- The command
git log divisionshows changes made in the division branch without switching branches.
- Cherry-picking is demonstrated by copying a commit ID and applying it to the main branch using
git cherry-pick <commit-id>.
Limitations of Cherry-Picking
- While cherry-picking is straightforward for a few commits, it becomes impractical for larger projects with many commits due to potential conflicts.
Merge vs. Rebase
Practical Demonstration Setup
- The speaker prepares to demonstrate both merging and rebasing by creating a new branch named "merge example".
Adding New Features
- A new functionality (multiplication feature in calculator.sh) is added on this new branch, followed by committing these changes.
Understanding Git: Rebase vs. Merge
Introduction to Rebase and Merge
- The speaker introduces the concept of rebase, noting that multiplication functionality is not included in the rebase example but is present in the merge example. This highlights a key difference between these two methods.
- A new functionality called "percentage" is added, demonstrating how changes can be made independently on branches without conflict.
Viewing Commit History
- The command
git log --onelineis introduced as a way to view commits succinctly, showing a simplified list of changes made.
- The speaker compares commit logs from both rebase and merge examples, emphasizing the differences in how changes are recorded.
Merging Changes into Main Branch
- The process of merging changes back into the main branch begins with checking out the main branch and making additional changes to
calculator.sh.
- After adding a test change, the speaker prepares to merge both rebase and merge changes into the main branch while explaining potential conflicts.
Handling Merge Conflicts
- Upon attempting to perform a git merge, an error occurs due to uncommitted changes. The importance of committing before merging is highlighted.
- When executing
git rebase, conflicts arise because multiple branches have modified the same file. This illustrates real-world scenarios where developers must resolve conflicts collaboratively.
Resolving Conflicts
- To fix merge conflicts, collaboration with other developers is essential. The speaker emphasizes communication when determining which code should be retained during merges.
- After resolving conflicts by including both percentage and multiplication functionalities, the speaker demonstrates how to finalize these changes through git commands.
Finalizing Changes and Observing Results
- Following conflict resolution, executing
git logreveals how different commits are integrated from both branches—demonstrating practical outcomes of using rebase versus merge.
- A visual representation of branching workflows is promised for better understanding; this includes creating separate branches for merging and rebasing from a common main branch.
Understanding Git Merge vs. Rebase
Overview of New Functionalities
- The discussion begins with the introduction of a new functionality called "percentage" added by developers in the main branch, alongside another feature termed "test commit."
Merging Changes
- When merging branches, all changes from different functionalities are combined into one single branch. However, the order of updates differs between merge and rebase.
Differences Between Merge and Rebase
- The key distinction lies in how changes are organized: merge creates a non-linear history while rebase maintains a linear commit history.
- A diagrammatic representation is used to illustrate that with merge, changes appear at the topmost position, whereas rebase updates occur before existing commits.
Impact on Change Tracking
- Using git merge can complicate tracking changes as it disrupts the chronological order of commits; developers may struggle to understand which change followed another.
- In contrast, git rebase allows for easier tracking since it preserves a linear sequence of commits.
Practical Implications for Developers
- For large projects where tracking commit history is crucial, using rebase is recommended to maintain clarity on the sequence of changes.
- If maintaining a linear commit history is not essential, developers may opt for merge instead.
Summary of Key Points
- Both git merge and git rebase achieve similar outcomes but differ significantly in how they structure commit histories.
- The speaker encourages practical experimentation with both methods to better understand their differences.
Additional Resources and Questions