5 Git Tips To Level Up Your Workflow
Tags: webdev, programming, git, productivity
Published: 2021-07-29 10:39:46

No matter how experienced you are, Git will always find a way to surprise you. It is loaded with neat tricks that have the power to make your daily coding routine much smoother. 

In this article, you'll find 5 extra tips that will help you bring your Git skills to the next level (in case you missed it, take a look at our previous post, <a href="https://blog.git.live/top-5-git-tips-and-tricks" target="_blank" >Top 5 Git Tips & Tricks </a>).
<br>

<p class="tip">
<a href="#tip1" class="tip" name="tip1"> Tip #1 </a>
</p>
 
<h2> Visualise in the command </h2>

You have cloned a repository and to understand it better, you'd like to look at the commit history. Wouldn't it be nice to get a visual representation of all that has happened? 

```shell
  $ git log --all --oneline --graph
```

If you need to visualise the Git history, `git log --graph` can be more helpful than a simple `git log`.  By default, `git log` displays full commit hash, author, date and message, each on a new line, which sometimes makes it difficult to get a clear understanding of what has happened. There are many ways to customise it, but if you just want to have a compact overview of your commit history you should try the command above. 

```shell
--all # will include commits from all branches 
```
```shell
--oneline # will show information on one line
```
```shell
--graph # will draw a text-based graphical representation of the commit history
```


![git-log-zoom.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1627486874378/qQkY88PbS_.png)
<br>

<p class="tip">
<a href="#tip2" class="tip" name="tip2"> Tip #2 </a>
</p>
 
<h2> Take a peek at a file of another branch </h2>
 

You are working on a branch `feature-one` and you want to take a quick look at a file that lives in `feature-two` branch. Wouldn’t it be cool to do it without actually switching branch?

```shell
$ git show <branch_name>:<file_name>
```

With this command you can view the content of a file from another branch. After running it you will be able to view the content of the chosen file in your terminal. 
 
  --------
<sup>
💡 You can also view changes from other branches and even your teammates' uncommitted code, directly in GitLive and, if you need to, <a href="https://blog.git.live/gitlive-8-0-Cherry-picking-your-teammates-changes" target="_blank" >cherry pick</a> them to apply those changes to your local files.
******


<p class="tip">
<a href="#tip3" class="tip" name="tip3"> Tip #3 </a>
</p>
 
<h2> Find out who's to blame </h2>

Some changes were made to the file you've been working on and you are not entirely sure why they are there, ideally you would like to ask for help from the person who made the changes. 

```shell
$ git blame <FILE_NAME>
```

You can use this command to see who changed what in a specific file, line by line. 




![git-blame-zoom.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1627486887647/mM6SX_Ayk.png)

Depending on what you want to show, you can pass different flags. In case you want to check the specific part of the file you would use $ `git blame -L <range>` which restricts the output to the selected line range. 

Other useful flags: 

```shell
$ git blame -w  # ignores white space modifications
```

```shell
$ git blame -M  # ignores moving and copying text within the same file and shows the original author instead
```

```shell
$ git blame -C  # ignores moving and copying text into other files and shows the original author instead
```
&nbsp;

<p class="tip">
<a href="#tip4" class="tip" name="tip4"> Tip #4 </a>
</p>
 
<h2> Text search through time </h2>


Your code editor most likely allows you to easily search for text in your project, but often it comes in handy to search through previous revisions of the files as well. 


```shell
$ git grep STRING $(git rev-list --all)
``` 

Git Grep is a command that allows you to quickly search through your tracked files for a given string or regex. By default it will ignore untracked files, but you can add `--untracked` flag to the command to search through untracked files.


Let’s take a look at some useful flags:

```shell 
$ git grep STRING # searches through the files in your working directory (current state)
```

```shell
$ git grep STRING $(git rev-list --all) # searches through all the commits (entire history of the project)
```

```shell
$ git grep STRING HEAD~1 # search only in the commit prior to the current head
```

```shell
$ git grep STRING <commit1> <commit2> <commit3> # search only in the specific commits (list as many commits as you need)
```

```shell
$ git grep -ni STRING  # Case insensitive search. Will list the matches along with line numbers
```

```shell
$ git grep -c STRING  # Shows the number of lines that match, instead of listing all the results
```

```shell
$ git grep STRING *.js # search only the files with the specific extension (ignore matches from other files)
```

&nbsp;

<p class="tip">
<a href="#tip5" class="tip" name="tip5"> Tip #5</a>
</p>
 
<h2> Teamwork is key: credit a co-author of a commit </h2>


The feature you’ve been working on was really tough, and you know you could not have done it without Anna’s help. Her contribution was invaluable and it’s only fair to give her credit for that. 

```shell
Co-authored-by NAME <email>

$ git commit -m "Prepare release.
>
>
Co-authored-by: name1 <name1@example.com>
Co-authored-by: name2 <name2@example.com>"
```


This easy way of supporting multiple authors was introduced by GitHub in order to improve collaboration within the team and promote social coding. This syntax is supported by GitHub and GitLab and it’s a great way to credit both developers who worked on a commit. 

To add one or more co-authors to the commit, you need to know their name and email (if they decide to keep their email private you should ask them for their GitHub provided no-reply email address instead which can be found on the email settings page in GitHub). You add them to the message (each on a new line) before the closing quotation mark.

If you follow this syntax, you will see the co-authored commit on GitHub (it happens also if you select the Squash and merge option on a pull request). It can also be included in the profile contributions graph and the repository's statistics. 


![team-commit-2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1627574769228/a3EBJulHB.png)
 
Thanks for reading! Hope you enjoyed our selection of 5 Git Tips to Level Up Your Workflow. If you liked it, you might also want to check our article on <a href="https://blog.git.live/top-5-git-tips-and-tricks" target="_blank" >Top 5 Git Tips & Tricks</a>. Feel free to share your favorite hacks in the comments. Happy Git-ing!