1. Overview

Often, when we clone a Git repository, we switch to the repository directory right after. After some time, it becomes kind of a reflex to change the directory. Sometimes, we chain these two commands for the same purpose. Not only that, but we might also have a script that clones multiple directories with random names. So, it becomes inconvenient to check the name of the directories every time we clone a repository.

However, there are a few ways to automate this process on Linux. So, in this tutorial, we’ll discuss how to switch to the cloned repository directory immediately. We’ll begin by writing a simple Bash function that we can use instead of using the git command directly. Then, we’ll learn how we can clone a repository into the current directory.

Finally, we’ll make use of the $_ variable to extract the directory name.

2. Switching to the Git Directory After Cloning

We can write a custom script for it, but then we’ll need to deploy the script on a bunch of machines where we need it. So, we’ll stick to the fundamental approach of using a Bash function, cloning into the current directory, and using the built-in $_ variable.

2.1. Bash Function Using basename

We can write a function when we need to run a set of commands frequently. We can put the function in our interactive shell configuration file. For a Bash shell, we can put the function in the .bashrc. So, let’s add the following function to the file:

gclone() {
  git clone "$1" && cd "$(basename "$1" .git)"

Let’s break it down:

  1. the function requires a single argument that we refer to by $1 — the URL of the repository
  2. the && operator chains multiple commands, where the commands after the && operator run only if the previous command was successful
  3. the basename command to get the base name of the directory from the URL
  4. cd changes the directory to the directory name returned by the basename command

Now, we can simply source the .bashrc file and test it out:

$ source ~/.bashrc

It should pick the newly created function that we can then use:

$ gclone https://github.com/himhaidar/in-your-pocket.git > /dev/null

basename also takes an optional suffix argument, which will be removed from the path. So, for instance, let’s say we have a URL that ends with “.git“:

$ repo=https://github.com/himhaidar/in-your-pocket.git

Next, let’s print out the basename of the URL:

$ basename $repo

Now, if we put the -s .git option, it removes that suffix:

$ basename -s .git $repo

Alternatively, we can remove the -s option and put “.git” as the last argument to basename:

$ basename $repo .git

2.2. Cloning Into the Current Directory

Additionally, we can also clone a repository into the current empty directory. We navigate to an already created directory, where we can clone a repository. So, if our use case requires cloning a repository we can use mktemp and switch to it:

$ cd $(mktemp -d)

mktemp returns the path to the temporary directory. Once we are inside the directory, we can run the following command to clone a repository into it:

$ git clone <repository_url> .

We can take these two commands and chain them together with &&:

$ cd $(mktemp -d) && git clone <repository_url> .

This approach is useful when we have an automation script that temporarily clones a set of repositories. Additionally, we should note that the command fails to run if we have a non-empty directory. For a non-empty directory, we can use the following workaround:

$ cd non_empty_dir
$ git init 
$ git remote add origin <repository_url>
$ git checkout -t origin/<branch> -f

This creates a new directory for the repository, initializes a new Git repository inside it, sets the remote origin, and checks out the specified branch. It’s important to note that this approach might overwrite the existing files.

2.3. The $_ Bash Variable

$_ is a special variable that holds the last argument of the previous command. For instance, if we clone a repository, the $_ variable contains the URL of the repository:

$ git clone https://github.com/himhaidar/in-your-pocket.git

Now, let’s echo the $_ variable:

$ echo $_

Now, we can chain these two commands:

$ git clone https://github.com/himhaidar/in-your-pocket.git && cd "$(basename "$_" .git)"

One thing to note here is that the $_ variable is exclusive to the Bash shell. It’s not implemented in other shells. Therefore, it’s not a very portable solution, but we can use it in our shell scripts that use Bash.

3. Conclusion

In this article, we discussed how we can automatically cd into a directory created by git clone. We wrote a simple Bash function that uses the basename helper to change the directory.

Additionally, we also saw how cloning into the current directory as well as using the $_ variable, have the same effect.

Inline Feedbacks
View all comments