DEV Community

Cover image for 7 tips for improving your productivity with Git
Daniel Genezini
Daniel Genezini

Posted on • Originally published at blog.genezini.com on

7 tips for improving your productivity with Git

Introduction

Git is the most popular source control system with an incredible 93.87% of adoption by developers (according to StackOverflow's 2022 Survey). It's a really powerful system with lots of hidden features not known by most of us developers.

In this post, I'll show some of these features that will make your work easier when using Git.

1 - Remove remote-deleted branches on fetch

Excerpt from Git documentation:

Git has a default disposition of keeping data unless it’s explicitly thrown away; this extends to holding onto local references to branches on remotes that have themselves deleted those branches.

If left to accumulate, these stale references might make performance worse on big and busy repos that have a lot of branch churn, and e.g. make the output of commands like git branch -a --contains needlessly verbose, as well as impacting anything else that’ll work with the complete set of known references.

To remove local branches that have no remote-tracking references while fetching all branches, instead of using the --all parameter, use the --prune parameter:

git fetch --prune
Enter fullscreen mode Exit fullscreen mode

git fetch --prune Documentation

2 - Merge/Rebase without checking out the target branch

When making a merge or a rebase, it's common to see people do the following:

git checkout branch-to-merge-from
git pull
git checkout my-working-branch
git merge branch-to-merge-from
Enter fullscreen mode Exit fullscreen mode

Git branches are just references to a commit. When we make a fetch, the remote branches are created locally with a origin/ prefix, so there is no need to checkout the branch-to-merge-from before merging.

ℹ️ The remote branch prefix depends on the remote name, but the default is origin.

The following works without having to checkout and pull in the local branch branch-to-merge-from:

git fetch --prune
git merge origin/branch-to-merge-from
Enter fullscreen mode Exit fullscreen mode

git Remote Branch Documentation

3 - Trigger the CI/CD pipeline with a blank commit

If you are working with a CI/CD pipeline (and you should be), sometimes you need to trigger the pipeline without making changes to the code. Instead of changing files adding empty lines at the end, creating unnecessary logs, we can create an empty commit:

git commit --allow-empty -m 'Empty commit'
git push
Enter fullscreen mode Exit fullscreen mode

git commit --allow-empty Documentation

4 - View a file in another branch

When working on a feature, we often need to check a file in another branch, for example, in the production branch (main). Some Git services offer a web interface that makes it easier to look for files in specific branches, but if you don't have this option, Git can show the file in the command line without having to switch branches, using the show command followed by the branch name and the path to the file.

git show main:src/Program.cs
Enter fullscreen mode Exit fullscreen mode

As with other Git commands, we can pass any commit or reference, instead of the branch name:

# Show the program.cs file in the previous commit
git show HEAD~1:src/Program.cs
Enter fullscreen mode Exit fullscreen mode

git show Documentation

5 - Checkout the previously used branch

To switch back to the previously checked-out branch, just pass - as the branch parameter:

git checkout develop
git checkout main
git checkout - #Checkout the develop branch
Enter fullscreen mode Exit fullscreen mode

6 - Searching in Git

If you want to search for a string in your repository, use the git grep command.

To look for the string in all commits, provide the list of commits to git grep using the git rev-list --all command as shown below:

# Search for the word "git" in all files of all commits
git rev-list --all | xargs git grep "git"
Enter fullscreen mode Exit fullscreen mode

Output of the git grep command

git grep Documentation

7 - Showing the commit log as a graph

Git can show the commit logs in the form of a graph in the command line. For this, use the --graph parameter of the git log command.

ℹ️ Pass the --oneline parameter to show the commit hash and commit message in one line, and make it easier to read the graph.

git log --graph --oneline
Enter fullscreen mode Exit fullscreen mode

Git graph output for the repository

Git can also show the commit history of a specific file:

git log --graph --oneline systemcontext.md
Enter fullscreen mode Exit fullscreen mode

Git graph output for a specific file

git log --graph Documentation

Extra - Git Aliases

To make use of these commands easier, we can create Git aliases. In this post I explain Git aliases and show some that I use daily.

Liked this post?

I post extra content in my personal blog. Click here to see.

Follow me

Oldest comments (9)

Collapse
 
cavo789 profile image
Christophe Avonture

Hello. I'm working a lot (really a lot) with CI and everytime I am updating my CI scripts, I was updating my codebase just to be able to run git add, git commit ad git push.

I was not aware of git commit --allow-empty. Nice ! Thanks for the tip.

Collapse
 
pbnj profile image
Peter Benjamin (they/them) • Edited

Overall, a nice list of productivity tips. Thank you for sharing.

One feedback:

If you are searching for a text in a commit source code (as opposed to the commit message itself), thengit rev-list --all | xargs git grep "<regex>" will return a lot of false-positive/noise. git log -p -G "<regex>" is a more succinct command and returns more accurate results.

For example, consider you are trying to find commits that changed my_func calls in a repo that contains the following 2 commits :

+ my_var = my_func(a, b, c);
- my_var = my_func(a, b);
Enter fullscreen mode Exit fullscreen mode
function my_func(var_a, var_b, var_c) {
  ...
}

+ function foo(var_1) {
+ ...
+ }
+ 
+ function bar(var_2) {
+ ...
+ }
Enter fullscreen mode Exit fullscreen mode

If you run git rev-list --all | xargs git grep "my_func", you will get both commits. In a sufficiently large repo, you will have to continue to sift through a lot of results to find what you are looking for.

If you run git log -p -G my_func, you will get the first diff only which is what you are looking for. This approach also allows you to be more fine-grained in your search in a large repo by searching a specific directory or file, like git log -p -G my_func some/dir.

See git log --help for more details.

Bonus/Related

If you're searching for text in commit messages: git log --grep <regex>

Collapse
 
schmitzel76 profile image
Patrick Schmitz • Edited

If you set the fetch.prune config to true, the--prune``will be implied on every fetch, so you don't have to specify it every time. Just set it using

git config --global fetch.prune true

Collapse
 
chema profile image
José María CL

So niice! I really appreciate you've added the documentation reference. I've learned a lot. Thanks!

Collapse
 
dgenezini profile image
Daniel Genezini

I'm glad I helped. Thank you!

Collapse
 
stefaniefluin profile image
Stefanie Fluin

These are amazing! Can't wait to go try them, especially the git checkout - shortcut.

Collapse
 
foolhardy21 profile image
Vinay

awesome. thanks!

Collapse
 
vishnumeera profile image
VishnuSankar

thanks for --allow-empty

Collapse
 
teddyboirin profile image
Teddy

Nice tips !