Git - few useful tips and commands

Photo by Gabriel Heinzer / Unsplash

Every one of us is working with git. Some developers like to use GUI clients to make their life easier, I prefer the good old command line.

Most of the time, we do the same things over and over again. Switching to master, starting new feature branch, rebasing with the master branch, pushing a fresh branch to remote. I have a few tips and tricks for you!

All aliases should be added to the .zshrc file to work! After adding the alias, remember to reload with source ~/.zshrc:)

Switching to the fresh master branch

This is the most common scenario, quickly change a branch to fresh master:

alias tomaster="git checkout master && git pull origin master"

New feature branch

Some projects require a specific feature branch name, like project_name/feature/jira_story, putting it every time by hand is not very efficient, so we should make it shorter!

newbranch() {
  git checkout -b "project_name/feature/$1"

Now all you have to do is write newbranch jira_story_number and the alias will create a new branch with `project_name/feature/jira_story_number name!

Refresh master without changing branch

This one comes in handy if you want to refresh master before rebasing:

alias refmaster="git fetch origin master:master"

Merge/rebase master to/on the feature branch

Just to make this easier add alias givemaster="git pull origin master" and alias rebasemaster="refmaster && git rebase master" to .zshrc. Problem solved:) You can see, that I'm using the alias inside an alias, that is neat! This ensures we rebase with an up-to-date master branch!


Do you have many old branches laying around, git is starting to work slow? You need some housekeeping.

alias cleangit="tomaster && git branch | grep -v master | xargs git branch -D". This will switch to the master, get all branches without the master and delete them, in one command:)

Pushing fresh feature branch

Very simple, just setting proper remote with a couple of letters:

alias pusz="git push -u origin HEAD"

And that's it. If you want, experiment with your own aliases. Maybe you use bundler to run Fastlane lanes and you are tired of putting all these words every time? Maybe you run a couple of test lines one by another and you always forget their names? Create an alias, make your life easier!

Bonus - branch name in the console!

This one is lengthy but worth it!

# Load version control information
autoload -Uz vcs_info
precmd() { vcs_info }

# Format the vcs_info_msg_0_ variable
zstyle ':vcs_info:git:*' formats 'on branch %b'

# Set up the prompt (with git branch name)
PROMPT='%n in ${PWD/#$HOME/~} ${vcs_info_msg_0_} > '

And the result is awesome:

Hope you like it!

Artur Gruchała

Artur Gruchała

I started learning iOS development when Swift was introduced. Since then I've tried Xamarin, Flutter, and React Native. Nothing is better than native code:)