threads / discuss / 17816

Cloning into an existing directory

Subject: Cloning into an existing directory

## tl;dr

5 messages between Feb 16, 2009 and Feb 17, 2009.

replies: 4people: 3as markdown or json

Brent Goodrick· Feb 16, 2009, 07:10 UTC · lore
Hi,

I would like to manage my startup scripts such as .bashrc and other setup files relative to my HOME directory using Git. However, git-clone disallows cloning into the existing "." directory, but only allows cloning into a subdirectory that does not yet exist. If my home directory is /home/brentg and my remote repository is on remote_machine:~brentg/my_setup.git then git clone in my home directory on the local machine creates /home/brentg/my_setup with files such as .bashrc inside it, which is not what I want. I want them checked out and managed _in_ the current working directory, and not to mess with other files or directories that already exist that are never to be managed by git.

I don't want to create softlinks from files in the HOME directory down into the subdirectory (e.g., /home/brentg/.bashrc --> my_setup/.bashrc) if I can at all avoid it, since then to do so for all of my setup would require extra scripting work, and it may be the case that some setup files are required to be regular files and not symbolic links by certain programs. Moving the files manually is also out of the question, since then I can't do a git diff operation on the file directly.

I did see the --bare option to clone, but the entry in the git-clone man page implies that using that option turns off a lot of tracking of things that I believe I will need:

       --bare
           Make a bare GIT repository. That is, instead of creating
<directory> and placing the administrative files in
           <directory>/.git, make the <directory> itself the $GIT_DIR.
This obviously implies the -n because there is nowhere to check
           out the working tree. Also the branch heads at the remote
are copied directly to corresponding local branch heads, without
           mapping them to refs/remotes/origin/. When this option is
used, neither remote-tracking branches nor the related
           configuration variables are created.

Specifically, the statement: "branch heads at the remote are copied directly to corresponding local branch heads, without mapping them to refs/remotes/origin" and "neither remote-tracking branches nor the related configuration variables are created" are what is scaring me off.

Is there a way do to this, or is the --bare option really what I need here? If so, what are the caveats of the use of that option given the above "scary" statements.

What is interesting to note, is that git clone on a local repository to "." seems to work just fine. It is only when cloning from remote repositories that it complains about the target being an existing directory.

Thanks, bg

Russell Steicke· Feb 16, 2009, 07:31 UTC · re: Brent Goodrick · lore

Re: Cloning into an existing directory

On 2/16/09, Brent Goodrick <bgoodr@gmail.com> wrote:
Show 13 quoted lines
> Hi,
>
>  I would like to manage my startup scripts such as .bashrc and other
>  setup files relative to my HOME directory using Git. However,
>  git-clone disallows cloning into the existing "." directory, but only
>  allows cloning into a subdirectory that does not yet exist.  If my
>  home directory is /home/brentg and my remote repository is on
>  remote_machine:~brentg/my_setup.git then git clone in my home
>  directory on the local machine creates /home/brentg/my_setup with
>  files such as .bashrc inside it, which is not what I want. I want them
>  checked out and managed _in_ the current working directory, and not to
>  mess with other files or directories that already exist that are never
>  to be managed by git.

cd git init git remote add origin remote_machine:~brentg/my_setup.git git fetch git branch master origin/master git checkout master

You may have to delete .bashrc and others before git will overwrite them on checkout.

-- 
Virus found in this message.
Brent Goodrick· Feb 16, 2009, 16:13 UTC · re: Russell Steicke · lore

Re: Cloning into an existing directory

Thanks very much for the advice, Russell.

I did a test by creating the small repo with one file in it, .bashrc and got to the point of the git-fetch. That git-fetch did not complain about the pre-existing .bashrc file. Should it, or is the design of git-fetch to alter the state inside the .git area only and not the working tree? The scan of the user manual and the git-fetch man page does not seem to clarify the effect (none?) that git-fetch has on the working tree.

Now, I see that you said it would complain upon checkout, which it did:

