How to push/pull files from Google Colab to GitHub using Personal Access Token

In this post, I will show how to use GitHub personal access token. Using this token we will push the files from the Google Colab to a GitHub repository. We will also see how you can pull the changes made in github to Colab.

Steps

  1. Generate Personal Access Token at github
  2. Add user email and user name to config
  3. Clone the github repository
  4. Change Current Directory to repository name
  5. Make changes and add them to commit stack
  6. Commit the change
  7. Push to the github
  8. pull command

STEP 1: Generate personal access token at github

The first step is to Generate Personal Access Token at github.
To generate token, you need to sign in to github.com.
If you are using github first time, you need to signup first. Enter a valid email and create password and enter available user name.
We have our github dashboard, to generate token,
  • Click on the profile picture, go to settings.
  • Then Go down and find the Developer settings
  • Click it, then person access tokens, and tokens.
  • You will get all your generated tokens here.
  • Click generate new token, then generate new token (classic). We will use classic one, you can use fine-grain also.
  • Write a note what’s this token for.
  • Set expiration, You can choose custom date also. let's set it for tomorrow.
  • Select scopes. It is very important. It will give you the access to read, write, commit the changes in the repository from remote.
  • You can check other scopes as well.
  • Go down and generate token
Here our token is generated.
Copy and save it to a secure place. Because you will not be able to see this token again.
Let’s copy the token and paste here.
token = "ghp_gn6YNwdxxSvJ3H.........................."
So for now we have generated our personal access token. We completed our first step.

2. Add user.email and user.name to config

Now the next step is to add user email and user name to the config.
For this we will use
git config -- global user.email "your_email_id"
git config -- global user.name "your_name"
Run this, Shift enter.
Note: Above change your_email_id and your_name with actual values.
You can check all Git Configuration settings using
git config --list

STEP 3: Clone the github repository

Now next step is to clone the GitHub repository.
We set the username and repository name. Here user name is your GitHub username and repo is GitHub repository.
username = "binary-study"
repo = "DemoBinary"
You can go and check to your GitHub account. If you don’t have a repository, you can create a new repository.
Now use "git clone command" to clone the repository to colab.
We already have token, username, and repo.
!git clone https://{token}@github.com/{username}/{repo}
Run the cell, shift enter.

Output

You may get the output something like below

Cloning into 'DemoBinary'...

remote: Enumerating objects: 60, done.

remote: Counting objects: 100% (60/60), done.

remote: Compressing objects: 100% (46/46), done.

remote: Total 60 (delta 25), reused 26 (delta 7), pack-reused 0 (from 0)

Receiving objects: 100% (60/60), 23.54 KiB | 7.85 MiB/s, done.

Resolving deltas: 100% (25/25), done.

A new folder with the same name as of repository name is created. We have files in this.

STEP 4: Change present working directory (pwd) to repository name

Let’s check the present working directory.
Use this command pwd
!pwd
Our current working directory is /content
We need to go inside our repo.
We will use cd repo name
%cd {repo}
/content/DemoBinary
Now we are inside our repository.

STEP 5: Make changes and add them to commit stack

Now next step is to make changes and add to commit stack.
You can modify the existing files or you can even add new files.
Let’s modify an existing file this myfile.py
We are adding here a new line
#this line is added from colab
Now let’s also create new file that is colab.htm
Write code in this file.
Save the file ctrl+S
You can check list of all file we have in current directory
Use ls command
!ls
These are the files that we have.


DemoGitHub.ipynb  myfile.py   README.md   Save_load_PyTorch_Model.ipynb

firstpython.py   mytext.txt  Refined_AGCN.ipynb  secondfile.py

Now let’s check the git status
!git status

Output

On branch main

Your branch is up to date with 'origin/main'.


Changes not staged for commit:

  (use "git add <file>..." to update what will be committed)

  (use "git restore <file>..." to discard changes in working directory)

modified:   myfile.py


Untracked files:

  (use "git add <file>..." to include in what will be committed)

mytext.txt


no changes added to commit (use "git add" and/or "git commit -a")


We have modified myfile.py
And we have one untracked file. This is the new file that we have created. We need to add to stack to commit.
To do this, we use "git add --a" command to add all files (new files and modified files).
You can add a specific file using file name like this.
!git add colab.htm
Let’s check the git status
We still need to add the modified file to commit stack.
For this, we again use "git add command" with file name.
But better to use git add –a, it will add all the files, new and modified.
Let’s now check the git status.

!git status

On branch main

Your branch is up to date with 'origin/main'.


Changes to be committed:

  (use "git restore --staged <file>..." to unstage)

modified:   myfile.py

new file:   mytext.txt

 
Now we have these two files to commit to finally save in GitHub.

STEP 6: Commit the changes

Now the next step is to commit the changes.
For this we use "git commit  -a -m" a for all files and m for a commit message. You can write any message, it’s just for your understanding.

!git commit -a -m "Commit from Google colab with new file"

[main d2797a9] Commit from Google colab with new file

 2 files changed, 2 insertions(+), 1 deletion(-)

 create mode 100644 mytext.txt


You can also commit specific file using file name like this
!git coomit filename -m specific message.

Let’s commit the colab.htm file first.
Run this
We have committed these files but still these files are not reflected in our GitHub repo. Let’s check.

STEP 7: Push to github

Finally we need to push to save all changes in GitHub repo.
So Final step is to push to github.
We use git push command
!git push origin branch_name

Here you can check your branch on github.
We have our branch as main.
So 
!git push origin main

Run this,

Enumerating objects: 6, done.

Counting objects: 100% (6/6), done.

Delta compression using up to 2 threads

Compressing objects: 100% (3/3), done.

Writing objects: 100% (4/4), 404 bytes | 404.00 KiB/s, done.

Total 4 (delta 2), reused 0 (delta 0), pack-reused 0

remote: Resolving deltas: 100% (2/2), completed with 2 local objects.

To https://github.com/binary-study/DemoBinary

   7e66ea7..d2797a9  main -> main


Well done!!
Finally we have pushed all the changes to github.
Let’s check it.
myfile.py is modified
And new file colab.htm is added to our repository.
Chek the commit messages - these are the messages that we have written while committing changes.
Commit from Google colab with new file
Commit from Google colab with modified.
This way you can push to all changes to github form colab.

8. Pull the changes from github

You can also pull changes from github to colab.Make some changes in any file
Let’s modify mytext.txt file.
Go for edit this file
Modify it and commit changes, write commit message and commit changes.
Now to pull the changes from github we use
!git pull origin main
command similar to push.
Run the cell and check if the changes are reflected here.

remote: Enumerating objects: 5, done.

remote: Counting objects: 100% (5/5), done.

remote: Compressing objects: 100% (3/3), done.

remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)

Unpacking objects: 100% (3/3), 971 bytes | 971.00 KiB/s, done.

From https://github.com/binary-study/DemoBinary

 * branch            main       -> FETCH_HEAD

   d2797a9..d22cdf4  main       -> origin/main

Updating d2797a9..d22cdf4

Fast-forward

 mytext.txt | 1 +

 1 file changed, 1 insertion(+)

Yes here also our file is updated.
So finally we have used push the changes from Goole colab to Github repository using Personal Access token. Also we push the changes from GitHub to colab.

Comments