Welcome to this blog post on Advance Git & GitHub for DevOps Engineers! In this post, we will cover some key concepts and commands that are essential for working with Git and GitHub effectively. π
Git Branching πΏ
Branching in Git is a powerful feature that allows developers to isolate development work without affecting other parts of the repository. Every repository has a default branch (usually master
), and you can create multiple other branches for specific tasks. This isolation helps in developing features, fixing bugs, and experimenting with new ideas without disrupting the main codebase.
Merging Branches π
Merging is the process of integrating changes from one branch into another. This is often done using pull requests, which allow for review and collaboration. Merging is essential for combining new features or bug fixes back into the main branch.
Git Revert and Reset βͺ
Git provides two important commands, git reset
and git revert
, that help manage changes in previous commits.
git reset allows you to unstage changes or move the HEAD to a previous commit, effectively removing or altering commits.
git revert creates a new commit that undoes specific changes from a previous commit, leaving a history of changes intact.
Git Rebase and Merge π
Git Rebase π
Git rebase integrates changes from one branch to another by modifying the commit history. It's often used to streamline the commit history and make it more linear. Rebase helps in avoiding the complexities that can arise during traditional merge operations.
Git Merge π
Git merge combines branches by creating a new commit that has two parent commits. This preserves the commit history of both branches. There are two methods of merging branches: git merge
and git rebase
. Both achieve the same goal but differ in their commit history representation.
π Task 1: Adding and Modifying Commits π±
Step 1: Creating a New Branch and Adding a Text File
cd /path/to/Devops/Git # Navigate to the Git folder
git checkout -b dev # Create and switch to the 'dev' branch
echo "This is the first feature of our application" > version01.txt
Step 2: Making Changes and Committing
git status # See the changes
git add version01.txt # Add the changes
git commit -m "Added new feature" # Commit with a message
Step 3: Pushing Changes to Remote Repository
git push origin dev # Push changes to remote 'dev' branch
Step 4: Adding More Content and Making Commits
echo "This is the bug fix in development branch" >> Devops/Git/version01.txt
git add Devops/Git/version01.txt
git commit -m "Added feature2 in development branch"
echo "This is gadbad code" >> Devops/Git/version01.txt
git add Devops/Git/version01.txt
git commit -m "Added features in development branch"
echo "This feature will gadbad everything from now." >> Devops/Git/version01.txt
git add Devops/Git/version01.txt
git commit -m "Added feature4 in development branch"
Step 5: Restoring to a Previous Version
git reset HEAD~1 # Remove the last commit but keep changes
# OR
git reset --hard HEAD~1 # Remove the last commit and discard changes
Remember to replace /path/to/Devops/Git
with the actual path to your Git folder. Additionally, be cautious when using git reset --hard
, as it permanently discards changes.
π² Git Branching and MergingπΏ
π Step 1: Creating and Merging Branches πΏ
Open your terminal or Git Bash. π»
Navigate to your project directory using
cd path/to/your/project
. πCreate a new repository or use an existing one with
git init
. πCreate a new file named
README.md
or any other file usingtouch
README.md
. πAdd and commit the initial file to the master branch:
git add README.md git commit -m "Initial commit"
Create a new branch named
dev
:git checkout -b dev
Make changes to the file, like adding more content or modifying it.
Add and commit the changes to the
dev
branch:git add README.md git commit -m "Added content in dev branch"
Switch back to the
master
branch:git checkout master
Merge the changes from the
dev
branch intomaster
:git merge dev