Git Branching
Git branching allows developers to create multiple branches of their codebase and work on them independently of each other. Each branch represents a separate line of development and can contain different sets of changes and versions of files.
The main branch in Git is typically called the
master
branch, and it is where the main development of a project takes place. However, developers can create new branches of themaster
branch or other existing branches, such as for new features, bug fixes, or experiments.When you create a new branch, it initially contains the same code as the branch it was created from. You can then make changes to the new branch without affecting the original branch. Once you have completed your changes on the new branch, you can merge the changes back into the original branch to combine the changes.
Git Revert and Reset
git revert
andgit reset
are two Git commands that allow you to undo changes made to your Git repository. However, they work in different ways and should be used depending on the situation.git revert
does not delete any data instead it creates, a new commit with the included files reverted to their previous state so your version control history moves forward while the state of the file moves backward.on the other hand,
git reset
is used to undo local changes to the state of a git repo.
Git Rebase and Merge
What Is Git Rebase?
Git rebase is used to apply changes from one branch to another branch.
When you run
git rebase <branch>
, Git takes all the changes that were made on the current branch since it diverged from the specified branch and applies them on top of the specified branch. This makes it look as though the current branch was created from the specified branch and all the changes on the current branch was made on top of it.What Is Git Merge?
Git Merge allows you to combine changes from one branch into another branch. When you merge branches, Git creates a new commit that incorporates the changes from both branches into a single branch.
Task 1:
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 master
, [hint try git checkout -b dev
], switch 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 the local repo first followed by the Remote repo for review. [Hint use your knowledge of Git push and git pull commands here]
- To move changes from the local repo to the remote, we push. To bring them to the local from the remote, we pull.
COPY
\-- A "pull request" is you requesting the target repository to please grab your changes. A "push request" would be the target repository requesting you to push your changes
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” [Hint use git revert or reset according to your knowledge]
TO get commit-ID git log
-- > here are three commands with similar names: git reset, git restore and git revert. git-revert is about making a new commit that reverts the changes made by other commits. git-restore is about restoring files in the working tree from either the index or another commit. This command does not update your branch.
git revert should be used to undo changes on a public branch, and git reset should be reserved for undoing changes on a private branch. You can also think of git revert as a tool for undoing committed changes, while git reset HEAD is for undoing uncommitted changes
git revert <commit-id>
Task 2:
Demonstrate the concept of branches with 2 or more branches with a screenshot.
add some changes to
dev
the branch and merge that branch inmaster
as a practice try git rebase too, see what difference you get.
In Git, rebase is an alternative method to merge for integrating changes from one branch into another. The basic difference between merge and rebase is that merge creates a new commit that has both branches as its parent, while rebase takes the commits from one branch and replays them on top of another
Thanks for your time.
Please share your valuable feedback by liking 👍, sharing🤝 and commenting.
See you tomorrow, with another blog.
For more such content please follow me here on hashcode.
#day10 #90DaysOfDevops #git #github #DevOps