Git Branching
Git branching is a feature of Git that allows you to create new branches off of the main branch (usually called "master") to isolate development work without affecting other branches in the repository. Each branch can have its own unique history of commits and changes, which can be merged back into the main branch when the work is complete. This makes it easier to collaborate on code changes without interfering with each other's work.
Creating a new branch is simple: you use the command git checkout -b <branchname>
to create a new branch and switch to it. You can then make changes to the code and commit them as usual. When you're ready to merge the changes back into the main branch, you use the git merge
command to combine the changes.
Git Revert and Reset
Git revert and reset are two commonly used tools in Git that allow you to remove or edit changes you've made in previous commits.
Git revert
creates a new commit that undoes the changes made in a previous commit, effectively "reverting" the code back to its previous state. This is useful when you want to undo a specific commit without affecting any other changes made since that commit.
Git reset
, on the other hand, allows you to reset the code to a previous commit and discard any changes made after that commit. This is more drastic than revert and should be used with caution, as it can permanently remove changes that you may want to keep.
Git Rebase and Merge
Git rebase and merge are two ways to combine changes from one branch into another.
Git rebase
integrates changes from one branch into another by moving the commits from the source branch and "replaying" them on top of the target branch. This creates a linear history and can make it easier to track the changes, but it can also result in conflicts that need to be resolved.
Git merge
, on the other hand, combines changes from one branch into another by creating a new commit that includes the changes from both branches. This creates a more complex history with multiple branches, but it can be easier to understand and manage.
What Is Git Merge?
Git merge is a command that allows developers to combine Git branches while the logs of commits on branches remain intact. The merge command takes the contents of a source branch and integrates them with a target branch. This creates a new commit that includes changes from both branches.
Merging can be done manually using the git merge
command or through a pull request on platforms like GitHub. When merging, Git will automatically try to combine changes from the source and target branches. However, conflicts can arise if the changes made in both branches overlap. In this case, Git will prompt you to resolve the conflicts manually.
Overall, Git merge is an essential tool for collaboration and version control in software development.
Task 1
To complete the given task, follow the steps given below:
- Create a new branch dev and switch to it:
git checkout -b dev
- Add a new file version01.txt with the given content:
echo "This is first feature of our application" > Devops/Git/version01.txt
- Commit the changes:
git add Devops/Git/version01.txt
git commit -m "Added new feature"
- Push the changes to the remote repository:
git push origin dev
- Add new commits to the dev branch:
echo "This is the bug fix in development branch" >> Devops/Git/version01.txt
git add Devops/Git/version01.txt
git commit -m "Added feature2 in development branch"
echo "This is gadbad code" >> Devops/Git/version01.txt
git add Devops/Git/version01.txt
git commit -m "Added feature3 in development branch"
echo "This feature will gadbad everything from now." >> Devops/Git/version01.txt
git add Devops/Git/version01.txt
git commit -m "Added feature4 in development branch"
- Revert the file to a previous version:
git revert HEAD~3
This will create a new commit that will undo the last three commits and bring the file content back to "This is the bug fix in development branch".
Task 2
Branching is a key concept in Git that allows developers to work on multiple features or fixes in parallel without interfering with each other's work. Here, we will demonstrate the concept of branching with two branches - master and feature.
- Create a new branch feature and switch to it:
git checkout -b feature
- Add some changes to the feature branch:
echo "This is a new feature" > file.txt
git add file.txt
git commit -m "Added new feature"
- Switch back to the master branch:
git checkout master
- Add some changes to the master branch:
echo "This is a bug fix" > file.txt
git add file.txt
git commit -m "Fixed a bug"
- Merge the feature branch into the master branch:
git merge feature
This will merge the changes made in the feature branch into the master branch.
- Alternatively, try using the git rebase command instead of merge:
git checkout feature
git rebase master
This will apply the changes made in the master branch to the feature branch before applying the changes made in the feature branch. This can result in a more linear commit history and fewer merge conflicts.
In conclusion, branching is a powerful feature in Git that allows developers to work on multiple features or fixes simultaneously without interfering with each other's work. It provides an effective way to isolate development work and experiment with new ideas. Git also provides various commands like merge, rebase, revert, and reset to manage and edit the code changes made in the repository.
Happy Learning ๐
Bhaktiben Kadiya