How to Push Your Flutter Code to GitHub Using Git

Pushing your Flutter project to GitHub is essential for version control and collaboration. Whether you’re a beginner or a seasoned developer, following the right steps ensures your code is safely hosted on GitHub. In this blog, we’ll walk you through the entire process of pushing your Flutter code to GitHub, addressing common issues like SSH authentication errors.


Prerequisites

  1. Git Installed: Ensure Git is installed on your machine. If not, download Git and follow the installation guide.
  2. GitHub Account: Create an account on GitHub if you don’t have one.
  3. Flutter Project: Make sure you have an existing Flutter project ready to push.

Step 1: Initialize Git in Your Flutter Project

Open your terminal or command prompt, navigate to your Flutter project directory, and initialize a Git repository:

cd /path/to/your/flutter/project
git init

This command creates a .git folder in your project directory, marking it as a Git repository.


Step 2: Create a README File

A README.md file provides an overview of your project. You can create it directly in the terminal:

echo "# My Flutter Project" >> README.md

Or use any text editor to manually create and save the file.


Step 3: Stage and Commit Your Files

Stage all your project files for the first commit:

git add .

Commit the changes with a meaningful message:

git commit -m "Initial commit"

Step 4: Create a GitHub Repository

  1. Log in to your GitHub account.
  2. Click on the New button or navigate to https://github.com/new.
  3. Fill in the repository name and description.
  4. Choose whether the repository should be public or private.
  5. Click Create Repository.

Step 5: Connect Your Local Repository to GitHub

To connect your local repository to the one you just created on GitHub, you need the repository URL. For SSH, it will look something like this:

git remote add origin [email protected]:your-username/your-repository.git

Replace your-username and your-repository with your actual GitHub username and repository name.


Step 6: Push Your Code to GitHub

Push your code to the main branch of your GitHub repository:

git branch -M main
git push -u origin main

Common Issues and Fixes

1. Permission denied (publickey) Error

This error occurs when Git cannot authenticate with GitHub using SSH. To fix this:

  1. Check for an SSH Key: ls ~/.ssh If you don’t see id_ed25519 or id_rsa, generate a new SSH key: ssh-keygen -t ed25519 -C "[email protected]"
  2. Add the SSH Key to the SSH Agent: eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_ed25519
  3. Add the Key to GitHub: Copy the public key: cat ~/.ssh/id_ed25519.pub Go to GitHub SSH Settings, click New SSH Key, and paste the key.
  4. Test the SSH Connection: ssh -T [email protected]

2. Switching to HTTPS

If SSH doesn’t work, use HTTPS instead:

  1. Update the remote URL: git remote set-url origin https://github.com/your-username/your-repository.git
  2. Push using: git push -u origin main

Step 7: Verify Your Push

Visit your repository URL on GitHub, and you should see all your Flutter project files successfully uploaded.


Conclusion

Congratulations! You’ve successfully pushed your Flutter project to GitHub. By using Git and GitHub, you now have version control, backup, and collaboration capabilities for your project. If you encounter any issues, revisit the steps or explore GitHub’s official documentation for further guidance.

Happy coding! 🚀