Maximizing Collaboration and Version Control: Advanced Git & GitHub for DevOps Engineers
Table of contents
As a DevOps engineer, you probably understand the importance of version control in your project management. Git is a powerful tool with many features that help you keep track of changes and collaborate with your team. In this blog post, we'll explore some of the advanced Git features that every DevOps engineer should know - branching, revert, reset, rebase, and merge.
Git Branching
Branching is a powerful feature in Git that allows you to create alternate versions of your project. You can create a new branch to work on a new feature, fix a bug, or experiment without affecting the main codebase. You can then switch back and forth between branches, merge them, or delete them.
Here's how you can create a new branch in Git:
git branch new-feature
This creates a new branch named "new-feature". You can then switch to this branch using:
git checkout new-feature
Now you can make changes to your code without affecting the main branch. Once you're done, you can merge the changes back to the main branch using:
git checkout main
git merge new-feature
Git Revert and Reset
Sometimes you may need to undo changes in your project. Git provides two ways to do this - revert and reset. Revert creates a new commit that undoes the changes made in a previous commit, while reset removes the commits from the branch.
To revert a commit, you can use:
git revert <commit>
This creates a new commit that undoes the changes in the specified commit.
To reset a commit, you can use:
git reset <commit>
This removes the commits from the branch, effectively undoing the changes made in those commits. Use this with caution as it can permanently remove commits.
Git Rebase and Merge
When you're working on a large project with many contributors, conflicts can arise when merging changes from different branches. Git provides two ways to handle this - rebase and merge.
Rebase is a way to integrate changes from one branch into another by applying the changes on top of the other branch. This creates a linear history of commits and avoids merge conflicts.
git checkout new-feature
git rebase main
This applies to the changes made in "new-feature" on top of "main".
Merge, on the other hand, creates a new commit that combines the changes made in different branches. This creates a branch with a merge commit, showing that the branches were merged.
git checkout main
git merge new-feature
This creates a new commit that combines the changes made in "main" and "new-feature".
TASK:
Step 1: Create a new branch called "dev" and switch to it.
git checkout -b dev
Step 2: Create a text file called "version01.txt" with the content "This is the first feature of our application".
echo "This is the first feature of our application" > Devops/Git/version01.txt
Step 3: Add and commit the changes with the commit message "Added new feature".
git add Devops/Git/version01.txt
git commit -m "Added new feature"
Step 4: Push the changes to the remote repository.
git push origin dev
Step 5: Add new commits to the "dev" branch with the specified content in "version01.txt".
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 feature3 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 6: Restore the file to a previous version where the content should be "This is the bug fix in the development branch".
git log
Note the commit hash of the commit where the content is "This is the bug fix in the development branch".
To use git revert:
git revert <commit-hash>
This will create a new commit that undoes the changes made in the specified commit.
To use git reset:
git reset <commit-hash>
This will reset the branch to the specified commit, erasing the changes made in the subsequent commits.
After reverting or resetting, you can add and commit the changes with an appropriate commit message, and push the changes to the remote repository.
git add Devops/Git/version01.txt
git commit -m "Restored file to previous version"
git push origin dev
That's it! You have now created a branch, made changes to a file, committed the changes, pushed them to the remote repository, and restored the file to a previous version using Git commands. Git provides powerful version control features that help developers effectively manage their codebase and collaborate with their teams. Happy coding!