5 Git Shortcuts to Improve Your Coding Speed and Efficiency

Simplify your Git workflow and speed up your coding with these 5 essential Git shortcuts. Learn how to work smarter, not harder, in your programming.

#coding
#git
#softwaredevelopment
#devtools
5 Git Shortcuts to Improve Your Coding Speed and Efficiency
Picture by veeterzy on Unsplash

In the past two years of my coding career, I’ve seen more job offers for developers than any other role. Companies look for any kind of programmer able to create software, and this hunt for geeks is varied, ranging from Web developers to Dev Ops, from iOS and Android engineers to hybrid app developers. Something common to nearly every role was always a required skill: being able to work with Git.

It sounds pretty normal, Git is an essential tool to keep track of the progress and evolution of any software project. The documentation online is really good, and every beginner knows it is important to get at least a basic knowledge of this tool to have a chance to get one of those developer positions.

Today I won’t show you the well-known, basic commands, but I’ll give you some additional tricks that I found useful to increase my productivity when working with Git and get the most out of it.

Command 1: Formatted logging

bash
git log --pretty='%Cred%h%Creset | %C(yellow)%d%Creset %s %Cgreen(%cr)%Creset %C(cyan)\[%an\]%Creset' --graph --all

It is a combination of options available for the git log command which will make your commit history more user-friendly and easy to interact with. For example, it pipes searches with grep or other bash commands.

The options from the help page of the command:

  • pretty: It allows the user to format the log using many placeholders. You can find the entire list of them at Git pretty formats.
  • graph: Draw a text-based graphical representation of the commit history on the left-hand side of the output.
  • all: Pretend as if all the refs in refs/, along with HEAD, are listed on the command line as commit.

This is a long command, so create an alias on your terminal to make your life easier 😅

bash
ls = log --pretty='%Cred%h%Creset | %C(yellow)%d%Creset %s %Cgreen(%cr)%Creset %C(cyan)\[%an\]%Creset' --graph --all

Command 2: Remove merged branches

bash
git branch --merged | grep -v ‘\*| xargs -n 1 git branch -d

This is probably one of the aliases I use the most. It takes care of removing all the merged branches, keeping clean your local repository from legacy branches and saving the time of deleting manually each one of them.

I saved it as a clear alias since it removes stuff from the repo.

clear = "!git branch — merged | grep -v ‘*’ | xargs -n 1 git branch -d"

Command 3: Add files to a previous commit

bash
git commit --amend --no-edit

How often do you commit forgetting to add a file to the stage before doing it? Ouch! This is pretty common even for people with a strong workflow, and resetting that commit to creating a new one including the missing files is not the fastest solution. With this command, you can edit your last commit adding files quickly and safely:

bash
// We have 3 files ==> file1, file2, file3
git add file1 file2
git commit -m "first 3 files"
// Doh!
git add file3
git commend
// Quickly solved!

Easy right? Pretty fast and clean! commend is the alias created for this command:

bash
commend = commit --amend --no-edit

Command 4: Track your commits

bash
git log --all --author=$USER --since='$1'

Have you ever wanted to check all the commits you made since a specific time in a repo? It could be helpful to keep track of your performance and see how you’re performing as your skills grow! This command will show you your commits in a repo since a time you can pass as an argument to the alias we’ll create now. I also combine it with the git ls I showed you before:

bash
since = !sh -c \\"git ls --all --author=$USER --since='$1'\\" -

Why does this look different from a normal alias? Because we need to use a shell to interpolate the argument for the time value. The result is pretty wonderful🚀

Command 5: Create a .zip of your repo

bash
git archive --format zip HEAD | $1.zip

If you need to export an archive of your current HEAD revision, and you want to avoid including node_modules and all the other .gitignore files, this is the command for you! It creates a zip archive of the files with the name you pass at the end of the alias:

bash
export = !sh -c "git archive --format zip HEAD > $1.zip" -

Doing so, with a simple git export myRepo, you’ll create your copy in just a second!

Summary

Proper usage of git will drastically increase your productivity and help you to work better in a team! It is an amazing tool that keeps evolving. I recommend following the changelog of future releases to stay updated about the latest features!

Last updated: