Introduction
If you have a Python or any coding/writing project on your local machine and want to share it on GitHub, you can do so using Git and a personal access token. Let’s create a local Git repository, set up a GitHub repository, and push your code to GitHub!
Prerequisites
To begin, you must create a GitHub account if you don’t have it yet.
Next, you will have to ensure that Git is installed on your computer. I suggest installing GitGit since you can use Bitbucket, GitLab, or any provider. I prefer using Git since I use both GitHub and BitBucket.
You can easily download the Git package from the Downloads webpage. To test that Git is installed correctly, check its version with:
git version
git version 2.34.1
If you like to work with your repositories visually, you can also download any graphical user interface from GUI Clients.
Alternatively, however, it is the most complicated installation. You can install the GitHub Command Line Tool (CLI), an open-source tool that allows you to use GitHub from your command line, helping you save time and avoid context switching. If you don’t yet have the GitHub CLI installed on your system, check the CLI installation instructions.
Things to do on GitHub
So, we have our project stored locally, and we want to keep it on GitHub. To do that, we will have to access GitHub securely with the personal access token, create a new GitHub repository to push our local code and share it when collaborating with our co-workers.
1. Generate a Personal Access Token
Personal Access Tokens are gibberish strings that act as passwords to access and work with GitHub repositories via the command line in the Terminal.
I have a well-detailed post The Token Way to GitHub Security explaining how to create personal access tokens for GitHub.
The process is relatively straightforward:
- Go to your GitHub settings (click on your profile picture, then Settings).
- Select Developer settings > Personal access tokens (classic).
- Click Generate new token.
- Set a descriptive name and choose the repo scopes you need.
- Click Generate token and copy it. You will use this token instead of your password.
For more information, see the GitHub documentation on creating a personal access token.
2. Create a New GitHub Repository
- Go to GitHub and click on New (or +) to create a new repository.
- Give your repository a name (e.g.,
my-python-project
,todo_app
) and optionally add a description. - Choose if you want it to be public or private.
- Do not initialise with a README, license, or
.gitignore
(since you already committed locally). - Click Create repository.
Things to do locally
Indeed, you will have to have your Python (or any other programming language project, or even your poem, coursework on anything, preferably text-based, since GitHub is a version control tool that is handy for tracking any text changes in time) project folder on your local machine.
1. Initialise a Local Git Repository
Navigate to your project folder in the terminal or command prompt and run:
cd /path/to/your/python-project
git init
This initialises an empty Git repository in your project folder like this:
Initialized empty Git repository in /Users/elena/Documents/git/my_todo_app/.git/
2. Create .gitignore (optional)
Sometimes, we have to exclude specific files from GitHub. For instance, we want to exclude the folder’ .venv` (with all the virtual environment files) from Git.
For this, add an entry to .gitignore
file:
.venv/
This tells Git to ignore the entire .venv
directory.
You must create the .gitignore
file if it does not exist.
I like using nano for quick edits :)
3. Stage and Commit Your Code
Add your files to the staging area and commit them:
git add .
git commit -m "Initial commit v1"
Replace "Initial commit v1"
with a more descriptive message if desired.
[master (root-commit) 658994d] Initial commit v1
5 files changed, 136 insertions(+)
create mode 100644 .gitignore
create mode 100644 app.py
create mode 100644 templates/edit.html
create mode 100644 templates/index.html
create mode 100644 todo.db
4. Connect Local Repository to GitHub
Copy the repository URL (HTTPS) from GitHub while adding your personal access token we generated above:
git remote add origin https://<your_personal_access_token>@github.com/<your-username>/<your-repo-name>.git
Replace <your-username>
and <your-repo-name>
with your GitHub details.
git remote add origin https://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@github.com/edaehn/todo_app.git
5. Push Your Code to GitHub
Push your local commits to the GitHub repository:
git push -u origin master
Since we used our personal access token, we will not be prompted for a GitHub username and password. Everything will go smoothly:
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 10 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (8/8), 2.31 KiB | 2.31 MiB/s, done.
Total 8 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), done.
To https://github.com/edaehn/todo_app.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
Verify Your Repository
- Go back to GitHub and visit your newly created repository page.
- Confirm your files are visible and that your commit history is as expected.
Conclusion
You have successfully created a Git repository for your excellent, possibly, Python project and uploaded it to GitHub. Remember to keep your personal access token secure and use Git commands to maintain version control effectively. Please don’t commit or publish your access tokens openly :)
Did you like this post? Please let me know if you have any comments or suggestions.
Git posts that might be interesting for youReferences
![]() |
About Elena Elena, a PhD in Computer Science, simplifies AI concepts and helps you use machine learning.
|