Advance Git & GitHub for DevOps Engineers
Day 10 : #90DaysOfDevOps Challange
Table of contents
Version control is an essential aspect of software development. Git, a distributed version control system, is a popular tool that helps developers manage their codebases effectively. Git provides several powerful features that allow developers to work collaboratively, keep track of changes, and revert to previous versions of their code. In this article, we'll explore some of Git's most powerful features, including branching, reverting and resetting, rebasing, and merging.
Git Branching
Git branching is a powerful feature that allows developers to create multiple independent branches of their codebase. Branches are useful when developers want to work on a new feature or fix a bug without affecting the main codebase. Git allows developers to create branches easily and switch between them.
To create a new branch, use the git branch
command followed by the name of the new branch. For example, to create a new branch called feature-branch
, use the following command:
git branch feature-branch
To switch to a different branch, use the git checkout
command followed by the name of the branch. For example, to switch to the feature-branch
, use the following command:
git checkout feature-branch
Once you have made changes in your branch, you can merge it back into the main codebase using the git merge
command. For example, to merge the feature-branch
into the master
branch, use the following command:
git checkout master
git merge feature-branch
Git Revert and Reset
Git revert and reset are two powerful features that allow developers to undo changes made to their codebase. Revert creates a new commit that undoes the changes made in a previous commit, while reset removes the changes from the current branch entirely.
To revert a commit, use the git revert
command followed by the commit hash. For example, to revert the last commit, use the following command:
git revert HEAD
To reset a commit, use the git reset
command followed by the commit hash. For example, to remove the last commit entirely, use the following command:
git reset --hard HEAD^
Git Rebase
Git rebase is a feature that allows developers to change the history of their codebase by modifying the order of commits. Rebase is useful when multiple developers are working on the same codebase and want to ensure that their commits are applied in the correct order.
To rebase a branch, use the git rebase
command followed by the name of the branch you want to rebase onto. For example, to rebase the feature-branch
onto the master
branch, use the following command:
git checkout feature-branch
git rebase master
Git Merge
Git merge is a feature that allows developers to combine multiple branches into a single branch. Merge is useful when developers have multiple branches with different features or bug fixes that they want to combine into a single branch.
To merge a branch, use the git merge
command followed by the name of the branch you want to merge. For example, to merge the feature-branch
into the master
branch, use the following command:
git checkout master
git merge feature-branch
Conclusion
Git provides several powerful features that allow developers to manage their codebases effectively. Git branching, reverting and resetting, rebasing, and merging are some of the most important features that developers can use to collaborate effectively and ensure that their code is stable and reliable. By mastering these features, developers can become more efficient and productive, and deliver high-quality code faster.
****************************Task Day 10************************************
Task 1. Add a text file called version01.txt inside the Devops/Git/ with “This is first feature of our application” written inside. This should be in a branch coming from master
swithch to dev
branch ( Make sure your commit message will reflect as "Added new feature").
- version01.txt should reflect at local repo first followed by Remote repo for review. [Hint use your knowledge of Git push and git pull commands here]
Add new commit in dev
branch after adding below mentioned content in Devops/Git/version01.txt: While writing the file make sure you write these lines
- 1st line>> This is the bug fix in development branch
- Commit this with message “ Added feature2 in development branch”
- 2nd line>> This is gadbad code
- Commit this with message “ Added feature3 in development branch
- 3rd line>> This feature will gadbad everything from now.
- Commit with message “ Added feature4 in development branch
Restore the file to a previous version where the content should be “This is the bug fix in development branch”
[Hint use git revert or reset according to your knowledge]
To add a text file called version01.txt inside the Devops/Git/ with "This is the first feature of our application" written inside, follow the steps below:
Open your terminal or command prompt and navigate to the Devops/Git/ directory.
Run the following command to create a new branch called
dev
and switch to it:git checkout -b dev
Create a new file called
version01.txt
inside the Devops/Git/ directory with the following content:This is first feature of our application
Add the new file to the Git repository and commit the changes with the message "Added new feature":
git add version01.txt git commit -m "Added new feature"
Push the changes to the remote repository for review:
git push origin dev
To add new commit in dev
branch after adding below mentioned content in Devops/Git/version01.txt, follow the steps below:
Open the
version01.txt
file and add the following lines:This is the bug fix in development branch
Add and commit the changes with the message "Added feature2 in development branch":
git add version01.txt git commit -m "Added feature2 in development branch"
Add the following lines to the
version01.txt
file:This is gadbad code
Add and commit the changes with the message "Added feature3 in development branch":
git add version01.txt git commit -m "Added feature3 in development branch"
Add the following lines to the
version01.txt
file:This feature will gadbad everything from now.
Add and commit the changes with the message "Added feature4 in development branch":
git add version01.txt git commit -m "Added feature4 in development branch"
To restore the file to a previous version where the content should be "This is the bug fix in development branch", follow one of the two methods below:
Using git revert:
git revert <commit hash>
Replace
<commit hash>
with the hash of the commit where the content was changed. For example, to revert the last commit, run the following command:git revert HEAD
Using git reset:
git reset <commit hash> --hard
Replace
<commit hash>
with the hash of the commit where the content was changed. For example, to reset to the commit with the message "Added feature2 in development branch", run the following command:git reset HEAD~2 --hard
This will reset the
version01.txt
file to the state it was in after the "Added feature2 in development branch" commit.
Task 2:
- Demonstrate the concept of branches with 2 or more branches with screenshot. - add some changes to dev
branch and merge that branch in master
- as a practice try git rebase too, see what difference you get. Give step be step process
Open a terminal or command prompt and navigate to the directory where you want to create your Git repository.
Use the
git init
command to initialize a new Git repository. This will create a new directory called.git
that contains all the necessary files for Git to work.Use the
git branch
command to create a new branch calleddev
. This will create a new branch that is identical to the current branch (in this case,master
).git branch dev
Use the
git branch
command again to list all branches in the repository. The current branch should be highlighted with an asterisk.git branch
Switch to the
dev
branch using thegit checkout
command.git checkout dev
Make some changes to a file in the
dev
branch. For example, let's add a new line to a file calledfile.txt
.echo "This is a new line in dev branch" >> file.txt
Use the
git add
command to stage the changes you made to the file.git add file.txt
Use the
git commit
command to create a new commit with the changes you made to the filegit commit -m "Added new line in dev branch"
Switch back to the
master
branch using thegit checkout
command.git checkout master
Merge the changes from the
dev
branch into themaster
branch using thegit merge
command.git merge dev
Use the
git log
command to view the commit history of the repository. You should see a new commit that contains the changes from thedev
branch.git log
To practice Git rebase, switch back to the
dev
branch using thegit checkout
command.git checkout dev
Make some more changes to the file.
echo "This is another new line in dev branch" >> file.txt
Use the
git add
command to stage the changes you made to the file.
git add file.txt
- Use the
git commit
command to create a new commit with the changes you made to the file.
git commit -m "Added another new line in dev branch"
Switch back to the
master
branch using thegit checkout
command.git checkout master
Use the
git log
command to view the commit history of the repository. You should see two new commits from thedev
branch.git log
Use the
git rebase
command to rebase thedev
branch onto themaster
branch.git rebase dev