# Logging in to HF Git repository from the code

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Flogging-in-to-hf-git-repository-from-the-code%2F122411%3E%5C
**Category:** Beginners\
**Created:** [November 15, 2024, 10:42am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Flogging-in-to-hf-git-repository-from-the-code%2F122411 "2024-11-15T10:42:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![electronics-is-cool](https://avatars.discourse-cdn.com/v4/letter/e/c0e974/32.png) [@electronics-is-cool](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Felectronics-is-cool)%5C
**Post date:** [November 15, 2024, 10:42am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Flogging-in-to-hf-git-repository-from-the-code%2F122411%2F1 "2024-11-15T10:42:09Z")

</div>

Hi, I have an issue authenticating to the private Hugging Face repository from the code. I have three repositories, one is for the source code of my application that I’ve developed over past months, the second is for my HF code that will genearate summaries of the code commits using JetBrains-Research/cmg-codereviewer-with-history and the third one is empty repository to which I want to save summaries of all the code commits.

I use this code:

```auto
# Log in to Hugging Face Hub
login(token=os.getenv("HF_TOKEN"), write_permission=True) 

# Git configuration
subprocess.run(["git", "config", "--global", "user.name", "my-hf-user-name"])

...

log("Cloning repository")
repo_url = "https://huggingface.co/spaces/my-organization-name/my-real-application"

# Clone the repository (no need for token in URL)
subprocess.run(["git", "clone", repo_url, output_dir])

```

Where of course there are real values in my-hf-user-name, my-organization-name and my-real-application. I also have defined HF\_TOKEN in environment variables.

However, I receive:

> [LOG] Cloning repository  
> Cloning into ‘repo’…  
> fatal: could not read Username for ‘[https://huggingface.co](https://huggingface.co)’: No such device or address  
> [LOG] Saving summaries to file  
> [LOG] Summaries saved to repo/commit\_summaries.json  
> [LOG] Configuring Git  
> error: could not lock config file //.gitconfig: Permission denied  
> [LOG] Committing and pushing changes  
> fatal: detected dubious ownership in repository at ‘/app’  
> To add an exception for this directory, call:  
> git config --global --add safe.directory /app

In my Dockerfile, I have additionally:

```auto
RUN git config --system --add safe.directory /app/repo
RUN git config --global user.name "my-hf-user-name"

```

Currently I’m trying to save to the repository (and this is where it fails) but later I’ll have to deal with reading from source repository too.

How can I correctly authenticate to HF Git repository from my code?

Thanks!

---

<div class="post-metadata">

**Author:** ![not-lain](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/not-lain/32/59092_2.png) [@not-lain](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnot-lain)%5C
**Post date:** [November 15, 2024, 11:12am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Flogging-in-to-hf-git-repository-from-the-code%2F122411%2F2 "2024-11-15T11:12:00Z")

</div>

i think you need to setup your ssh credentials first

> **[Git over SSH](https://huggingface.co/docs/hub/en/security-git-ssh#checking-for-existing-ssh-keys)**
>
> We’re on a journey to advance and democratize artificial intelligence through open source and open science.

if you don’t want to setup your ssh credentials, haggingface offers other tools that you can use for downloading repos or files and uploading them in **python** :

- [download](https://huggingface.co/docs/huggingface_hub/main/en/package_reference/file_download) :
  - entire repo download : snapshot\_download
  - single file download : hf\_hub\_download

- [upload](https://huggingface.co/docs/huggingface_hub/main/en/guides/upload) :
  - entire folder : upload\_folder
  - single file : upload\_file

---

<div class="post-metadata">

**Author:** ![electronics-is-cool](https://avatars.discourse-cdn.com/v4/letter/e/c0e974/32.png) [@electronics-is-cool](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Felectronics-is-cool)%5C
**Post date:** [November 15, 2024, 12:27pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Flogging-in-to-hf-git-repository-from-the-code%2F122411%2F3 "2024-11-15T12:27:28Z")

</div>

Thank you! So I’m doing it this way now:

```auto
repo_id_to_summarize = "my-organization-name/my-real-project"
local_dir = "repo_to_summarize"
snapshot_download(
    repo_id=repo_id_to_summarize, 
    local_dir=local_dir, 
    repo_type="space", 
    cache_dir=".cache"
)

if os.path.exists(os.path.join(local_dir, ".git")):
    log("Repository downloaded successfully")
else:
    log("Error: Repository not found")

```

But here I get “Error: Repository not found” and also later, when it gets git commits:

> Traceback (most recent call last):  
> File “summarize\_commits.py”, line 65, in   
> commits = get\_git\_commits()  
> File “summarize\_commits.py”, line 27, in get\_git\_commits  
> repo = Repo(local\_dir)  
> File “/usr/local/lib/python3.8/site-packages/git/repo/base.py”, line 289, in **init**  
> raise InvalidGitRepositoryError(epath)  
> git.exc.InvalidGitRepositoryError: /app/repo\_to\_summarize

---

<div class="post-metadata">

**Author:** ![not-lain](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/not-lain/32/59092_2.png) [@not-lain](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnot-lain)%5C
**Post date:** [November 15, 2024, 12:36pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Flogging-in-to-hf-git-repository-from-the-code%2F122411%2F4 "2024-11-15T12:36:00Z")

</div>

try this instead

```diff
repo_id_to_summarize = "my-organization-name/my-real-project"
local_dir = "repo_to_summarize"
snapshot_download(
    repo_id=repo_id_to_summarize, 
    local_dir=local_dir, 
    repo_type="space", 
    cache_dir=".cache"
)

-if os.path.exists(os.path.join(local_dir, ".git")):
+if os.path.exists(local_dir):
    log("Repository downloaded successfully")
else:
    log("Error: Repository not found")

```

---

<div class="post-metadata">

**Author:** ![electronics-is-cool](https://avatars.discourse-cdn.com/v4/letter/e/c0e974/32.png) [@electronics-is-cool](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Felectronics-is-cool)%5C
**Post date:** [November 15, 2024, 12:50pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Flogging-in-to-hf-git-repository-from-the-code%2F122411%2F5 "2024-11-15T12:50:54Z")

</div>

Thanks! But it will only help me with custom validation of the path, it won’t solve the issue with “git.exc.InvalidGitRepositoryError: /app/repo\_to\_summarize”
