Spectral now part of Check Point’s CloudGuard to provide the industry’s most comprehensive security platform from code to cloud Read now

Git and GitHub Secrets You Should Know

By Eyal Katz April 29, 2021

Git and GitHub are almost synonymous when it comes to code repositories and version control. However, the majority of developers under-utilize these two tools and just stick to the bare-bones basics.

Knowing how to commit, push and pull is nice. However, there are some Git and GitHub secrets and tricks you’re probably not using. We’ve collected a few of these you may not yet be familiar with.

xkcd git

Git features you should know

Git is the most widely used code version control system by developers today. It dates back to 2005, is widely supported by IDEs and various integrations. Most developers encounter git at the start of their careers and only few choose SVN or other code versioning systems.

However, beyond the usual commit, push and pull commands, there are a few other things that many developers do not use. In part, it’s because we often just fall back on what we already know how to use even in our sleep. Other excuses include not being familiar with the commands and not seeing the benefits of additional Git features. 

That’s about to change. Meet our top “secret” Git features to know.

Squashing all useless git changes

The truth is developers don’t always stick to one branch when working on a project. We jump between features, leaving a trail of unused branches behind us. It’s easy to boot up a new branch via git checkout  -b <branch-name> and tinker about in the code. But there’s a cost.

You then find yourself so caught up in the development process that you forget to branch out when you add a new feature. The next thing you know, instead of true version control with clean code and feature separation, you’ve just used git mainly as a code saving space.

So you end up with a bazillion commits that are actually quite useless. But you still want to keep your code. However, you want to simplify it by just having point A and point B of your code, rather than all the bits in between.

This is where git squash comes in handy.

git squash lets you combine multiple commits into one. You can do this via the following command:

git rebase -i HEAD~3

git rebase -i is a tool that lets you squash your commits into one based on where you are right now. HEAD~3 stands for the last 3 commits that you want to squash into one single commit. If you’ve got more than just 3 commits, change the number accordingly.

git add shorthand and patching

You know the drill -- before you can commit anything, you need to add all your files first via git add. Then you follow it up with a commit message.

Sometimes you just want to get things done quickly. There’s a shorthand version for this and it looks like the following:

git commit -am "your message here"

What if you don’t want to add all your file changes and only a portion of your file to the commit? This is where patch comes in.

git add -p or git add --patch is a way for you to selectively add parts of a file for a commit rather than the entire thing. Sometimes, you can cherry-pick code blocks and separate them into different commits for roll-back reasons.

Autocorrection in Git

We’ve all made our fair share of typos and attempted shorthand when trying to run a git command. Here’s an example:

 $ git stats
 git: ‘stats’ is not a git command. See ‘git --help’.
 ​
 The most similar command is
 status

To avoid this and other various typo mishaps, you can turn on git autocorrect in your git configuration through the following command:

$ git config --global help.autocorrect 1

So next time you type git stats, it will automatically autocorrect to git status.

git status

Making backups of untracked files in Git

Sometimes you don’t want to commit or merge your code but you still want a backup of the untracked files somewhere. Or perhaps you want to delete your files but still want a backup for whatever reason.

You can create a zip archive of your untracked files that you can bring up at a later date via git. Here is the command for it:

 git ls-files --others --exclude-standard -z |\
 xargs -0 tar rvf ~/backupfilename.zip

The above will create an archive and exclude all the files listed inside .gitignore.

GitHub ‘Hidden’ Features You Should Know About

Github is a platform that hosts git files (but not only). With over 190 million repositories and 28 million of them being public, it is the largest platform for hosting source code in the world. Not only that, but it is also mostly free to use.

If you’re a seasoned developer or developer team head, you already know there is more to it than just uploading your code into GitHub. GitHub is a platform with in-built features — many of which users are not aware of. Here are some that you might find beneficial.

Fuzzy file finder

The file finder feature was introduced back in 2011. Instead of trawling manually through the different files, you can just hit t on your keyboard to bring up the file finder feature.

git file finder

This feature can come in handy when you don’t want to download the repository in full onto your local environment, or just want a quick look or a code snippet from a repo.

Linking Code Snippets

You found a piece of code that you want to show your fellow developer, or just add it to an article you’re working on. However, copying and pasting can cause formatting issues and make it hard for the person to read.

GitHub has a feature that lets you easily link and share code snippets from a repo through just a few clicks.

First, select the lines you want to share, open the inline toolbar and click Copy permalink. This will give you a permalink that you can use anywhere to render the code snippet.

Markdown Mastery

Documentation on GitHub repositories is written in markdown. Here is a quick cheat sheet guide for most of you markdown needs:

MarkdownDescription
Pair of backticks for inline code
“`Three backticks at the beginning and end for block code. If you have followed directly by the code type, the coloring for that sample will be modified for the language. For example, ““css  will modify the displayed block for css optimized color scheme.
# – ####Each # represents the title type. For example, # is equivalent to an h1. ###### is equivalent to h6
Three dashes will give you a horizontal rule
[title](https://www.example.com)This will give you a link

Dark theme

Dark mode is here! You can change the appearance of your GitHub repo through your account settings. Why is this a big deal? When everything else is in dark theme — from your IDEs to consoles — switching between a bright white GitHub site to your dark desktop can be quite an eyesore.

To do this, go to your Account Settings, click on Appearance and your options of ‘Default light’, ‘Default dark’ and ‘Dark dimmed’ will appear.

Conclusion

Beyond the core git and GitHub features, there is a host of other capabilities that we don’t employ regularly but can use to improve our workflow. The ones listed here are just a starter guide to help increase your overall productivity and streamline your workflow. And as you may have guessed: they’re not really secrets – just uncommon knowledge – that you now possess.

Related articles

Identity Governance: What Is It And Why Should DevSecOps Care?

Did you know that the household data of 123 million Americans were recently stolen from Alteryx’s Amazon cloud servers in a single cyberattack? But the blame

Parallel Testing Unleashed: 10 Tips to Turbocharge Your DevOps Pipeline

Parallel Testing Unleashed: 10 Tips to Turbocharge Your DevOps Pipeline

Every software team is constantly looking for ways to increase their velocity. DevOps has emerged as a leading methodology that combines software development and IT operations

MongoDB Replica Set: A Developer's Tutorial to MongoDB Replication

MongoDB Replica Set: A Developer’s Tutorial to MongoDB Replication 

There’s one thing every developer should do – prepare for the unknown.  MongoDB is a NoSQL database widely used in web development, designed to handle unstructured

Stop leaks at the source!