How to write your own git commands like "git refresh"?
Shobhit Puri Oct 06, 2017 Updated on Oct 29, 2017
Do you use git version control? Obviously you do! Can you think of any actions that you perform using git multiple times a day? For example, I update my local code with remote master multiple times a day. This can involve few steps. In this post, I will show you how you can write your own custom
git commands, in just two simple steps. You'll be able to then club multiple git commands as one. At the end of the post, there is also a link to the GitHub repository, where you can find many such ready-to-use custom git commands like
git pushremote and
As engineers, we have this inbuilt desire to simplify and automate tasks to reduce the effort involved (others might call it 'laziness'). Let us apply that to git actions and take an example of
git refresh. During development of a feature, when I reach a stage that I would like to remember, I commit the changes and push the code to my remote branch. Before committing, I usually update my local branch with master, to avoid any conflicts when making a pull request. This becomes necessary when a development team gets big and the master branch gets updated frequently. For doing the above task of updating my local with remote master, I had to do the following six steps multiple times a day:
# 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 # Checkout the branch where I was working initially git checkout current-branch # Update current local branch from the local master git rebase master # Apply the stashed changes git stash apply
I thought that for a task that I do so frequently, "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. Each step is explained in the comments:
#!/bin/sh # Check if params are sufficient enough to go ahead. remoteBranch=$1 test -z "$remoteBranch" && echo "ERROR: Please provide the source branch with which you want to update your current 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:
And we are done!
git refresh master
You can find the scripts for the
git refresh and other similar custom commands like
git switch and
git pushremote on the following git repository: https://github.com/shobhitpuri/git-refresh
You can create your own commands by following the above steps. Just 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 originally published on shobhitpuri.com.