git: How do you add an external directory to the repository?

前端 未结 8 1758
旧巷少年郎
旧巷少年郎 2020-12-02 10:16

I want to add an external directory to an existing repository.

External Dir: /home/some/directory

Working Dir: /htdocs/.git

If I attempt the followin

相关标签:
8条回答
  • 2020-12-02 10:48

    Or you can use submodule if you want to work across different git repos (ln doesn't work in this way). Try 'man git-submodule'.

    0 讨论(0)
  • 2020-12-02 10:52
    git --work-tree=/ add /home/some/directory
    
    0 讨论(0)
  • 2020-12-02 10:53

    If the code that you want to add to your repo is not too big, you could also automatically copy it to your repo each time before you push.

    In my case I for example need one R file, which I modify quite often, in several repos and I wrote a little shell script:

    #!/usr/bin/env bash
    
    cp -r /some/directory/file.R .
    git add .
    git commit -m "add"
    git push
    

    ...and I run this script in order to push all changes, including the ones in /some/directory.

    0 讨论(0)
  • 2020-12-02 10:53

    This got me thinking, since I would like very much the solution of using a symlinked file/dir but am stuck on a windows computer. As far as I know the symlinks in windows doesnt really work in the same way. So another solution could be to write two scripts/bash functions to "import" and "export" the file(s) in question, such as:

    import() {
        cp -fr /home/some/directory /htdocs/
    }
    
    export() {
        cp -fr /htdocs/directory /home/some/
    }
    

    Then you would have a copy of the file in your repository, which you could git add.

    0 讨论(0)
  • 2020-12-02 10:55

    I had the same error... Googled it to death... not much came out.

    Christian's answer worked :

    git --work-tree=/ add /home/some/directory
    

    But then "work-tree" got me going. I grep'd all Git docs and came up with core.worktree.

    I used

    git config --global core.worktree /
    

    And voila! I can now add from any directory in the system. I don't know if it will cause problems any other places, but I'll try to update as I go along.

    0 讨论(0)
  • 2020-12-02 11:03

    Add a symbolic link to the directory within the repository. Then, add the same.

    ln -s /home/some/directory/
    git add directory
    
    0 讨论(0)
提交回复
热议问题