This enables you to pull changes from a different remote branch to your local branch with just one command. You can write your own custom git commands to do whatever repetitive actions you do multiple times a day. For, me I usually update my local branch with the remote branch from which I branched off initially. Doing this would usually take multiple commands.
Instead of:
git stash
git checkout remote_branch
git pull --rebase origin remote_branch
git checkout current-branch
git rebase remote-branch
git stash apply
you can just do:
git refresh remote_branch_name
git refresh remote_branch_name
remote_branch_name
is the remote branch from which you want to pull changes.
-
Put the
git-refresh
file anywhere on your system. Lets say you've put in the folder namedgitScripts
, so the folder path is/Users/username/path/gitScripts/
-
Add the directory path to your environment
PATH
. For Linux/Mac, you can edit yourbash_profile
by doingvim ~/.bash_profile
. Add following line in the file in the beginning:#!/bin/bash # For git commands export PATH=$PATH:/Users/user/Documents/gitScripts # Other existing export statements. # End of file
Now after saving the file, do the following on the terminal:
source ~/.bash_profile
-
That's it. You are done. You should be able run the command.
Lets say you want to make a command git awesome
which takes one parameter and then calls series of git commands.
-
Create a file named
git-awesome
in a folder somewhere. -
Add that folder path to your environment
PATH
as shown previously, if not already. -
Have the following inside the file:
#!/bin/sh # Check if params are sufficient enough to go ahead. # P.S: This command takes one parameter so check if you have the param. parameter=$1 test -z $parameter && echo "ERROR: Please provide the param." 1>&2 && exit 1 # Find which is your current branch if currentBranch=$(git symbolic-ref --short -q HEAD) then echo On branch $currentBranch echo Doing work using $parameter ... # Write your git commands here echo Success! else echo ERROR: Cannot find the current branch! fi
Credits and Inspiration : Extending Git: add a custom command