loading...

How to write your own git commands like "git refresh"?

shobhit profile image Shobhit Puri Updated on ・3 min read

Quick Summary

Do you use git version control? If you are in Software Development, I'm sure you have used it at some point. Can you think of any actions that you perform using git many times a day? For example, I update my local code with remote master many times a day. This can involve a few steps each time. In this post, I will show you how you can write your own custom git commands, in two simple steps. You'll be able to then club many git commands as one. At the end of the post, there is a link to a GitHub repository. There you can find many ready-to-use custom git commands like git refresh and git switch.

Introduction

As engineers, we have an inbuilt desire to simplify and automate tasks. While others might call it laziness, the aim is to reduce the effort involved in doing mundane tasks. Let us apply that to git actions and take an example of git refresh. During development, you often reach a stage that you would like to remember. When that happens, it is a good idea to commit the changes and push the code to your remote branch. Before creating a commit, it is customary to update your local branch with remote base branch. This helps to avoid any conflicts when making a pull request. In big teams, the remote branch gets updated quite often causing your local branch to lag behind. So this step becomes even more necessary as teams grow in size. For doing the above task of updating the local with remote master, on an average you'd do the following six steps:

   # Stash the changes in the current branch
   git stash
   # Checkout master branch 
   git checkout master
   # Pull from remote master and rebase
   git pull --rebase origin master
   # Go back to the previous branch where you were working
   git checkout current-branch
   # Update current local branch from the local master
   git rebase master
   # Apply the stashed changes
   git stash apply

For a task that you perform many times a day, "There is got to be a better way. And there is Kevin!"

Creating Custom Command

For our example of git refresh, we need to tell git to update our current branch from remote master.

Step 1: Create a file called git-refresh. Save the the file in a folder at /Users/user/Documents/gitScripts. Write the following in the git-refresh file. The comments explain the steps:

#!/bin/sh

# Check if params are enough to go ahead.
remoteBranch=$1
test -z "$remoteBranch" && echo "ERROR: Please provide the source remote branch." 1>&2 && exit 1

# Find which is your current branch
if currentBranch=$(git symbolic-ref --short -q HEAD)
then
    echo On branch "$currentBranch"
    echo "Pulling updates from the remote branch $remoteBranch ..."

    # Stash current changes
    git stash
    # Checkout remote branch from where you want to update. 
    git checkout "$remoteBranch"
    # Pull the branch to update it
    git pull --rebase origin "$remoteBranch"
    # Checkout current branch which you were on before.
    git checkout "$currentBranch"
    # Rebase the changes
    git rebase "$remoteBranch"
    # Apply the stashed changes
    git stash apply

    echo "Updated the $currentBranch with changes from $remoteBranch"
else
    echo ERROR: Cannot find the current branch!
fi

Step 2: Add the directory path to your environment PATH. For Linux/Mac, you can edit your bash_profile. Add following line in the beginning:

    #!/bin/bash
    # For git commands
    export PATH=$PATH:/Users/user/Documents/gitScripts
    # Other existing export statements.
    # End of file

source the file to apply the changes:

    source ~/.bash_profile

And... done!

Usage

git refresh master




GitHub Repository

Please find the scripts for custom commands in the following git repository. https://github.com/shobhitpuri/git-refresh. Sample custom commands include git refresh, git switch and git pushremote.

You can create your own commands by following the above steps. Create a new file git-yourcommand and write the commands in the shell script. You can pass the params as required. Feel free to write your feedback and let me know what awesome custom commands have you created.


This post was first published on shobhitpuri.com.

Discussion

pic
Editor guide
Collapse
terceranexus6 profile image
Paula

This is great, didn't know I could do such a thing. Really love it, thank you!!

Collapse
ben profile image
Ben Halpern

I too have never really used these, but I knew it was a thing, do to one of my favorite utilities, git standup.

Collapse
shobhit profile image
Shobhit Puri Author

Thanks @Paula for your kind comment. Hope it helps you with your daily work.
I played around with multiple ways to shorten the series of repetitive commands. One of them was to create aliases of the existing git commands. Like git pull --rebase origin can be git pro or you can make git stash apply as git sap but that still it wasn't much helpful. Custom commands have made life easier.

Collapse
swiftwinds profile image
SwiftWinds

For all those getting git: 'refresh' is not a git command. See 'git --help'., make sure to chmod +x git-refresh first if you're on macOS or Linux (I believe that Windows users do not have to worry about this)

Collapse
kennybatista profile image
Kenny Batista

Thank you!

Collapse
swiftwinds profile image
SwiftWinds

No problem :)

Collapse
nbelakovski profile image
nbelakovski

Thanks for this, I knew it was possible to make one's own git verbs, but needed to know how. So glad I was able to find this!

A suggestion if I may, your example set of commands could be much simplified with existing git functionality. You can achieve the same result with

git fetch origin master
git rebase --autostash origin/master

Autostash does exactly what you think it does, it automatically stashes any local changes, does the rebase, and then pops the changes. By fetching and rebasing off of the remote branch, you skip the need to update your local branch. Of course, this all besides the point of the article, thanks again!

Collapse
nrobinson2000 profile image
Nathan Robinson

Nice, but I'd recommend making your script Shellcheck compliant.

Collapse
shobhit profile image
Shobhit Puri Author

Thanks Nathan. That's really helpful. I'll update the script accordingly.

Collapse
swiftwinds profile image
SwiftWinds

Thanks, this is really useful. :) How does this deal with rebase conflicts though? How can we make the git command wait until we resolve the conflicts before continuing?