Reputation: 4207
If I wanted to make a directory and change directory into it all in one line, I could do something like this:
mkdir dir_name && cd $_
How can I do the same with git clone
?
The command, git clone repo_url && cd $_
, won't work obviously, because there's no such directory as repo_url
. But is it possible to do it in one line?
Upvotes: 17
Views: 15978
Reputation: 2190
I don't really like aliases with special names like cdls
or gitclone
because on a new system I can't remember the original commands.
That's why I like the following solution for aliases:
## git extra features
# - clone + cd
function git() {
if [ $1 = "clone" ]
then
command git $@ && cd "$(basename "$_" .git)"
else
command git $@
fi
}
Using command
calls the original git
command, not this one in the ~/.bashrc
file (or ~/.bash_aliases
file).
Upvotes: 7
Reputation: 308
Oneliner alias (to include in a ~/.aliases
or wherever you keep your aliases):
alias gccd='f(){ git clone "$1" && cd "$(basename $1 .git)"; unset -f f; }; f'
All this does is define a temporary function, so it allows for taking parameters (AKA the repo URL). Enjoy!
Upvotes: 0
Reputation: 7303
If you want to find the name automatically, you could try something like this:
git clone https://host.com/you/the-repo.git && cd "$(basename "$_" .git)"
That way you don't have to specify a folder name to git.
The underscore is a special bash variable that holds the last argument to the previous command.
Example:
The following command will print 'C': touch "A" "B" "C" && echo "$_"
In the above git command, $_
will be https://host.com/you/the-repo.git
Returns the base file name of a string parameter.
basename string [suffix]
The basename command reads the String parameter, deletes any prefix that ends with a / (slash) and any specified Suffix parameter, and writes the remaining base file name to standard output.
In the above git command, the basename command will return the-repo
(which is what git will have named the repo it just cloned).
Upvotes: 24
Reputation: 22369
Bash function:
m.git.clone(){
local url="${1}" dir="${2}"
if [[ "${dir}" == "" ]]; then
dir="$(basename "${url}" .git)"
fi
git clone "${url}" "${dir}" && cd "${dir}"
}
Can be used:
m.git.clone [email protected]:user/path.to.git
Or
m.git.clone [email protected]:user/path.to.git customDir
Both work and cd only on success.
Upvotes: 0
Reputation: 1529
Add the following to your ~/.bashrc
. Don't forget to source it!
function gccd { git clone "$1" && cd "$(basename $1 .git)"; }
export -f gccd
gccd stands for git clone and cd. This is the function equivalent of an alias. Now you can type: gccd <repo>
. It will do exactly what you want.
Updated so that it works with both URL and with trailing .git. Thanks @kost
Upvotes: 1
Reputation: 1999
You can use the following
git clone http://repo_url.git && cd "!$:t:r"
Imp: Do not forget the double quotes in the cd
command, else it won't work in some other shells or Git Bash in Windows.
How does it work?
The first command is the obvious git clone
command. The second cd
command is intriguing.
Now there is something called Word Designators for command history
!$
is the last part of the last command runHere the last command run would be git clone http://repo_url.git
. This command consists of three parts. 1. git
, 2. clone
and 3. http://repo_url.git
. And http://repo_url.git
is the last part. Hence !$
==> http://repo_url.git
Then there is something called Word Modifiers, which modify the string preceding it.
:t
removes all leading file name components, leaving the tailSo here !$:t
would be read like (!$):t
. Hence !$:t
==> repo_url.git
:r
removes the trailing suffix from filenames like abcd.xyz
, leaving abcd
So here !$:t:r
would be read like {(!$):t}:r
. Hence !$:t:r
==> repo_url
So it would cd
to repo_url
To debug this yourself, use :p
which just prints the command preceding it without executing it. Equivalent would be echo
Run the following in the exact sequence
git clone http://repo_url.git
!$:p
==> http://repo_url.git
(or echo !$
)!$:t:p
==> repo_url.git
(or echo !$:t
)!$:t:r:p
==> repo_url
(or echo !$:t:r
)Upvotes: 1
Reputation: 3775
You can add a directory name for the git clone command:
git clone repo_url my_repo_dirname && cd "$_"
Upvotes: 5