GitHub/Git Tips and Tricks

GitHub/Git Tips and Tricks

5 mins read716 Views Comment
Updated on Mar 7, 2022 16:37 IST

Here are some top Git/GitHub tips and tricks that will help you to increase your productivity and efficiency as a developer.

2022_03_GitHubGit-Tips-and-Tricks.jpg

In this article, we are going to look into some key tips and tricks that every Github/Git user should know. These not only help the user to increase efficiency while working on Github but also can help personalize the Github experience.

Table of Contents

Recommended online courses

Best-suited Github courses for you

Learn Github with these high-rated online courses

Free
2 hours
Free
1 hours
name
GUVICertificate
499
3 hours
– / –
15 hours
3.52 K
11 hours
455
2 hours
389
11 hours
389
7 hours
429
8 hours
7.14 K
3 weeks

1. File finder in Github:

This is a very useful feature of Github. This helps us to locate a specific file in a Github repository.  For finding a particular file inside a Github repository, we can just press “t” on the keyboard and start typing the name of the file that you are looking for. Take a look at the below image for reference:

File finder in Github - Git Tips & Tricks

2. Linking Code Snippets in Github:

We can link a specific line(s) of code using the permalink functionality of Github. This functionality can be very useful during code reviews. A reviewer can share a specific part of the committed code for changes by linking them. To link a specific line(say, for instance, line 13 of a  script), we can just click on the line number which pops a dotted menu (…). Click on the dotted menu followed by the copy permalink. Take a look at the below image for reference:

Linking Code Snippets in Github

Now if you paste the copied link to your browser, it will automatically take you to the exact code snippet as shown below:

Linking Code Snippets in Github

3.  Autocorrection in Git:

Git has an auto-correct feature that can come in handy to avoid typos while executing git commands. As most developers at some point mistype the git commands, it has significantly reduced the chances of encountering such errors in the future.  

At this point, we have not enabled the auto-correct feature in the current system. Take a look at the below image for the response from git where we mistype the git log command to git lot. At this instance, git will throw an error and might suggest some similar commands.

Autocorrection in Git - GitHub Git Tips & Tricks

Now let’s enable the auto-correction functionality using the below command and again run git lot:

git config --global help.autocorrect 1

As you can see in the below image the command automatically called the git log command even if there was a typo:

GitHub Git Tips & Tricks

4. Searching in Git:

Git has this amazing feature where you can directly search a string that can be a file, a code snippet using the below command:

git grep -n <STRING TO BE SEARCHED>
 

Take a look at the below example where we use the above command to search for a string “test“:

git grep -n test
 

The above command will list all the occurrences of the string “test” in the current directory irrespective of the changes made in the repository:

Searching in Git - GitHub Git Tips & Tricks

5. Viewing File of Another Branch:

Let’s say you are on branch A and you need to check the contents of a specific file on another branch B. For this kind of scenario we can use the below command to check the contents of a specific file from another branch:

git show <branch name>:<file path>
 

In the below example we are in a brach called test, and we are looking into the contents of a file named App.js on the main branch using the below command:

git show main:src/App.js

Take a look at the below image for reference:

Viewing File of Another Branch:

6. Backing up Untracked Files:

If there rises a situation where you need to backup your untracked files in git (as they  generally get deleted after a commit), for future use, you can make use of the below git command:

git ls-files --others --exclude-standard -z |xargs -0 tar rvf ~/<FILE NAME>.zip
 

For instance, let’s back up our untracked files inside a zip folder named untracked-file-backup using the below command. At this point, we have an untracked file with the name test4.txt.

git ls-files --others --exclude-standard -z |xargs -0 tar rvf ~/untracked-file-backup.zip
 
Backing up Untracked Files:

The above command will create a backup of your untracked files.

7. Listing all branches Merged with Main:

In a collaborative project with multiple developers, there is a simple way to track which branches have been merged with the main branch. To lit all the branches that have been merged with the main use the below command:

git branch --merged main
 

Take a look at the below image for reference. Here we encounter that the test1 git branch  has been merged with the main branch:

Listing all branches Merged with Main

8. Track Changes Between Intervals:

Let’s suppose you want to track changes that are made to the git repository at a specific time. For this we can use the below command:

git whatchanged --since='<TIME INTERVAL>'

For example, let’s check what changed in our git repository in the last 2 weeks. We can use the below command:

git whatchanged --since='2 weeks ago'
 

The above command will list all the changes made since the last two weeks as shown below:

Track Changes Between Intervals

9. Tracking the Number of Commits:

Git also comes with this feature where you can track the exact number of commits made in a git repository across all branches. To get the count of the commits, which can be used for uses cases like tracking the progress of a project, use the below command:

git rev-list --all --count
 
 

The above command will output the number of commits across all branches as shown below:

Tracking the Number of Commits:

10. Getting Help:

As much as we like to believe in ourselves, everyone needs help at some point. Git also has a feature to help its user in various scenarios while using Git.  If you are stuck with a verb that you do not know what it does you can follow any of the following approaches to get more information about the verb:

1. git help <verb>
2. git <verb> --help
3. man git <verb>
 

For example, let’s take help to understand the function of the verb: status in git.

  • Using git help <verb>:
Getting Help:
  • Using git <verb> –help:
  • Using man git <verb>:

Hope you find this blog helpful for your Git and GitHub adventures. Still, the best way to learn these tips is to apply them in your own projects to increase your productivity.

Top Trending Tech Articles:
Career Opportunities after BTech Online Python Compiler What is Coding Queue Data Structure Top Programming Language Trending DevOps Tools Highest Paid IT Jobs Most In Demand IT Skills Networking Interview Questions Features of Java Basic Linux Commands Amazon Interview Questions

Recently completed any professional course/certification from the market? Tell us what liked or disliked in the course for more curated content.

Click here to submit its review with Shiksha Online.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio