Ask More From Git - Cool Tips

Introduction 

 
In Git, once you have a workflow setup for your team, Git becomes a handy and powerful tool, and with git tips, you can achieve more in less time.
 
This guide will explain advanced and handy git tips to boost your productivity in a concise time frame.
 
So let’s dive in…
 

Delete (Unwanted) Branches

 
You might have a few stale branches in your local repository. It can happen for many reasons.
 
One of the common reasons when you merge a branch from remote to master, it gets deleted from the remote (but not from your local repository)
 
It’s safe to be deleted, as you have all the history present in your master branch.
 
You can change your local repository settings to delete branches on each fetch/pull.
 
Here is the command that you use on git bash.
 
git config --global fetch.prune true
 
 
If you have fewer branches in your repository, then you can easily stay up to date with your repo changes.
 

Keep Your Repo Clean

 
In the software industry, source code is costly. It’s the responsibility of developers to keep their source code neat and clean.
 
Your repository is meant for the source code and not the unwanted files like dll, binary, temp files, etc. There is a straightforward way to manage that - use the .gitignore file.
 
In simple words, the gitignore file is a particular file in plain text. It contains a list or pattern for files/directories to ignore.
 
There are many advantages to using a gitignore file, mainly to keep your code repository clean and keep repo size under control. It's important to follow git best practices to keep your repo clean and organized.
 

Count Commits

 
There is a git command to count the number of commits in a branch. You might need it at some point to check how many commits you have done in the last week or the previous month.
 
Also, it’s a handy command to review to compare two branches with the number of commits
 
Here is the command to count commits
 
git rev-list --count <branch-name>
 
You can see the dev branch has only 34 commits in the screenshot below, and the master has only 32 commits.
 
Therefore, it's easy to compare and see that the dev has two extra commits.
 
 

Search like a Pro (in any Git branch)

 
You can search any string in your repository. Searching is a cool git tip.
 
And here, you can search in any branch within a repository.
 
git rev-list --all | xargs git grep -F '<Your search string>'
 
Here, you can see that I am searching for a stylesheet element.
 
git rev-list --all | xargs git grep -F 'font-size: 52px;'
 
 
And voila, I can see this particular line is present in two different files in my repo (within a fraction of a second).
 

Optimize Your Git Repo

 
As a software professional, you have already heard about the garbage collectors.
 
In simple words, a garbage collector helps to run your program smoothly and do memory management. Internally It cleans up the space used by unused or unreachable objects.
 
Similarly, In Git, there is a concept of garbage collection. It runs automatically, but of course, you can run in on-demand.
 
Here is the git command that you can use:
 
git gc --prune=now --aggressive
 
 
In case your team uses git pull / push commands heavily, you might need to run the git gc command.
 

Bonus - Git Guides

 
Git has many hidden gems, and one of the hidden gems is its git guides.
 
If you want to explore Git, you can simply use the following command to get more information.
 
git help -g
 
 

Conclusion

 
Git is a powerful tool, but it’s a software program, so it’s the responsibility of a developer to use it wisely.
 
With these Git tips and tricks, you can manage your git repo more effectively.


Similar Articles