Frontend Weekly
Published in

Frontend Weekly

The definitive guide to using git aliases

Speed up your repeated git tasks

Who wouldn't want their dev time to be reduced ⏱

Photo by paolo candelo on Unsplash

I like to do things fast. Nowadays I am working on multiple branches in git and there are a lot of repetitive tasks like creating branches, checking out the project, staging files for commit, commit the code, push to the remote branch, etc.

I felt these tasks are taking too much time, each and every time I need to type all these sets of commands. I found two ways to solve this problem, it saves me a lot of time and I don't remember some of the lengthy commands. Without further talk, let’s jump into the ways to solve the issue

Git Aliases

Git aliases are my lifesaver in these days. This is a very simple trick, instead of writing the entire command, we can create aliases for the length commands. This is not only for git, but we can also create aliases for any of the commands.

Creating aliases is pretty simple, you just need to add the command in your profile file. For example, if you are using a bash terminal in mac you can open the .bash_profile file and add the commands. If you use any other shell-like zsh you can open the corresponding profile file, in case of zsh it is .zshrc.

  • Open your terminal, In my case, I am using a bash terminal.navigate to root folder by typing cd ~ .
  • For there you can see your shells profile file by typing ls -la command.
  • Edit the .bash_profile fils (in bash shell) by typing vim .bash_profile
  • You can either open these files directly by typing open ~/.bash_profile command.
  • add the aliases of your choice like
alias ga=’git add’

save the file, exit the terminal. Now you are ready to use your commands. Next time in your terminal you type gp, it will run the command git add.

These are some of my aliases I use every day.

One of the other cool things we can use is, we can pass arguments also to alias. For that, we have to create functions.

For example, I have to set upstream for my local branch by providing the name of the remote branch. I can achieve this by creating a simple function like

function gsf() { git branch — set-upstream-to=origin/”$1" `git symbolic-ref — short HEAD`; }

so whenever I wanted to set upstream for my branch I just type

gsf feature/branch

instead of typing

git branch — set-upstream-to=origin/feature/branch feature/my-local

I liked this function very much, previously I have to remember this command and type every time. Now I just need to pass my remote branch name.

lazygit

This is the second method for expediting your git task, this one is very specific to git. This one is another simple UI terminal for git. Using lazygit we can run the git commands by just pressing some basic keys. For example, if you want to add all files, you can just press the letter a . That's it files get added. I am currently not using this tool, because I am very much comfortable with my own aliases. For using lazygit we need to learn the tool, it might take some time for understanding the key bindings.

This is the link to use lazygit.

That’s it folks, Thanks for reading. Hope this will help someone.Cheers 🍻

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dileep Hariharan

Dileep Hariharan

Full-Stack Developer | Obsessive learner | Avid Reader