$ git checkout master error: Untracked working tree file '.bashrc' would be overwritten by merge.

Fair enough: git is doing the right thing here and not overwriting the target file since it is not yet git-controlled. Given that I may have many files, my naive way of fixing that is to

1. Move aside each file it complains about
2. Run the git-checkout command again
3. Move each file back to their original names, thus creating a local
edit w.r.t. git
4. Run git diff to see those changes, making additional edits
5. Finally, check in the result

To side-step writing my own wrapper script around git, is there a command-line option to do steps 1 through 3, but not 4 and 5?

Thanks again for your help, bg

On Sun, Feb 15, 2009 at 11:31 PM, Russell Steicke <russellsteicke@gmail.com> wrote:

Show 30 quoted lines
> On 2/16/09, Brent Goodrick <bgoodr@gmail.com> wrote:
>> Hi,
>>
>>  I would like to manage my startup scripts such as .bashrc and other
>>  setup files relative to my HOME directory using Git. However,
>>  git-clone disallows cloning into the existing "." directory, but only
>>  allows cloning into a subdirectory that does not yet exist.  If my
>>  home directory is /home/brentg and my remote repository is on
>>  remote_machine:~brentg/my_setup.git then git clone in my home
>>  directory on the local machine creates /home/brentg/my_setup with
>>  files such as .bashrc inside it, which is not what I want. I want them
>>  checked out and managed _in_ the current working directory, and not to
>>  mess with other files or directories that already exist that are never
>>  to be managed by git.
>
> cd
> git init
> git remote add origin remote_machine:~brentg/my_setup.git
> git fetch
> git branch master origin/master
> git checkout master
>
> You may have to delete .bashrc and others before git will overwrite
> them on checkout.
>
>
>
> --
> Virus found in this message.
>
Andrew Ruder· Feb 16, 2009, 16:48 UTC · re: Brent Goodrick · lore

Re: Cloning into an existing directory

On Mon, Feb 16, 2009 at 9:13 AM, Brent Goodrick <bgoodr@gmail.com> wrote:
> 1. Move aside each file it complains about
> 2. Run the git-checkout command again
> 3. Move each file back to their original names, thus creating a local
> edit w.r.t. git

Actually, on my git (1.6.0.4) this just magically works due to the fact that 'git init' sets up the repository with HEAD pointing to refs/heads/master (which doesn't exist yet) and you go ahead and create the master branch with the 'git branch' command.

In other words, in this particular situation the 'git checkout' command is completely unnecessary and if you just run a 'git status' you should already see that git sees all the differences already as local edits (assuming you didn't call you branch in the 'git branch' step something other than master).

-- 
Andrew Ruder <andy@aeruder.net>
http://www.aeruder.net
Brent Goodrick· Feb 17, 2009, 02:53 UTC · re: Andrew Ruder · lore

Re: Cloning into an existing directory

On Mon, Feb 16, 2009 at 8:48 AM, Andrew Ruder <andy@aeruder.net> wrote:
Show 17 quoted lines
>
> On Mon, Feb 16, 2009 at 9:13 AM, Brent Goodrick <bgoodr@gmail.com> wrote:
> > 1. Move aside each file it complains about
> > 2. Run the git-checkout command again
> > 3. Move each file back to their original names, thus creating a local
> > edit w.r.t. git
>
> Actually, on my git (1.6.0.4) this just magically works due to the
> fact that 'git init' sets up the repository with HEAD pointing to
> refs/heads/master (which doesn't exist yet) and you go ahead and
> create the master branch with the 'git branch' command.
>
> In other words, in this particular situation the 'git checkout'
> command is completely unnecessary and if you just run a 'git status'
> you should already see that git sees all the differences already as
> local edits (assuming you didn't call you branch in the 'git branch'
> step something other than master).

Well, it does show them as local edits, and actually shows the file as deleted. I ended up having to do a git-add on those files and commit them, which will work for me.

bg

← back to recent threads