For many developers, truly harnessing the power of Git can be a daunting task. Git can seem intimidating when you first start using it. This is one of the reasons most developers limit themselves to using the standard push, pull, and merge commands.
However, over the years, Git has emerged as the standard tool for version control because of how powerful it is. So if you’re using Git on a regular basis, then you can vastly improve your workflow by learning some of these advanced features.
In this article, I’ll discuss ten advanced Git commands every developer should know.
1. Git Rebase
If you wanted to merge all your commits in a feature branch to the main
branch, what probably comes to mind is using the git merge
command. This will take all of the changes in the featured branch and stuff it into one merge commit and then put that merge commit into the main branch. After those changes are combined into that merge commit, it looks scattered, especially when you have a bunch of people working on the featured branch and pushing changes almost every time.
The git rebase command can solve that for us. It takes all of your commits from a featured branch and moves them on top of the main commit.
A great advantage is that it easily traces your commits inside the main branch.
>_git rebase <base>
You should use the git rebase
command to consolidate into a single branch when you have multiple private branches. And it will display the commit history in a linear form. In a linear history, it’s easy to see the progression of changes because they are all applied one after the other, without any branches or merge commits. This can make it easier to understand how the code evolved over time.
Additionally, a linear history can make it easier to track down bugs because you can more easily see when a particular change was introduced. It can also make it easier to revert changes since you can simply revert a single commit rather than dealing with the complexity of merging branches.
Searching a Git Repo
Have you ever wanted to search for a string in your repository history? Well, you can do that now with the grep
command, which helps you search for a specific string inside all the commits (the entire history of the project).
2. Git Grep
git grep
is a useful tool for quickly searching through the codebase of a Git repository to find specific information searches through the tracked files in the current branch of a Git repository and returns a list of lines that match the specified pattern. It can be used to search for code snippets, function calls, or specific strings of text.
To search through files in your working directory (current project state) you can make use git grep
:
>_git grep STRING
Here’s an example of searching for the word “blockchain” in a web3 repository:
>_git grep blockchain
Here’s the result:
3. Git Rev-List
You can also perform complex searches by adding the git rev-list. To understand how to do this, let’s take a brief look at the git rev-list
The git rev-list
is a Git command used to list the revisions in a Git repository. It provides a list of all the commit objects in the repository, including the commit hash, author, date, and message, sorted in reverse chronological order. The output of the git rev-list
command can be used to display the commit history, track the progression of a Git repository over time, or perform various operations on the commits.
>_git rev-list [OPTIONS] [<revision ranges>]
Here,OPTIONS
is a list of optional parameters that control the output of the git rev-list command, and <revision ranges>
is a list of revisions that you want to list.
For example, to list all the revisions in the repository, you can run:
>_git rev-list --all
To list the revisions reachable from the current branch, you can run:
>_git rev-list HEAD
To list the revisions between two branches, you can run:
>_git rev-list branch1..branch2
You can use the git rev-list
command in combination with other Git commands, such as xargs and git grep, to perform more complex searches.
>_
git rev-list –all | xargs git grep -F '<your string>'
Using xargs
is useful in this case because it takes the output of the git rev-list
command, which is a list of all revisions in the Git repository, and passes each revision as an argument to the git grep
command, so that the contents of each revision can be searched for the specified string.
You just have to replace the <your string>
with the string you want to search for:
>_
git rev-list --all | xargs git grep -F 'test'
This will search for the word ‘test’ and display the results.
Here’s the result:
4. Git Log
The git log
command is one of the most useful Git commands you should be familiar with. When you need to look at your commit history, you use the git log
command. The git log command displays the most recent commits as well as the current state of the HEAD.
>_git log
In the images below, you can see a log or history of the commits made so far.
Notice that each commit includes a unique sha-id
created by the SHA algorithm. It also includes the date
, time
, author
, and other information.
You can scroll through your log history by pressing the k
key to move up and the j
key to move down. Press q
to exit.
You can also view your logs as a graph just by adding the --graph
option:
>_git log --graph
5. Git Diff
Comparing changes you made between two files or branches before committing or pushing is helpful as it allows you to review your changes and make sure they are ready to be shared with others. This can help you catch mistakes or unintended changes before they are committed to the repository, which can save time and reduce the risk of introducing problems.
Thankfully, with git diff, you can do that. It lets you see what has changed between two branches, commits, and files.
Git Diff to Compare Files
To check the working directory against the local repository:
>_git diff
This will show the differences between the file you are currently working on and the version in the last commit. The output will show a list of changes, with added or modified lines highlighted in green.
Git Diff to Compare Branches
To see the differences between two branches:
>_git diff branch1 -- branch2
This will show the differences between the two specified branches. The output will show a list of changes for each file that has differences between the two branches, with added or modified lines highlighted in green.
Git Diff to Compare Commits
To see the differences between two specific commits:
>_git diff commit1 commit2
This will show the differences between commit1
and commit2
. The output will show a list of changes for each file that has differences between the two commits, with lines that were added or modified highlighted in green.
Conclusion
Git is a very powerful collaboration tool. If all you ever learn is push, pull, and merge, you can still go far. But mastering Git is worth the effort. It can increase your productivity and improve the quality of your collaborations with other developers.
The commands covered in this tutorial are just the start. There are many more. You might want to check out Git aliases and git bisect, and other interesting commands.