Advance Git & GitHub for DevOps Engineers
Git branching:
Use a branch to isolate development work without affecting other branches in the 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 allow you to develop features, fix bugs, or safely experiment with new ideas in a contained area of your repository.
Git revert and reset:
Two commonly used tools that git users will encounter are git reset and git revert. The benefit of both of these commands is that you can use them to remove or edit changes you’ve made in the code in previous commits.
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 logs are modified once the action is complete. Git rebase was developed to overcome merging’s shortcomings, specifically regarding logs.
What is git merge:
Git merge is a command that allows developers to merge Git branches while the logs of commits on branches remain intact. The merge wording can be confusing because we have two methods of merging branches and one of those ways is actually called “merge,” even though both procedures do essentially the same thing.
TASK 1: Add a text file called version01.txt inside the Devops/Git/ with “This is the first feature of our application” written inside. This should be in a branch coming from master
, [hint try git checkout -b dev
], switch to dev
branch ( Make sure your commit message will reflect as "Added new feature"). [Hint use your knowledge of creating branches and Git commits commands.]
TASK 2:
Demonstrate the concept of branches with 2 or more branches with a screenshot.
add some changes to
dev
the branch and merge that branch inmaster
as a practice try git rebase too, and see what difference you get version01.txt should reflect at the local repo first followed by the Remote repo for review. [Hint use your knowledge of Git push and git pull commands here].
Add new commit in
dev
branch after adding the below-mentioned content in Devops/Git/version01.txt: While writing the file make sure you write these lines1st line>> This is the bug fix in a development branch
Added feature2 in the development branch
2nd line>> This is gadbad code
- Commit this with the message “ Added feature3 in the development branch
3rd line>> This feature will gadbad everything from now.
Commit with the message “ Added feature4 in the development branch
git log
: Git log is a utility tool to review and read the history of everything that happens to a repository.
git log --oneline
- The online option is used to display the output as one commit per line.
Restore the file to a previous version where the content should be “This is the bug fix in the development branch” [Hint use git revert or reset according to your knowledge]
git reset
: Reset the current HEAD to the specified state.syntax:
git reset <commit_ID>
In the below image, if you will do a git log you can see only two commits as the reset ones are deleted.
TASK 3:
Demonstrate the concept of branches with 2 or more branches with a screenshot.
git branch
:A branch is a version of the repository that diverges from the main working project. It is a feature available in most modern version control systems. A Git project can have more than one branch
Git command :
Create a new Branch: git branch -b <branch name >
Show git branch: git branch
To go to a specific branch: git checkout <branch-name>
Creating and going to that branch: git checkout -b <branch-name>
add some changes to
dev
the branch and merge that branch inmaster
git merge
: Git merge is a command that allows developers to merge Git branches while the logs of commits on branches remain intact.As a practice try git rebase too, and see what difference you get.
git rebase:
Rebasing is the process of moving or combining a sequence of commits to a new base commit. Rebasing is most useful and easily visualized in the context of a feature branching workflow. The primary reason for rebasing is to maintain a linear project history.
git rebase <base>
--
Git Stash:
Git stash is a command that allows you to temporarily save changes you have made in your working directory, without committing them. This is useful when you need to switch to a different branch to work on something else, but you don't want to commit the changes you've made in your current branch yet.
To use Git Stash, you first create a new branch and make some changes to it. Then you can use the command git stash to save those changes. This will remove the changes from your working directory and record them in a new stash. You can apply these changes later. git stash list command shows the list of stashed changes.
You can also use git stash drop to delete a stash and git stash clear to delete all the stashes.
Cherry-pick:
Git cherry-pick is a command that allows you to select specific commits from one branch and apply them to another. This can be useful when you want to selectively apply changes that were made in one branch to another.
To use git cherry-pick, you first create two new branches and make some commits to them. Then you use git cherry-pick <commit_hash> command to select the specific commits from one branch and apply them to the other.
Resolving Conflicts:
Conflicts can occur when you merge or rebase branches that have diverged, and you need to manually resolve the conflicts before it can proceed with the merge/rebase. git status command shows the files that have conflicts, the git diff command shows the difference between the conflicting versions and the git add command is used to add the resolved files.
TASK 1:
Create a new branch and make some changes to it.
git branch
the command is used to check the branches in git.Create a new branch branch2 with git
checkout -b branch2
Use git stash to save the changes without committing them.
Switch to a different branch, make some changes, and commit them.
Use git stash pop to bring the changes back and apply them on top of the new commits.
TASK 2:
In version01.txt of the development branch add the below lines after “This is the bug fix in the development branch” that you added in Day10 and reverted to this commit.
Line2>> After bug fixing, this is the new feature with minor alterations”
Commit this with the message “ Added feature2.1 in development branch”
Line3>> This is the advancement of the previous feature
Commit this with the message “ Added feature2.2 in development branch”
Line4>> Feature 2 is completed and ready for release
Commit this with the message “ Feature2 completed”
All these commits messages should be reflected in the Production branch too which will come out from the Master branch (Hint: try rebase).
below, I have switched from dev to production and used git rebase command, now all the commits are merged in production.
TASK 3:
In the Production branch Cherry pick Commit “Added feature2.2 in development branch” and added the below lines in it:
Switch to production branch.
for cherry-pick use the command**:** git cherry-pick <commit ID for feature2.2>
here cherry-picking gives conflict error. so we need to solve conflict errors.
After resolving the conflict error use the command git cherry-pick <commit ID> or git cherry-pick --continue
Thank you for reading this blog! :)
Happy learning!
#Learningwithsharing
Subscribe to my newsletter
Read articles from RAKESH REVASHETTI directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
RAKESH REVASHETTI
RAKESH REVASHETTI
I'm Rakesh, a DevOps engineer who enjoys automation, continuous integration and deployment. With good hands-on experience in DevOps & cloud technology, i'm proficient in various tools and technologies related to infrastructure automation, containerization, cloud platform, monitoring and CI/CD.