Table of contents
This is the Day 10 of #90DaysOfDevOps challenge. In the previous blog, we discussed Git and GitHub's important commands to get started with Git and with the continuation of Part 1 we came up with more deep-dive concepts and commands of Git.
In today's blog we'll discuss :
Git Branching
Git Revert and Reset
Git Rebase and Merge
And completing the tasks of Creating some branches in the Original Repo.
Git Branching:
A team of developers had written the code and pushed it to the GitHub repository and deployed it to the production servers. Now if some developers want to add some new features, and fix the bugs without disturbing the source code then you have to create new branches for adding features and new branch for bugs. By this, we can separate files, and folders into new branches.
The branching function is what makes it powerful for Git. After finalizing all the works from different branches then all developers will gonna merge to the source code i.e in the main branch.
Branching Name Strategies Examples:
feature/feature-name
bugs/description
Remember when you create a new branch and make new commits HEAD will point to your new branch.
By executing git branch
we can see how many branches you have created and also tells which branch you are in, current branch has (*) indicating with on that branch name.
You can see here, we are in the main branch with a * sign.
If we execute git log
you can see the HEAD pointer is pointing to the main branch with its commits Author, and date on that branch.
One thing also I want to discuss commit ID is that Git identifies all the commits by SHA-1 hash ID. (839a....)
Now, how to create a new branch and execute two commands. You can create a branch with two commands:
git branch dev
By this command, you'll create the branch dev but in this command, the HEAD pointer will not switch automatically to the newly created branch. It remains on that main branch.
Now the second command is the same command but we execute with checkout to automatically switched to the newly created branch and -b for the branch name.
git checkout -b <branch-name>
Now you can see this command has already switched to the dev branch. You can move to the main branch by executing the command
git checkout main
To delete a branch the command is
git branch --delete dev
Git Revert and Reset :
The git revert command is used to undo the commit and change it and make a new commit. It does not delete any data in this process it will create new changes in the commits.
Before this, we will execute git log
to see all the history of our commits.
Now, I want to revert the latest commit (839a...) where HEAD is pointing to the main branch.
git revert HEAD --no-edit
This command will revert means undoing the changes in history. As the HEAD pointer is on the latest commit so I typed HEAD with --no-edit (I don't want to do edit the message). You can see in history Revert is written.
git log --oneline
This command is the same as git log
seeing history but in a shorter form.
Suppose I want to revert the second commit how would I do?
git revert HEAD~2 --edit
Remember one thing where the HEAD is pointing to the commit then the commit is the latest commit and his lower commits are 2nd,3rd, and so on.
So in this command, the second commit is reverted where HEAD is pointing to the second commit (82f1..) with an edit option. --edit option will open the editor and make the changes in a file.
You can see HEAD is pointing to the second reverted commit where I changed the "README.md" file to "REAME".
Git Reset:
Git Reset is used to unstage the file and bring back changes to the working directory. This is the most complex and powerful command so be careful while using it. It can also be used to remove the commit.
There are 3 different ways for git reset
to keep the changes.
git reset --soft HEAD~1
- This command will remove the commit and put the commit in a staging area.git reset --mixed HEAD~1
- This command will remove the commit as well as brings the commit into unstaging area and moves it to the working directory.git reset --hard HEAD~1
- This is the destructive command because after this you cannot be able to get back as it removes the commit as well as changes from the working directory.
Git Merge:
Previously we created the dev branch and suppose I added new features to the dev branch. Now, I want to add the created features to the main branch. So how would I do this? Here comes the git merge function where we merge all works to the main branch without deleting anything and without making any changes in history.
Now, I am going to add the file version01.txt and pushed it to GitHub.
touch version01.txt
See we are in the dev branch and I created the file by touch command.
Now, execute this command to bring the file from the working directory to the staging area.
git add .
Now I am committing the file with the message of "Added New Feature".
git commit -m "Added New Feature"
Now push to GitHub but now you have to tell the git which upstream you want to push means which branch you want to push.
git push --set-upstream origin dev
Now if you refresh the GitHub Repo page you can see the dev branch and under that branch, the Version01.txt file has been added.
Now I am Adding new commits in dev
branch after adding below-mentioned content in 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 the message “ Added feature2 in development branch”
2nd line>> This is gadbad code
Commit this with the message “Added feature3 in development branch"
3rd line>> This feature will gadbad everything from now.
Commit with message “ Added feature4 in development branch
For example, I had done some mistakes in adding the content in Feature 4 dev so I want to change the content to “This is the bug fix in the development branch” and I am giving the message "Feature 4 contents has been changed". I am using git reset --soft HEAD~1
the command to bring the file into the staging area. After this execute git add .
then commit with the given message git commit -m "Feature 4 contents has been changed"
and then push to GitHub.
Now I want to merge the dev branch contents to the main branch. So first, let's check the contents of the main branch.
To merge dev into main we need to come into main branch and then execute
git merge dev
Git Rebase:
Git rebase is totally different from git merge as git merge is working on a three-way merge but git rebase is in a linear format. Take the example of the above dev and the main branch you are merging the dev branch into the main branch so by using git rebase the dev branch has been deleted and all its commits are gonna replaced by the main branch commits. The rebased branch no longer exists as the git history will also be rewritten.
That's why rebase command is destructive.
So, this is the Day 10 of #90DaysOfDevOps challenge. Don't worry very soon I'll make a blog on all Git commands and make a cheat sheet too.
Follow me for more Linux and DevOps blogs. Connect me on Twitter too.
THANK YOU :)