Day 13 of My 90-Day DevOps Challenge: Advance Git & GitHub
Git Branching
Branches are a key concept in Git that let you work on development tasks without affecting other parts of your repository. Each repository has one default branch and can have many other branches. You can merge a branch into another using a pull request. Branches allow you to develop features, fix bugs, or safely try out new ideas in a separate area of your repository. ๐
Git Revert and Reset
Git reset and git revert are two commonly used commands that let you remove or modify changes you've made in previous commits. Both commands are useful in different situations. ๐๐ ๏ธ
Git Rebase and Merge :
What Is Git Rebase?
Git rebase is a command that lets users integrate changes from one branch to another, modifying the commit history once the action is complete. Git rebase helps maintain a clean project history. ๐งน
What Is Git Merge?
Git merge is a command that lets developers combine Git branches while keeping the commit logs on the branches unchanged. Although merging and rebasing perform similar tasks, they handle commit logs differently.
Task 1: Feature Development with Branches
1. Create a Branch and Add a Feature
Navigate to your project directory:
bashCopy codecd /path/to/your/repo
Create and switch to a new branch named
dev
:bashCopy codegit checkout -b dev
Create the
version01.txt
file inside theDevops/Git/
directory:bashCopy codemkdir -p Devops/Git echo "This is the first feature of our application" > Devops/Git/version01.txt
Add and commit the changes:
bashCopy codegit add Devops/Git/version01.txt git commit -m "Added new feature"
2. Push Changes to GitHub
Push the dev
branch to your remote repository:
bashCopy codegit push origin dev
3. Add More Features with Separate Commits
Append new content and commit after each change:
bashCopy codeecho "This is the bug fix in development branch" >> Devops/Git/version01.txt git commit -am "Added feature2 in development branch" echo "This is gadbad code" >> Devops/Git/version01.txt git commit -am "Added feature3 in development branch" echo "This feature will gadbad everything from now" >> Devops/Git/version01.txt git commit -am "Added feature4 in development branch"
4. Restore the File to a Previous Version
Use
git log
to check the commit history:bashCopy codegit log --oneline
To revert the last two commits and retain the first fix:
bashCopy codegit revert HEAD~2
This creates a new commit that removes the last two changes but keeps them in history.
Task 2: Working with Branches
1. Demonstrate Branches
Create multiple branches:
bashCopy codegit checkout -b feature-branch-1 git checkout master git checkout -b feature-branch-2
To list all branches:
bashCopy codegit branch
Capture screenshots of the branch structure in your GitHub repository or using:
bashCopy codegit log --oneline --graph --all
2. Merge Changes into Master
Switch to the
master
branch and mergedev
:bashCopy codegit checkout master git merge dev
3. Practice Rebase
Switch to the
dev
branch and rebase ontomaster
:bashCopy codegit checkout dev git rebase master
If conflicts occur, Git will prompt you to resolve them manually.
This task will help solidify your Git workflow, making it easier to collaborate on larger projects in DevOps. Let me know if you need clarifications! ๐