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
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!"
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:
git refresh master
Please find the scripts for custom commands in the following git repository. https://github.com/shobhitpuri/git-refresh. Sample custom commands include
git switch and
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.