Git Branching
Use a branch to isolate development work without affecting other branches in the repository. Each repository has one default branch, and can have multiple other branches. You can merge a branch into another branch using a pull request.
Branches allow you to develop features, fix bugs, or safely experiment with new ideas in a contained area of your repository.
Git Revert and Reset
Two commonly used tools that git users will encounter are those of git reset and git revert . The benefit of both of these commands is that you can use them to remove or edit changes you’ve made in the code in previous commits.
Git Rebase and Merge
What Is Git Rebase?
Git rebase is a command that lets users integrate changes from one branch to another, and the logs are modified once the action is complete. Git rebase was developed to overcome merging’s shortcomings, specifically regarding logs.
What Is Git Merge?
Git merge is a command that allows developers to merge Git branches while the logs of commits on branches remain intact.
The merge wording can be confusing because we have two methods of merging branches, and one of those ways is actually called “merge,” even though both procedures do essentially the same thing.
Refer to this article for a better understanding of Git Rebase and Merge Read here
Task 1:
Add a text file called version01.txt inside the Devops/Git/ with “This is first feature of our application” written inside. This should be in a branch coming from master
, [hint try git checkout -b dev
], swithch to dev
branch ( Make sure your commit message will reflect as "Added new feature"). [Hint use your knowledge of creating branches and Git commit command]
- version01.txt should reflect at local repo first followed by Remote repo for review. [Hint use your knowledge of Git push and git pull commands here]
Add new commit in dev
branch after adding below mentioned content in Devops/Git/version01.txt: While writing the file make sure you write these lines
1st line>> This is the bug fix in development branch
Commit this with message “ Added feature2 in development branch”
2nd line>> This is gadbad code
Commit this with message “ Added feature3 in development branch
3rd line>> This feature will gadbad everything from now.
Commit with message “ Added feature4 in development branch
Restore the file to a previous version where the content should be “This is the bug fix in development branch” [Hint use git revert or reset according to your knowledge]
Task 1
Gitbash in locally, creating directories devops/git/version01.txt file
Git init and committing from master branch
Pushing the commits to master branch of DevOps repository (remote github)
First commits have been pushed to a repository named devops, check the below image for reference
Creating a Dev branch from the master and
Now making changes to version01.txt file and making 2nd commit
$ vi devops/git/version01.txt
1st line>> This is the bug fix in development branch
$ git add .
$ git commit -m "Added feature2 in development branch"
$ git branch -M dev
$ git push -u origin dev
Now making changes to version01.txt file again and making 3rd commit
$ vi devops/git/version01.txt
2nd line>> This is gadbad code
$ git add .
$ git commit -m "Added feature3 in dev branch"
$ git push -u origin dev
Now making changes to version01.txt file again and making 4nd commit
$ vi devops/git/version01.txt
3rd line>> This feature will gadbad everything from now.
$ git add .
$ git commit -m "Added feature4 in dev branch"
Now we want to restore to the commit "This is bug fix in the development branch" using git revert or reset command and this is possible because we already know Git is a version control system that helps us to restore or revert to previous changes.
$ git log --oneline provides us the complete log history of our commits
$ git reset --hard df50691
Refer: Git revert / Git reset link
Use
git revert
when you want to undo changes without modifying existing commits, especially if changes have been shared with others.Use
git reset
when you want to discard commits and rewrite history, but be cautious when using it on commits that have been pushed to a shared repository.git revert
is safer because it creates a new commit to undo changes, leaving the commit history intact.git reset
is riskier as it modifies the commit history, potentially causing issues if the changes have been shared with others.
Task 2:
Demonstrate the concept of branches with 2 or more branches with screenshot.
add some changes to
dev
branch and merge that branch inmaster
as a practice try git rebase too, see what difference you get
Let's Create a new branch DEV make some changes and commit it
Now let's merge the changes in Dev to master.
Merge is a success and both the files in master and dev have the same information
Check the git logs of the merge
git merge --abort
Reset it back to the first commit(master)
Let's use rebase for master-dev
Git Merge:
Creates a new commit (merge commit) that combines changes from different branches.
Preserves the commit history of each branch, resulting in a more branch-like structure.
Git Rebase:
Rewrites the commit history by incorporating changes from one branch onto another.
Produces a linear commit history, making it look like the changes happened in a sequential order.
Use
git merge
when you want to maintain a clear history of different branches and collaborations.Use
git rebase
when you want a more linear and organized commit history, especially for feature branches.
Refer Git videos