Advance Git & GitHub for DevOps Engineers
GIT Branching:
Branching in Git allows us to work on different features, bug fixes, or experiments in isolation, without affecting other parts of the repository. Each repository has a default branch, usually called "master" or "main," and we can create multiple other branches to work on specific tasks.
Git branching enhances productivity by ensuring proper coordination among developers and helping organize a series of planned, structured releases. Having a branching is necessary to avoid conflicts when merging and to allow for the easier integration of changes into the master.
Git revert and reset
git reset and git revert are commands that you can use to remove or edit changes you’ve made in the code in previous commits. Understanding how both of them work will save you a significant amount of time, allow you to make cleaner code, and have more confidence in making commits when you do.
Git revert:
git revert is used to create a new commit that undoes the changes made in a previous commit.
It is a safe way to undo changes since it does not modify the commit history. Instead, it adds new commits that reverse the changes made by a specific commit.
When you run git revert <commit>, Git will create a new commit that undoes the changes introduced by the specified commit.
This command is useful when you want to undo changes while preserving the commit history and keeping a record of the reverted changes.
- Git reset:git reset is used to move the current branch pointer to a different commit or to modify the staging area and working directory.It allows you to reset the state of the repository to a previous commit, discarding commits and potentially losing changesgit reset --hard <commit> moves the branch pointer to the specified commit and discards all changes made after the specified commit. It resets both the staging area and the working directory.
Git Rebase and Merge:
git rebase:
- git rebase is used to incorporate changes from one branch onto another by moving or replaying commits.It allows you to apply the commits of one branch on top of another branch, resulting in a linear commit history.When you run git rebase <branch>, Git identifies the common ancestor commit between the current branch and the specified branch. It then applies each commit from the current branch onto the specified branch, one by one.
git merge:
git merge is used to combine changes from one branch into another by creating a new merge commit.
It integrates the changes from a source branch into a target branch, preserving the commit history of both branches.
When you run git merge <branch>, Git creates a new commit that represents the combination of changes from the specified branch into the current branch.
Task1:
Add a text file called version01.txt inside the DevOps/Git/ with “This is the first feature of our application” written inside
Create a text file called version01.txt inside the DevOps/Git/directory and Write the content "This is the first feature of our application" in the file.
- Create a new branch named dev from the master branch git checkout -b dev
Commit your changes with the message "Added new feature."
git add version01.txt
git commit -m "Added new feature"
Push to Remote:Push the dev branch to the remote repository for review.
git push -u origin dev
Task 2:
Make additional changes to version01.txt as requested & commit these changes with appropriate messages:
Task3 :
Demonstrate the concept of branches with a screenshot.
- As a practice try git rebase too, and see what difference you get.
Git stash:
In Git, the git stash command is used to save changes that are not ready to be committed yet, allowing you to switch to a different branch or work on a different task without committing incomplete work. It temporarily stores your modifications and reverts the working directory to the last committed state.
Run git stash to save your changes. Git will create a new stash that includes all modified and staged files.
git stash show shows the changes recorded in the latest stash
If you have untracked files that you want to include in the stash, use the --include-untracked or -u option: git stash --include-untracked.
You can also provide a message to describe the stash using the --message or -m option: git stash save "My stash message".
To retrieve your changes and apply the stash, use git stash apply.
If you want to remove the applied stash from the stash list, use git stash drop or git stash pop.
To see a list of stashes, use git stash list. It will display the stash index, description, and the branch where the stash was created.
Task 4 :
Create a new branch and make some changes to it:
Start by creating a new branch named "feature3" using git checkout -b feature3
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.
Task5:
# Switch to branch "development"
git checkout development
# Create a file version01.txt and add below lines.
# "This is the bug fix in development branch
# Line2>> After bug fixing, this is the new feature with minor alteration"
# Commit the changes with message "Added feature2.1 in development branch
git add version01.txt
git commit -m "Added feature2.1 in development branch"
# In the version01.txt file, add: Line3>> This is the advancement of the previous feature
git add version01.txt
git commit -m "Added feature2.2 in development branch"
# In the version01.txt file, add: Line4>> Feature 2 is completed and ready for release
git add version01.txt
git commit -m "Feature 2 completed"
# Switch to the Production branch
git checkout production
# Use rebase to apply the commits from the development branch to the Production branch
git rebase development
Task6:
# Switch to the Production branch
git checkout production
# Cherry-pick the commit "Added feature2.2 in development branch"
git cherry-pick <commitID of the commit “Added feature2.2 in development branch”>
# In the version01.txt file, add: Added few more changes to make it more optimized
git add version01.txt
git commit -m "Optimized the feature"
Thank you, Hope you find it helpful, please give it a like 👍, share it with your friends, share your thoughts, and give me some valuable feedback.😇
Happy Learning.
Subscribe to my newsletter
Read articles from Nikhil Yadav directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Nikhil Yadav
Nikhil Yadav
I am a highly motivated and enthusiastic individual completed B.Tech from Savitribai Phule University, Pune . With a strong interest in DevOps and Cloud technologies, I am eager to kick-start my career in this domain. Although I do not have much professional experience, I possess a willingness to learn, excellent problem-solving skills, and a passion for technology. I am committed to contributing my best to any team I work with.