Skip to content

Instantly share code, notes, and snippets.

@srebalaji
Last active October 10, 2023 13:10
Show Gist options
  • Save srebalaji/4b23fb62f6ce26c17b73ca07ae7cfc67 to your computer and use it in GitHub Desktop.
Save srebalaji/4b23fb62f6ce26c17b73ca07ae7cfc67 to your computer and use it in GitHub Desktop.
Examples of git custom command
#!/bin/sh
branch=$1
if [ ! -z "$1" ]
then
git branch -D $branch
git push -d origin $branch
else
echo "Branch name is not specified"
fi
#!/bin/sh
message=$1 # First parameter will be the commit message
currentBranch=$(git symbolic-ref --short -q HEAD) # Getting the current branch
if [ ! -z "$1" ] # checking if the commit message is present. If not then aborting.
then
git add .
git commit -m "$message"
git push origin $currentBranch
else
echo "Commit message is not provided"
fi
#!/bin/sh
git fetch
remoteBranch=$(git symbolic-ref --short -q HEAD)
if [ ! -z "$1" ]
then
remoteBranch=$1
fi
echo "Showing diff between $remoteBranch origin/$remoteBranch"
git diff $remoteBranch origin/$remoteBranch
#!/bin/sh
git fetch
remoteBranch=$(git symbolic-ref --short -q HEAD)
if [ ! -z "$1" ]
then
remoteBranch=$1
fi
echo "Showing logs between $remoteBranch origin/$remoteBranch"
git log $remoteBranch..origin/$remoteBranch --oneline
#!/bin/sh
newBranch=$1
if [ ! -z "$1" ]
then
git stash
git checkout -b $newBranch
git stash pop
else
echo "Branch name is not specified"
fi
@JesseRigon
Copy link

JesseRigon commented Dec 9, 2022

I update the script to take into account existing local branches, and also updated to replace git checkout with git switch as that is the current standard.

#!/bin/sh

# Git Switch With Changes (Staged and Unstaged, but not Untracked)

BranchName=$1

if [ ! -z "$BranchName" ]
then
	if [ $(git branch -l | grep $BranchName) ]
	then
  		git stash
  		git switch $BranchName
  		git stash pop
	else 
		git stash
		git switch -c $BranchName
		git stash pop
	fi
else
  echo "Branch name is not specified"
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment