How to Connect Google Colab with GitHub
This article provides a practical guide for transitioning Python code created in Google Colab to a full-fledged development workflow using VSCode on a MacBook and GitHub. It outlines the specific steps for those who have previously developed only in Colab to establish a more efficient development system by setting up version control with GitHub and a comfortable local editing environment with VSCode.
- Repository Setup: Preparing the receiving environment on GitHub.
- Initial Sync: Safely migrating code from Colab to GitHub.
- Local Environment: Setting up the editing environment in VSCode.
- Operational Workflow: Establishing a daily development cycle.
Current Situation and Goals
- Current Situation
- Code exists only on Google Colab (.ipynb file).
- A private repository is ready on GitHub.
- No code exists in the local environment.
- Version control with Git has not been implemented.
- Goals
- Manage code change history with GitHub.
- A comfortable editing environment on MacBook/VSCode.
- Smooth operational checks in Google Colab.
1. GitHub Repository Setup
To accept saves from Colab, you first need to create a main
branch in your repository.
- Open your GitHub repository.
- Create a README file: Click "Create a new file" or "Add a README file." Creating a
README.md
will automatically generate themain
branch.
2. Initial Save from Colab to GitHub
- Open the target notebook in Colab.
- From the menu, select File > Save a copy in GitHub.
- Configure the destination:
- Repository: Select
your-username/your-repository-name
. - Branch: Select
main
. - Commit message: Enter a clear message, such as "Initial commit of notebook from Colab."
- Repository: Select
- Click the
OK
button to execute the save.
3. Local Development Environment Setup
Clone the Repository
- In your GitHub repository, click the green "Code▼" button.
- Copy the HTTPS URL.
- Open your terminal and run the following command:
git clone [COPIED_REPOSITORY_URL]
VSCode Setup
- Open the cloned folder in VSCode.
- Install useful extensions for development:
- Python
- Jupyter
- To make
.ipynb
files easier to manage with Git, configure them to open in a text-based format.- Right-click the file > "Open With..." > "Text Editor."
- This will display the file in JSON format, making it easier to see changes and diffs.
4. Daily Development Workflow
From Editing to Saving on GitHub
-
Edit Code in VSCode
- Freely implement your ideas and edit code in your local environment.
-
Save Changes to GitHub
- Once you're done editing, run the following commands in the terminal to push your changes to GitHub.
git add .
git commit -m "feat: Improve user authentication logic"
git push origin main -
Verify in Colab
- Open Colab, select File > Open notebook, and switch to the GitHub tab.
- Search using your repository URL (
https://github.com/your-username/your-repository-name
). - Open the updated notebook and run it to verify its operation.
Key Operational Rules
To maintain a smooth workflow and avoid conflicts, it's crucial to follow these rules.
1. Sync Before Starting Work
No matter which environment you're editing in, always fetch the latest version from GitHub first.
- When working locally:
git pull origin main
- When working in Colab:
- Re-open the latest notebook from "File > Open notebook > GitHub" tab.
2. Save Upon Completion
When you finish editing, always save (push) your changes to GitHub.
- If you edited locally:
git add .
git commit -m "Description of changes"
git push origin main - If you edited in Colab:
- Execute "File > Save a copy in GitHub" and save with a commit message describing the changes.
3. Avoid Simultaneous Edits in Multiple Places
Do not edit the same file in multiple environments at the same time, as this is the leading cause of conflicts. Start working in a new environment only after the work in the previous one is completed and saved to GitHub.
4. Handling Conflicts
If a conflict occurs, handle it calmly in your local environment.
- Check the status with
git status
. - If necessary, stash your current changes with
git stash
. - Fetch the latest version from the remote with
git pull origin main
. - Use VSCode's diff view to resolve the conflicts, then commit and push again.
Summary
Following these steps will help you build an efficient development environment that integrates Google Colab, GitHub, and local VSCode.
- GitHub: Reliable version control and code history retention.
- Local VSCode: A comfortable and feature-rich editing environment.
- Google Colab: A powerful execution environment with access to GPUs/TPUs.
By leveraging the strengths of each tool and adhering to proper operational rules, you can create a flexible and safe development workflow that minimizes the risk of conflicts.