DEV Community

loading...
Cover image for Automatic change directory after git clone

Automatic change directory after git clone

equiman profile image Camilo Martinez Updated on ・2 min read

The take command it's one of my favorite features about ZSH. Because with a single command you can create a new directory and automatically change into it.

So, why not do the same with git clone?

That's why I search for a way to create cake command, inspired on take but for clone, that automatically cd to the directory created after git clone.

Add this script function on ~/.zshrc file.

cake() {
  [[ $# -eq 0 ]] && return
  local last="${@: -1}"
  local rest="${@:1:${#}-1}"
  local cmd=""
  local dir="$(basename "$1" .git)"
  if [[ -z $rest ]]; then
    cmd="$last"
  else
    cmd="$rest $last"
    if [[ !"$last" =~ ^"-" ]]; then
      dir="$last"
    fi
  fi
  echo "\e[32m❯ \e[33mCloning...\e[0m"
  git clone $cmd
  echo "\e[32m❯ \e[33mChanging dir...\e[0m"
  cd $dir
}
Enter fullscreen mode Exit fullscreen mode

Once finish, reopen all terminals or update his source running source ~/.zshrc command and now you can use it.


Works for repository links both with and without ".git" and also if you manually target the directory name at the end.

cake

cake-target


But I know you have angry for more. I have created cape command that does the same as cake but also installs the npm packages and opens the project on VScode.

cape() {
  cake "$@"
  echo "\e[32m❯ \e[33mInstalling...\e[0m"
  npm i
  echo "\e[32m❯ \e[33mOpening...\e[0m"
  code .
}
Enter fullscreen mode Exit fullscreen mode

cape

vscode


Sources:


That’s All Folks!
Happy Coding 🖖

Buy me a cofee

Discussion (7)

pic
Editor guide
Collapse
tilkinsc profile image
Cody Tilkins • Edited

Largely incompatible with the common parameters --recursive and --branch.

I suggest using %* and figuring out how to scry %* to find the link.

You could also drop the quotes around the %1 and enforce the caller to wrap their arguments in quotes like,
cake "--branch master https://.../" FCC

Collapse
equiman profile image
Camilo Martinez Author • Edited

Definitely need an improved version taking those aspects.

While that happens, it can be used in the most simple cloning way and run the other commands like git branch after that.

Collapse
equiman profile image
Camilo Martinez Author

@tilkinsc code was updated and can be used with the other parameters.

Only one condition, URI needs to be the first parameter right next to the cake or cape command.

Collapse
tilkinsc profile image
Cody Tilkins

I reviewed the code. Try out using a regex match out a basic url and isolate the repo name and supply the entire command after a shift to the git clone function. This is good for testing your skills.

Collapse
michaelcurrin profile image
Michael • Edited

Thanks for sharing.

Note that this works because it's a shell function. I tried cd before in a shell alias and it didn't work.

I have some clone aliases here for git config. In particular I use the clone-s one to clone my own repos as SSH rules.

github.com/MichaelCurrin/dotfiles/...

Collapse
equiman profile image
Camilo Martinez Author

Yes, I've tried the same with aliases but didn't work. That's why I've decided to use a function instead.

By the way, cool undo and reset aliases. 😎

Collapse
michaelcurrin profile image
Michael

Thanks.

You can use code instead of cd I think in an alias. And you can also define and use an alias with in a single git alias.