7 tips for improving your productivity with Git
Tags: git, github, productivity, webdev
Published: 2022-12-28 00:29:59

## Introduction

Git is the most popular source control system with an incredible 93.87% of adoption by developers (according to [StackOverflow's 2022 Survey](https://survey.stackoverflow.co/2022/#section-version-control-version-control-systems)). 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](https://git-scm.com/docs/git-fetch#_pruning):
> 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 <commit> 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:

```bash
git fetch --prune
```

[git fetch --prune Documentation](https://git-scm.com/docs/git-fetch#Documentation/git-fetch.txt---prune)

## 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:

```bash
git checkout branch-to-merge-from
git pull
git checkout my-working-branch
git merge branch-to-merge-from
```

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`:

```bash
git fetch --prune
git merge origin/branch-to-merge-from
```

[git Remote Branch Documentation](https://git-scm.com/book/en/v2/Git-Branching-Remote-Branches)

## 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: 

```bash
git commit --allow-empty -m 'Empty commit'
git push
```

[git commit --allow-empty Documentation](https://git-scm.com/docs/git-commit#Documentation/git-commit.txt---allow-empty)

## 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.

```bash
git show main:src/Program.cs
```

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

```bash
# Show the program.cs file in the previous commit
git show HEAD~1:src/Program.cs
```

[git show Documentation](https://git-scm.com/docs/git-show)

## 5 - Checkout the previously used branch

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

```bash
git checkout develop
git checkout main
git checkout - #Checkout the develop branch
```

## 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:

```bash
# Search for the word "git" in all files of all commits
git rev-list --all | xargs git grep "git"
```

![Output of the git grep command](https://blog.genezini.com/p/7-tips-for-improving-your-productivity-with-git/images/git-grep.png)

[git grep Documentation](https://git-scm.com/docs/git-grep)

## 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.

```bash
git log --graph --oneline
```

![Git graph output for the repository](https://blog.genezini.com/p/7-tips-for-improving-your-productivity-with-git/images/git-log-graph-1.png)

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

```bash
git log --graph --oneline systemcontext.md
```

![Git graph output for a specific file](https://blog.genezini.com/p/7-tips-for-improving-your-productivity-with-git/images/git-log-graph-2.png)

[git log --graph Documentation](https://git-scm.com/docs/git-log#Documentation/git-log.txt---graph)

## Extra - Git Aliases

To make use of these commands easier, we can create Git aliases. In [this post](https://blog.genezini.com/p/git-aliases-a-time-saving-secret-weapon-for-improved-workflow-and-productivity/) I explain Git aliases and show some that I use daily.

## Liked this post?

I post extra content in my personal blog. [Click here](https://blog.genezini.com/) to see.

## Follow me

- [On Feedly](https://feedly.com/i/subscription/feed/https://blog.genezini.com/index.xml)
- [RSS](https://blog.genezini.com/index.xml)
- [Linkedin](https://www.linkedin.com/in/danielgenezini/)
- [Twitter](https://twitter.com/GeneziniDaniel)