Git clone repository in a specific folder but keep default folder name
What about something like this:
## Your links in an array
declare -a arr=("https://github.com/username/repositoryName" "https://github.com/username/repositoryName2")
## Folder to store each of these git repos
folder=myFolderName
## Go through each link in array
for i in "${arr[@]}"
do
## Use basename to extract folder name from link
git clone $i $folder/$(basename $i)
done
Which will produce your following git repositories:
myFolderName/repositoryName
myFolderName/repositoryName2
Note: You don't need to add .git
at the end of each HTTPS link for git clone
.
If you need to include .git
for some reason, you can strip it with:
git clone $i $folder/$(basename ${i%.*})
How do I clone a Git repository into a specific folder?
Option A:
git clone git@github.com:whatever folder-name
Ergo, for right here
use:
git clone git@github.com:whatever .
Option B:
Move the .git
folder, too. Note that the .git
folder is hidden in most graphical file explorers, so be sure to show hidden files.
mv /where/it/is/right/now/* /where/I/want/it/
mv /where/it/is/right/now/.* /where/I/want/it/
The first line grabs all normal files, the second line grabs dot-files. It is also possibe to do it in one line by enabling dotglob (i.e. shopt -s dotglob
) but that is probably a bad solution if you are asking the question this answer answers.
Better yet:
Keep your working copy somewhere else, and create a symbolic link. Like this:
ln -s /where/it/is/right/now /the/path/I/want/to/use
For your case this would be something like:
ln -sfn /opt/projectA/prod/public /httpdocs/public
Which easily could be changed to test if you wanted it, i.e.:
ln -sfn /opt/projectA/test/public /httpdocs/public
without moving files around. Added -fn
in case someone is copying these lines (-f
is force, -n
avoid some often unwanted interactions with already and non-existing links).
If you just want it to work, use Option A, if someone else is going to look at what you have done, use Option C.
Change name of folder when cloning from GitHub?
You can do this.
git clone https://github.com/sferik/sign-in-with-twitter.git signin
# or
git clone git@github.com:sferik/sign-in-with-twitter.git signin
refer the manual here
Git clone default save directory
If no path is specified git clone example.repo
will create a new folder in the current directory called "example". If you wish to put the repository elsewhere you may navigate using the change directory command: cd
.
From the info you have given and assuming you are using Git Bash since you tagged your question with it, I would run cd /F/navigate-to-your-desired-location
, then run git clone example.repo
. Please note that Git Bash works similar to Unix and may see your HDD as a directory (folder). Best of luck!
How do I clone a subdirectory only of a Git repository?
EDIT: As of Git 2.19, this is finally possible, as can be seen in this answer.
Consider upvoting that answer.
Note: in Git 2.19, only client-side support is implemented, server-side support is still missing, so it only works when cloning local repositories. Also note that large Git hosters, e.g. GitHub, don't actually use the Git server, they use their own implementation, so even if support shows up in the Git server, it does not automatically mean that it works on Git hosters. (OTOH, since they don't use the Git server, they could implement it faster in their own implementations before it shows up in Git server.)
No, that's not possible in Git.
Implementing something like this in Git would be a substantial effort and it would mean that the integrity of the clientside repository could no longer be guaranteed. If you are interested, search for discussions on "sparse clone" and "sparse fetch" on the git mailinglist.
In general, the consensus in the Git community is that if you have several directories that are always checked out independently, then these are really two different projects and should live in two different repositories. You can glue them back together using Git Submodules.
git clone - how to keep .git in folder name by default
You don't need external scripts to wrap this! This is exactly what git aliases are for, e.g. I have an alias called clonez
. Add the following to your ~/.gitconfig
:
[alias]
clonez = "!sh -c 'git clone $1 ${1##*/}' -"
Usage:
git clonez git://github.com/till/jsonlint.git
Done. Creates a local folder jsonlint.git
.
git clone without project folder
git clone
accepts a last argument that is the destination directory, it is by default the name of the project but you can change it. In your case you probably want simply .
:
$ git clone origin-url .
But note that, from man git-clone
:
Cloning into an existing directory is only allowed if the directory is empty.
Clone git repositories and include the owner in the folder structure
Update:
As I just got a pleasant upvote of this old Q&A, I wanted to update my posting. I have recently begun to put this script into a PowerShell module (GitManagement) which can be found here: https://github.com/totkeks/PowerShell-Modules
Built a solution in powershell myself. You can configure different Git providers by name and the regular expression that is used to parse the URL and build the corresponding path for the repository.
<#
.DESCRIPTION
Clone git repositories including the project/user/organization/... folder structure
#>
Param(
[parameter(Mandatory = $true)]
[String]
$Url
)
#------------------------------------------------------------------------------
# Configuration of available providers
#------------------------------------------------------------------------------
$GitProviders = @{
"Azure" = {
if ($args[0] -Match "https://(?:\w+@)?dev.azure.com/(?<Organization>\w+)/(?<Project>\w+)/_git/(?<Repository>[\w-_]+)") {
return [io.path]::Combine($Matches.Organization, $Matches.Project, $Matches.Repository)
}
}
"GitHub" = {
if ($args[0] -Match "https://github\.com/(?<UserOrOrganization>\w+)/(?<Repository>[\w-_]+)\.git") {
return [io.path]::Combine($Matches.UserOrOrganization, $Matches.Repository)
}
}
}
#------------------------------------------------------------------------------
# Find the right provider and clone the repository
#------------------------------------------------------------------------------
$Match = $GitProviders.GetEnumerator() |
Select-Object @{n = "Provider"; e = {$_.Key}}, @{n = "Path"; e = {$_.Value.invoke($Url)}} |
Where-Object { $_.Path -ne $null } |
Select-Object -First 1
if ($Match) {
Write-Host "Found match for provider: $($Match.Provider)"
if ($Global:ProjectsDir) {
$TargetDirectory = [io.path]::Combine($Global:ProjectsDir, $Match.Provider, $Match.Path)
}
else {
Write-Error "No projects directory configured. Aborting."
}
git clone $Url $TargetDirectory
}
else {
Write-Error "No match found for repository url: $Url"
}
Clone contents of a GitHub repository (without the folder itself)
If the current directory is empty, you can do that with:
git clone git@github.com:me/name.git .
(Note the .
at the end to specify the current directory.) Of course, this also creates the .git
directory in your current folder, not just the source code from your project.
This optional [directory]
parameter is documented in the git clone
manual page, which points out that cloning into an existing directory is only allowed if that directory is empty.
Related Topics
Fastest Way to Determine User Permissions in /Etc/Sudoer
Sublime Text 2 Build (Ctrl +B) Intel Fortran Compiler
Reading Microphone Data by Polling Using Alsa [Or V4L2]
Openldap Naming Context Issue with Apache Directory Studio
How to Increase a Date Within a Loop in Bash
How to Continue Next Iteration When an Error Occurs in Bash
Issue with Signal Handling, Interrupt Handling
How to Delay Pipe Netcat to Connect on First Input
Perl Script to Capture Stderr and Stdout of Command Executed in Back-Quotes
Creating Filename_$(Date %Y-%M-%D) from Systemd Bash Inline Script
Tar: Cowardly Refusing to Create an Empty Archive
How to Generate Multiple Ssh Public Key and Configure Those on Windows Machine from Gitbash
How to Efficiently Get 10% of Random Lines Out of The Large File in Linux
How to Decide How Much Stack I Can Use After a Call to Pthread_Attr_Setstacksize