Git clone with predefined user email and user name

Last Updated on by

Post summary: Small bash script to clone a Git repository and set and


There are cases when committing with a different user to a different Git repository is needed. Git offers a very easy command to change and, as long as you remember to do so.

git config "Firstname Lastname"
git config ""

I always forget to do it, so I made up a small script that I use to clone a repository and it does it for me.

Git also offers a command to globally change and and this is valid for each and every repository that is cloned. If the use case is to work with one name and email only, then maybe this is the best option.

git config --global "Firstname Lastname"
git config --global ""



if [ -z "$1" ]
  echo "Please provide the Git repo as argument"
  exit 1

if [ -z "$2" ]
  echo "Please provide the repo as argument"
  exit 1

if [ -z "$3" ]
  echo "Please provide the repo as argument"
  exit 1

IFS='/' read -r -a urlParts <<< "$1"

IFS="." read -r -a repoParts <<< "$urlPartsLast"
if [ "$repoPartsLast" == "git" ]
  unset 'repoParts[${#repoParts[@]}-1]'
repoName=$(printf ".%s" "${repoParts[@]}")

git clone "$1"

cd $repoName
git config "$2"
git config "$3"

The script file should be made executable with chmod +x and then the script can be invoked with the following command:

./ "Firstname Lastname"

Script insights

The script checks for empty arguments and returns error in case of such. Note that and can be hardcoded into the script itself, this makes it easier to invoke. Then the script splits by slash (/) the Git URL into different parts. It takes the last part, which is supposed to be the repository name. The last part is additionally split by dot (.) and the git suffix is ignored. Script clones the repository and navigates to the folder where it sets the and


This script is helping not to forget to clone a Git repository with correct and

Category: Tutorials | Tags: