8

I cloned a public github repo of mine into my drive. I am able to use colab with the notebooks. I can also pull. I however face the following error when trying to push despite having correctly configurated:

!git config --global user.email "my_email"
!git config --global user.name "my_user"

When doing !git push origin master I get the following error:

fatal: could not read Username for 'https://github.com': No such device or address

Has somebody encountered this problem before?

gstukelj
  • 2,291
  • 1
  • 7
  • 20
G. Macia
  • 1,204
  • 3
  • 23
  • 38
  • Have you tried "!git remote add origin https://:github@github.com//reponame.git" "!git push -u origin master" ? – milo Dec 23 '19 at 12:10
  • Does this answer your question? [fatal: could not read Username for 'https://github.com': No such file or directory](https://stackoverflow.com/questions/22147574/fatal-could-not-read-username-for-https-github-com-no-such-file-or-directo) – phd Dec 23 '19 at 16:13
  • https://stackoverflow.com/search?q=%5Bgit%5D+fatal%3A+could+not+read+Username – phd Dec 23 '19 at 16:14

7 Answers7

11

Here is how to clone, add a file, and push back

uname = "korakot"
!git config --global user.email '$uname@gmail.com'
!git config --global user.name '$uname'

from getpass import getpass
password = getpass('Password:')
!git clone https://$uname:$password@github.com/korakot/myrepo
%cd myrepo
# create a file, then add it to stage
!git add hello.txt
!git commit -m 'commit message'  # commit in Colab
!git push origin master          # push to github
korakot
  • 37,818
  • 16
  • 123
  • 144
  • I tried this approach and it seems that you can no longer use password to access and edit a git repository. The answer given by @Himanshu garg works. – mitra mirshafiee Sep 24 '21 at 06:46
6

The answer marked as accepted is correct, but GitHub has deprecated the use of username password for HTTPS. See here and Github's blog.

You should create a personal access token and use as follows:

!git remote add origin https://<USERNAME>:<Token>@github.com/<USERNAME>/reponame.git

To create your personal access token:

  1. in your GitHub account go to settings
  2. go to Developer settings
  3. go to Personal access tokens
  4. generate new token

Make sure to save it as you only see it once.

Wok
  • 4,956
  • 7
  • 42
  • 64
elaad5
  • 61
  • 2
  • 2
  • 1
    Thanks! Just in case some people encounter another error when using `add`, the solution works with `set-url`. See https://stackoverflow.com/a/10028024/376454 – Wok Jun 22 '23 at 09:01
5

None of the options mentioned above worked for me. I finally found the answer in this Medium article. Basically, the key line that I was missing was:

> !git remote add origin https://<USERNAME>:<PASSWORD>@github.com/<USERNAME>/reponame.git
G. Macia
  • 1,204
  • 3
  • 23
  • 38
4

To copy a file from google drive. These steps are the only one that needs to be executed.

I use this method to push a large dataset (size ~ 200MB) to github using google drive to save the INTERNET DATA and TIME. Using this method any file or folder inside your google drive can be pushed using google's backend internet.

Steps :-

  1. Push the below code section to colab Notebook.

    uname = "darkshadow013"
    !git config --global user.email 'email_address'
    !git config --global user.name 'darkshadow013'
    
    #Make a clone of github REPO
    !git clone https://<U_NAME>:<PERSONAL_ACCESS_TOKEN>@github.com/<U_NAME>/<REPO_NAME>
    
    #Copy file from either google drive after mounting using file browser
    !cp <PATH_OF_FILE_TO_COPY> /content/<REPO_NAME>
    
  2. Mount Google drive.

  3. This is an Optional Step, But must be performed if file size is >100MB as 100MB is the file_size limit of github.

    #Download git-lfs to Push Files larger than 100MB.
    !wget -O git-lfs.tar.gz https://github.com/git-lfs/git-lfs/releases/download/v2.13.2/git-lfs-linux-amd64-v2.13.2.tar.gz
    !tar xzf git-lfs.tar.gz
    !bash ./install.sh
    !git lfs install
    %cd <REPO_NAME>
    #FILE_NAME is the file with size >100MB and you wants to PUSH to GITHUB
    !git lfs track <FILE_NAME>
    
  4. Final Step is to add the file, commit and then push.

     !git add <FILE_NAME>
     !git commit -m 'commit message'  # commit in Colab
     !git push
    

That's it all you need to do is run this code and voila!!

Thanks

1
  • cd repo_directory
  • git config --get remote.origin.url to check the remote URL
  • git config -e --local check/update the [remote "origin"] section
  • git config -e --global check/update the [user] and [http] section

**Given the fact that you are using colab, in order to check your git configurations file , just use ! cat ~/.gitconfig for --global , ! cat <repo_directory>/.git/config for --local.

**In order to write your desired configuration files, use the %%shell cmd in a new cell, e.g for <repo>/.git/config same can be applied for ~/.gitconfig:

%%shell
cat <<EOF >> <repo>/.git/config
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = https://github.com/<user>/<repo>.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
EOF

--local By default, git config will write to a local level if no configuration option is passed. Local level configuration is applied to the context repository git config gets invoked in. Local configuration values are stored in a file that can be found in the repo's .git directory: .git/config

--global Global level configuration is user-specific, meaning it is applied to an operating system user. Global configuration values are stored in a file that is located in a user's home directory. ~ /.gitconfig on unix systems and

dejanualex
  • 3,872
  • 6
  • 22
  • 37
0
  1. First step is to produce a 'Personal access tokens' go to https://github.com -> settings -> Developer settings -> Personal access tokens -> Generate new token name it, Scopes it's access, copy the token string ghp_some_string in your text file

  2. in Google colab

    2.1: !git clone https://github.com/some_user/some_file.git

    2.2: navigate to the folder %cd ... -> make the desired modifications

    2.3:

!git config --global user.email "your@mail.com"
!git config --global user.name "your_github_username"
!git pull
!git add * :/
!git commit -m 'some_modif'
!git push https://ghp_some_string@github.com/some_user/some_file.git
Wael
  • 1,640
  • 1
  • 9
  • 20
0

The below steps worked successfully for me:

Generate a Personal Access Token on GitHub:

  • Navigate to your Git account settings, then Developer Settings.

  • Click the Personal access tokens menu, then click Generate new token.

  • Select repo as the scope. The token will be applicable for all the specified actions in your repositories.

  • Click Generate Token.

The generated token will look like ghp_qf32Wli6qjO......

Execute below commands in collab or your command prompt:

!git config --global user.name "Provide user name"
!git config --global user.email "Provide email"
username = 'Provide git user name'
git_token = 'Provide git token'
repository = 'Provide git repository name'
!git remote add origin https://{git_token}@github.com/{username}/{repository}.git
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
  • This shows up in my case: error: key does not contain a section: anshumansinha16 fatal: not a git repository (or any of the parent directories): .git – Anshuman Sinha Jun 30 '22 at 05:50