14

Is it possible to clone a single file from a remote repository with Git? For instance, I am looking to copy someone else's .htaccess file into my own project. I do not need their entire repository, just this one file.

MadPhysicist
  • 5,401
  • 11
  • 42
  • 107
  • 2
    Possible duplicate of [How to checkout only one file from git repository?](http://stackoverflow.com/questions/2466735/how-to-checkout-only-one-file-from-git-repository) – Jonathan.Brink May 20 '16 at 18:35

3 Answers3

26

Rather than clone, perhaps you can simply download the file.

Assuming the repository is public, you can download a single file like this:

wget https://raw.githubusercontent.com/jquery/jquery/master/src/ajax.js

To get the url, navigate to the file in GitHub, and choose the "raw" view.

Note that this will only work with public repos.

Jonathan.Brink
  • 23,757
  • 20
  • 73
  • 115
  • Yes, I have done just that. However, I am still wondering if Git allows for such operation to be done. It would be convenient. Can wget copy directories as well? – MadPhysicist May 20 '16 at 18:34
  • @MadPhysicist Yes, it's possible. See http://stackoverflow.com/questions/2466735/how-to-checkout-only-one-file-from-git-repository . I was suggesting that perhaps a tool like wget or curl may be an easier option – Jonathan.Brink May 20 '16 at 18:36
  • Should be updated to note that only works with public repos, and the OP does not specify public or private. – shellscape Jan 04 '21 at 17:23
0

For Example, you want to clone this python file efficientnet_weight_update_util.py

tensorflow/tensorflow/python/keras/applications/efficientnet_weight_update_util.py

If you use the wget tool to get the file from the link you will get the py file with HTML format so instead, use the raw version from https://raw.githubusercontent.com

view raw

and copy the URL to use it with wget

https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/python/keras/applications/efficientnet_weight_update_util.py

e.g.


!wget https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/python/keras/applications/efficientnet_weight_update_util.py

now you have the efficientnet_weight_update_util.py file only from the repo.

0

I needed this feature so I created this tool. Copy the content to the file (e.g. gitget.sh)

#!/bin/bash
CURRENT_DIR=`pwd`
REPOSITORY=$1
REPOSITORY_BRANCH=$2
REMOTE_PATH=$3
LOCAL_PATH=$4
TMP_DIR=$(mktemp -d -t ci-XXXXXXXXXX)
cd $TMP_DIR 
git clone -b $REPOSITORY_BRANCH -n  $REPOSITORY --depth 1 . 
git checkout HEAD $REMOTE_PATH
if [[ -z $LOCAL_PATH ]]; then
    mv $REMOTE_PATH $CURRENT_DIR
else
    if [[ "$LOCAL_PATH" = /* ]]; then
        mv $REMOTE_PATH $LOCAL_PATH
    else
        mv $REMOTE_PATH $CURRENT_DIR/$LOCAL_PATH
    fi 
fi

cd $CURRENT_DIR && rm -rf $TMP_DIR

Usage

Download remote file to current dir:

bash gitget.sh git@host:username/repo.git main some/file/path

Download remote file to specific file (relative or absolute path)

bash gitget.sh git@host:username/repo.git main some/file/path some/dir/file.ext

Download remote file to specific dir (relative or absolute path)

bash gitget.sh git@host:username/repo.git main some/file/path some/dir/

Note: Works with https urls, too.

This tool is leaning on depth=1 feature of clone - whole repository is not fetched to local.

For our convenience, we can get this script from gitlab's repository as well: https://gitlab.com/vladimir.djuricic/tools/-/raw/main/bash/gitget.sh?inline=false

Vladimir Djuricic
  • 4,323
  • 1
  • 21
  • 22