Git Branching
Branches are a core concept in Git that allow you to isolate development work without affecting other parts of your repository. Each repository has one default branch, typically called main
or master
, and can have multiple other branches. Branches let you develop features, fix bugs, or safely experiment with new ideas in a contained area of your repository. You can merge a branch into another branch using a pull request, which allows for code review and integration testing before the changes are incorporated into the main codebase.
Git Revert and Reset
Git reset and git revert are two commonly used commands that allow you to remove or edit changes you’ve made in the code in previous commits. Both commands can be very useful in different scenarios.
Git reset: This command is used to undo changes by moving the HEAD and the current branch pointer to a specified commit. It can modify the staging area and working directory, making it a powerful but potentially dangerous command if not used carefully. There are three main options:
--soft
,--mixed
, and--hard
.--soft
: Moves the HEAD to the specified commit, but leaves the index and working directory unchanged.--mixed
(default): Moves the HEAD to the specified commit and resets the index, but leaves the working directory unchanged.--hard
: Moves the HEAD to the specified commit and resets both the index and working directory.
Git revert: This command creates a new commit that undoes the changes made by a previous commit. It is a safer way to undo changes, especially in a shared repository, because it does not alter the commit history.
Git Rebase and Merge
Git rebase and git merge are two commands that integrate changes from one branch into another, but they handle the commit history differently.
Git rebase: This command allows users to move or combine a sequence of commits to a new base commit. Rebasing modifies the commit history by creating new commits for each commit in the original branch, starting from the new base commit. This results in a linear, cleaner project history, but it can be dangerous if not used correctly, as it rewrites commit history.
Example:
git checkout feature-branch git rebase main
Git merge: This command integrates changes from one branch into another by creating a new commit, called a merge commit, which preserves the history of both branches. The commit logs on branches remain intact, providing a complete history of how the codebase has evolved.
Example:
git checkout main git merge feature-branch
Task 1: Feature Development with Branches
Create a Branch and Add a Feature:
Add a text file called
version01.txt
inside theDevops/Git/
directory with “This is the first feature of our application” written inside.Create a new branch from
main
git checkout -b dev
- Commit your changes with a message reflecting the added feature.
git add version01.txt
git commit -m "Adding new feature"
Push Changes to GitHub:
- Push your local commits to the repository on GitHub
git push origin dev
Add More Features with Separate Commits:
Update
version01.txt
with the following lines, committing after each change:- 1st line:
This is the bug fix in development branch
- 1st line:
echo "This is the bug fix in development branch" >> version01.txt
git commit -am "Added feature2 in development branch"
2nd line: This is gadbad code
echo "This is gadbad code" >> version01.txt
git commit -am "Added feature3 in development branch"
- 3rd line:
This feature will gadbad everything from now
echo "This feature will gadbad everything from now" >> version01.txt
git commit -am "Added feature4 in development branch"
Restore the File to a Previous Version:
- Revert or reset the file to where the content should be “This is the bug fix in development branch”
git revert HEAD~2
Task 2: Working with Branches
Demonstrate Branches:
- Create 2 or more branches and take screenshots to show the branch structure.
Merge Changes into Master:
Make some changes to the
dev
branch and merge it intomaster
.git checkout master git merge dev