简体   繁体   中英

how to push from colab to github

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?

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

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

the answer marked accepted is the correct one but GitHub has deprecated the use of username password for HTTPS (see here and from github blog )

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

  • 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 ! cat ~/.gitconfig for --global , ! cat <repo_directory>/.git/config ! cat <repo_directory>/.git/config for --local.

**In order to write your desired configuration files, use the %%shell cmd in a new cell, eg 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

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. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM