Introduction
The article explains a few less common git commands, which are extremely useful. There are many rare git commands, in this article, we will see 5 of them, and we will keep exploring more such git commands in upcoming articles.
Commands we look into
- git commit -amend
- git grep
- git cherry-pick
- squash
- git log
Let’s explore them
# git commit -amend
The git commit ‘amend’ option is used to edit the previous commit message (most recent), sometimes we make some spelling mistakes or want to edit the previous commit message for any reason, amend option should be used.
In my sample repo, I have added and committed a file with the message “Commit” which doesn’t clarify what is this commit is all about
git commit -m "Commit"
[main 709c3b9] Commit
1 file changed, 4 insertions(+)
Let’s change the message to more meaningful message.
git commit --amend -m "Modified README.md file for better description"
[main 6423ed4] Modified README.md file for better description
Date: Tue Dec 28 12:52:51 2021 -0600
1 file changed, 4 insertions(+)
Once the git commit amend is executed, we can push it to the repo using git push.
# git grep
Very useful command, It helps us in searching for a text or phrase in the entire codebase from the command line, if the search is successful then the lines containing the text are displayed along with the filename, if the text is not found then nothing is displayed.
git grep "Exploring"
README.md: Exploring few git commands
Let’s search for a Text which does not exist
git grep "SCALA"
Nothing is displayed.
# git cherry-pick
The purpose of ‘cherry-pick’ command is to choose a commit from one branch and apply it to another branch.
Consider we have 2 branches, ‘main’ and ‘demo’. We are at the ‘main’ branch, we have some commits in the ‘main’ branch which we need to push to the ‘demo’ branch. Cherry-picking is a three-step process
Step1: Move to ‘demo’ branch, branch where we need to apply the change
git checkout ‘demo’
Step2: cherry-pick the commit id from the 'main' branch.
git cherry-pick 15cd2879d506bdc0256f268ce1c8f132c2936c09
Step3: Push
git push
Our cherry-picked commit is now in the ‘demo’ branch.
# git squash.
Squash is also a very useful option; it is used to combine multiple commits into one single commit.
When we work on a specific feature branch, then we may have several sequential commits indicating the steps taken for building a particular feature, then during PR merge the entire commit history should be squashed to one because it simplifies the project’s Git tree, also when we create the PR for the code review and we squash our last ‘n’ commits, it helps code reviewer as well.
Let’s go to our ‘demo’ branch, in the ‘demo’ branch I have created 3 commits "Commit 1", "Commit 2", "Commit 3"
Let’s squash all these 3 commits to one.
git rebase -i HEAD~3
This command tells git that we need to act on the last 3 commits from the Head.
The result of the execution of the above command is the image below, the top 3 lines are the 3 commits, and below are the bunch of options.
We can modify Line no 2 and 3 and instead of the pick we modify with ‘squash’, this indicates git to combine the last 2 commits to one
On saving the file, we asked to modify the commit message by opening another terminal window. Here we need to comment out the ‘commit messages’ we don’t need and that’s it. Once this is done, a git push is required, and all our long commit history is combined into one.
# git log
Log command is used to retrieve the list of the entire commit history. Without using any option with the git log command, the entire commit history is retrieved, so it’s better to use options
git log -n 2
The command will retrieve the recent 2 commit history along with the commit message
git log –-oneline
--oneline option retrieves a list of commit IDs along with the commit messages.
There are many more such useful options with the log command please explore as it’s difficult to accommodate all of them in the article.
The next article covers more such rare commands.