To work with Git and GitHub, you must know how to create branches, merge and rebase them and how revert and reset commits.
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 separate area of your repository.
Git Revert and Reset
Two commonly used tools that git users will encounter are 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 reset
- Can be done on a branch or a file. It alters the commit history and therefore, is used on private repos. Is for undoing uncommitted changes.
git revert
- Can be done on a branch. It doesn't alter the commit history and is used on public repos. Is a tool for undoing committed changes.
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. The commit history is altered.
Git Merge
Git merge is a command that allows developers to merge Git branches while the logs of commits on branches remain intact. The commit history is not altered and is just added.
Task 1: Demonstrate the concept of git revert or git reset depending on the use case.
Add a text file called version01.txt inside the Devops/Git/ with “This is the first feature of our application” written inside. This should be in a branch coming from the main
, switch to dev
branch ( Make sure your commit message will reflect as "Added new feature"). version01.txt should reflect at the local repo first followed by the remote repo for review.
- Created and switched to a new branch "dev" -
- Created a new file -
- Commit this change -
And now, we will push all the changes to remote repository.
Add a new commit in dev
branch after adding the 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 the development branch
Commit this with the message “ Added feature2 in development branch”
2nd line>> This is gadbad code
Commit this with the message “ Added feature3 in the development branch
3rd line>> This feature will gadbad everything from now.
Commit with the message "Added feature4 in the development branch"
Restore the file to a previous version where the content should be “This is the bug fix in the development branch”. Using
git revert commit_id
will reverse the change and delete the commit history -
Task 2: Demonstrate the concept of branches with 2 or more branches using git merge and rebase.
Add some changes to
dev
branch and merge that branch inmain
After making the required changes in
dev
branch, checkout to themain
branch before merging -
Now, we can see that the
main
branch has all the folders and files from thedev
branch. Also, it should be noticed that irrespective of the commits that we do in thedev
branch, only the last commit will be visible in themain
branch.As a practice, try git rebase too, and see what difference you get.
Here, I added a newfile.txt, committed it, made another change and again committed. So now, we have 2 commits.
To rebase, we need to checkout to the
main
branch and then dogit rebase branch_name.
Resolved one conflict and the rebase was successful.Here, we can see that both the commits that were made in the
prod
branch are visible in the log of themain
branch. It seems as if I had made the commits in themain
branch only.
Today, we saw some advanced git commands like checkout, merge, rebase, reset and revert.\
Thanks for reading!
#devops#90DaysOfDevops#TrainWithShubham
Let's connect on Linkedin - linkedin.com/in/namya-khullar-7b5758200