DEV Community

abbazs
abbazs

Posted on

A function to clone git repository using gitlab personal access token

A step-by-step tutorial on how to use the gitlab_clone function from the provided code:

Step 1: Create a Folder for Scripts

  • Open a terminal or command prompt.
  • Run the following command to create a folder named "scripts" in your home directory:
  mkdir $HOME/scripts
Enter fullscreen mode Exit fullscreen mode

Step 2: Save the Function in a File

  • Open a text editor of your choice.
  • Copy the following code block:
  #####GITLAB CLONE WITH TOKEN############################################
  gitlab_clone() {
      get_token() {
          echo "Secrets file not found or GITLAB_TOKEN is empty. Please provide Git username and personal access token:"
          read -r -p "Username: " username
          read -r -p "Token: " token
          echo "export GITLAB_TOKEN=$username:$token" > $HOME/scripts/secrets.sh
          source $HOME/scripts/secrets.sh
      }

      if [[ -f $HOME/scripts/secrets.sh ]]; then
          source $HOME/scripts/secrets.sh
      fi

      local repo_url=$1
      local cred=${GITLAB_TOKEN}

      if [[ -z $cred ]]; then
          get_token
          cred=${GITLAB_TOKEN}  # Assign the value of GITLAB_TOKEN after calling get_token
      fi

      local url=$(echo "$repo_url" | sed "s|https://|https://$cred@|")
      git clone "$url"
  }
  alias gclone="gitlab_clone"
Enter fullscreen mode Exit fullscreen mode
  • Paste the copied code into the text editor.
  • Save the file with the name "git_func.sh" inside the "scripts" folder created in Step 1. Make sure to keep the .sh extension for shell script files.

Step 3: Source the File in .bashrc

  • Open a terminal or command prompt.
  • Run the following command to open the .bashrc file in a text editor:
  nano ~/.bashrc
Enter fullscreen mode Exit fullscreen mode
  • Scroll to the end of the file and add the following line:
  source $HOME/scripts/git_func.sh
Enter fullscreen mode Exit fullscreen mode
  • Press Ctrl+X to exit the text editor, then press Y to save the changes, and Enter to confirm the file name.

Step 4: Activate the Changes

  • In the same terminal or command prompt, run the following command to reload the .bashrc file and activate the changes:
  source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Step 5: Using the gitlab_clone Function

  • Now, you can use the gitlab_clone function, which is aliased to gclone, in your terminal.
  • Run the following command to clone a GitLab repository:
  gclone <repository_url>
Enter fullscreen mode Exit fullscreen mode

Replace <repository_url> with the actual URL of the repository you want to clone.

  • If the GITLAB_TOKEN is not set or the secrets file doesn't exist, the function will prompt you to provide your Git username and personal access token.
  • Enter your Git username when prompted and press Enter.
  • Enter your personal access token when prompted (the input will be hidden) and press Enter.
  • The function will save the GitLab token in the secrets.sh file and automatically source it for future use.
  • The repository will be cloned using the provided credentials.

You have now set up the gitlab_clone function and can use it to clone GitLab repositories conveniently.

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay