πΏππ Git Branching |
πβͺπ Git Revert and Reset |
ππ Git Rebase and Merge |
π Task 1: π οΈ |
π Task 2: π |
πβ¨Conclusion! ππ |
Git BranchingπΏππ
Git Branching involves creating isolated spaces for developing specific features without affecting the main codebase. Changes are integrated back into the main branch through merging, conflicts are addressed when necessary, obsolete branches are deleted, and visualization tools help understand the branching structure. πΏπ
Git Revert and Reset
Git Revert π
Purpose: Undo a specific commit by creating a new commit that undoes the changes.
Command:
git revert <commit-hash>
Git Reset π
Purpose: Move the branch pointer to a specific commit, discarding changes.
Commands:
git reset --soft <commit-hash>
git reset --mixed <commit-hash>
git reset --hard <commit-hash>
Git Revert undoes changes with a new commit, while Git Reset repositions the branch pointer, with options to preserve or discard changes. ππ
Git Rebase
π Git Rebase : Reorganizes commit history by incorporating changes from one branch into another. It creates a cleaner, linear history by placing the local changes on top of the remote branch.
Command: git rebase <branch-name>
Purpose: Streamlines the commit history for a more cohesive and understandable narrative. ππ
π Git Merge
π Git Merge: Integrates changes from one branch into another, creating a new commit that combines the histories of both branches.
Command: git merge <branch-name>
Purpose: Unites divergent lines of development, bringing changes together in a collaborative and organized manner. ππ
π Task 1: π οΈ
#make a directory
mkdir Devops
#change to devops
cd Devops
#for git dir
mkdir Git
# Navigate to the Git/ directory
cd Git/
# Create a new branch named 'dev' and switch to it
git checkout -b dev
# Create a text file called version01.txt and add the specified content
echo "This is the first feature of our application" > version01.txt
# Stage the changes
git add version01.txt
# Commit the changes with the specified commit message
git commit -m "Added new feature"
Add new commit in dev
branch after adding below mentioned content in Devops/Git/version01.txt
# Navigate to the Devops/Git/ directory
cd Devops/Git/
# Switch to the 'dev' branch
git checkout dev
# Open version01.txt and add the specified content
#insted of echo we can use the vim version.txt
echo "This is the bug fix in development branch" >> version01.txt
# Stage the changes
git add version01.txt
# Commit the changes with the specified commit message
git commit -m "Added feature2 in development branch"
# Open version01.txt and add the specified content
echo "This is gadbad code" >> version01.txt
# Stage the changes
git add version01.txt
# Commit the changes with the specified commit message
git commit -m "Added feature3 in development branch"
# Open version01.txt and add the specified content
echo "This feature will gadbad everything from now." >> version01.txt
# Stage the changes
git add version01.txt
# Commit the changes with the specified commit message
git commit -m "Added feature4 in development branch"
# Push the changes to the remote repository
git push origin dev
These commands add new commits with the specified content to the 'dev' branch in the local repository and then push those changes to the remote repository. The changes will be reflected in the remote repository for review.
π Task 2: π
The concept of branches with 2 or more branches
Adding some changes to dev
branch and merge that branch in master
Create a New Repository:
# Create a new directory for your repository mkdir git-demo # Navigate to the newly created directory cd git-demo # Initialize a new Git repository git init
Create and Switch to the 'dev' Branch:
# Create and switch to the 'dev' branch git checkout -b dev
Make Changes in 'dev' Branch:
# Open a text editor and add some content to a file (e.g., file.txt) echo "Changes in dev branch" > file.txt # Stage and commit the changes git add file.txt git commit -m "Added changes in dev branch"
Switch to 'master' Branch:
code# Switch to the 'master' branch git checkout master
Merge 'dev' Branch into 'master':
code# Merge 'dev' into 'master' git merge dev
Create a New Branch and Make Changes:
# Create and switch to a new branch (e.g., 'feature-branch') git checkout -b feature-branch # Make changes in the new branch echo "Changes in feature branch" > feature.txt git add feature.txt git commit -m "Added changes in feature branch"
Rebase 'feature-branch' onto 'master':
# Switch to 'feature-branch' git checkout feature-branch # Rebase 'feature-branch' onto 'master' git rebase master
Resolve any conflicts if prompted.
# Continue with the rebase git rebase --continue
The rebase integrates the changes from 'master' into 'feature-branch,' providing a linear commit history.
πβ¨Conclusion! ππ
Mastering Git and GitHub is crucial for DevOps engineers. Git branching, hooks, and advanced merge strategies enhance collaboration. Practical tasks like creating branches and leveraging Git rebase offer hands-on experience. Integrating these practices elevates development efficiency. Happy learning! ππ