Day 12 Task: Deep Dive in Git & GitHub for DevOps Engineers
Git is a distributed version control system that allows developers to track changes in their code, collaborate with others, and manage software projects efficiently. Here’s an overview of what Git is and how it works:
What is Git?
Version Control System: Git helps manage versions of files and directories, allowing you to keep track of changes over time.
Distributed: Unlike centralized version control systems, every developer has a full copy of the repository, including its history, on their local machine. This enables offline work and reduces dependency on a central server.
Fast and Efficient: Git is designed to handle large projects quickly and efficiently, enabling rapid branching and merging.
Key Concepts
Repository (Repo): A Git repository is a directory that contains all of your project files and the history of changes made to them. A repository can be local (on your computer) or remote (on a server like GitHub or GitLab).
Commit: A commit is a snapshot of your project at a specific point in time. Each commit has a unique identifier (hash) and includes a commit message that describes the changes made.
Branch: A branch is a separate line of development. You can create branches to work on features or fixes independently. The default branch in Git is usually called
main
ormaster
.Merge: Merging is the process of integrating changes from one branch into another. This is how you bring together different lines of development.
Remote: A remote repository is a version of your project that is hosted on the internet or another network. You can push your local changes to a remote repository or pull updates from it.
How Git Works
Initializing a Repository: You start by creating a Git repository using:
bashCopy codegit init
Tracking Changes: You add files to the staging area to track changes:
bashCopy codegit add filename
Committing Changes: After staging, you commit the changes with:
bashCopy codegit commit -m "Commit message"
Branching and Merging: You can create branches to develop features independently:
bashCopy codegit branch new-feature git checkout new-feature
After completing your work, you can merge it back into the main branch:
bashCopy codegit checkout main git merge new-feature
Working with Remotes: To collaborate with others, you push your local changes to a remote repository:
bashCopy codegit push origin main
You can also pull changes from a remote repository:
bashCopy codegit pull origin main
Viewing History: You can view the history of commits using:
bashCopy codegit log
- Set your user name and email address, which will be associated with your commits.
Git Branching
Branches are a core concept in Git that allow you to isolate development work without affecting other parts of your repository. Each repository has one default branch, and can have multiple other branches. You can merge a branch into another branch using a pull request.
Branches let you develop features, fix bugs, or safely experiment with new ideas in a contained area of your repository.
Git Revert and Reset
Git reset and git revert are two commonly used commands that allow you to remove or edit changes you’ve made in the code in previous commits. Both commands can be very useful in different scenarios.
Git Rebase and Merge
What Is Git Rebase?
Git rebase is a command that lets users integrate changes from one branch to another, and the commit history is modified once the action is complete. Git rebase helps keep a clean project history.
What Is Git Merge?
Git merge is a command that allows developers to merge Git branches while keeping the logs of commits on branches intact. Even though merging and rebasing do similar things, they handle commit logs differently.
For a better understanding of Git Rebase and Merge, check out this article.
Tasks with Answers
Task 1: Feature Development with Branches
Create a Branch and Add a Feature:
- Add a text file called
version01.txt
inside theDevops/Git/
directory with “This is the first feature of our application” written inside.
- Add a text file called
Answer
- Create a new branch from
master
.
Summary
Git provides a powerful and flexible way to manage code changes, collaborate with others, and maintain a clear history of a project. Its distributed nature makes it particularly well-suited for teams of all sizes.
If you have specific questions or need more details on any aspect of Git, feel free to ask!