How do I clone into a non-empty directory?

前端 未结 16 1300
庸人自扰
庸人自扰 2020-11-22 06:20

I have directory A with files matching directory B. Directory A may have other needed files. Directory B is a git repo.

I want to clone directory B to directory A bu

相关标签:
16条回答
  • 2020-11-22 06:41

    I liked Dale's answer, and I also added

    git clone --depth 2 --no-checkout repo-to-clone existing-dir/existing-dir.tmp
    git branch dev_new214
    git checkout dev_new214
    git add .
    git commit
    git checkout dev
    git merge dev_new214
    

    The shallow depth avoided a lot of extra early dev commits. The new branch gave us a good visual history that there was some new code from this server that was placed in. That is the perfect use branches in my opinion. My thanks to the great insight of all the people who posted here.

    0 讨论(0)
  • 2020-11-22 06:42

    The following worked for me. First I'd make sure the files in the a directory are source-controlled:

    $ cd a
    $ git init
    $ git add .
    $ git commit -m "..."
    

    Then

    $ git remote add origin https://URL/TO/REPO
    $ git pull origin master --allow-unrelated-histories
    $ git push origin master
    
    0 讨论(0)
  • 2020-11-22 06:46

    In the following shell commands existing-dir is a directory whose contents match the tracked files in the repo-to-clone git repository.

    # Clone just the repository's .git folder (excluding files as they are already in
    # `existing-dir`) into an empty temporary directory
    git clone --no-checkout repo-to-clone existing-dir/existing-dir.tmp # might want --no-hardlinks for cloning local repo
    
    # Move the .git folder to the directory with the files.
    # This makes `existing-dir` a git repo.
    mv existing-dir/existing-dir.tmp/.git existing-dir/
    
    # Delete the temporary directory
    rmdir existing-dir/existing-dir.tmp
    cd existing-dir
    
    # git thinks all files are deleted, this reverts the state of the repo to HEAD.
    # WARNING: any local changes to the files will be lost.
    git reset --hard HEAD
    
    0 讨论(0)
  • 2020-11-22 06:48

    I had a similar problem with a new Apache web directory (account created with WHM) that I planned to use as a staging web server. I needed to initially clone my new project with the code base there and periodically deploy changes by pulling from repository.

    The problem was that the account already contained web server files like:

    .bash_history
    .bash_logout
    .bash_profile
    .bashrc
    .contactemail
    .cpanel/
    ...
    

    ...that I did not want to either delete or commit to my repository. I needed them to just stay there unstaged and untracked.

    What I did:

    I went to my web folder (existing_folder):

    cd /home/existing_folder
    

    and then:

    git init
    git remote add origin PATH/TO/REPO
    git pull origin master
    git status
    

    It displayed (as expected) a list of many not staged files - those that already existed initially from my cPanel web account.

    Then, thanks to this article, I just added the list of those files to:

    **.git/info/exclude**
    

    This file, almost like the .gitignore file, allows you to ignore files from being staged. After this I had nothing to commit in the .git/ directory - it works like a personal .gitignore that no one else can see.

    Now checking git status returns:

    On branch master
    nothing to commit, working tree clean
    

    Now I can deploy changes to this web server by simply pulling from my git repository. Hope this helps some web developers to easily create a staging server.

    0 讨论(0)
提交回复
热议问题