How to Create a Repository Using Git and Github
In this tutorial, we are going to learn how to create a new Git repository for a project. To follow this tutorial you should have a terminal installed if you are using Windows and the Git software should be installed. In not please read my tutorial on how to install and configure Git.
Creating a New Git Repository
The first step is to open a new terminal window and navigate to the project you would like to turn into a Git repository. To do this use the cd command.
cd /Users/johnhoward/Dropbox/Python/VideoDownloader/
Replace the path with the path to your project.
Now your current working directory is the root of your project we can initialize an empty Git repository using the git init
command. Note - change directory to the relevant folder within the project if you don't want to track the entire thing.
git init
Initialized empty Git repository in /Users/johnhoward/Dropbox/Python/VideoDownloader/.git/
If everything when OK you will get a message saying an empty Git repository has been initialised. You can verify this by typing ls -a
command and verifying that a .git
folder exists.
ls -a
.DS_Store .git .idea __pycache__ colors.py download.py downloads
Sending Files to the Staging Level Using Git add
If your project already contained files or you have made changes since creating a Git repository, you will want to add them to the staging level. The staging level is all the files that will be committed when the next commit command it ran. You can add files to the staging level using the Git add
command followed by a list of the files you would like to add.
git add colors.py download.py
Now we are ready to commit a version of the files. In my example, colors.py
and download.py
will be ready to commit.
Committing Changes
Once the Git add
command has been run and you are happy to commit a version. Type git commit
followed by the -m
(message) flag and provide a short summary of your commit inside quotation marks.
git commit -m "Version 1 complete."
[master (root-commit) bf207b8] Version 1 complete.
2 files changed, 85 insertions(+)
create mode 100644 colors.py
create mode 100644 download.py
If everything went OK you should get a message confirming the commit has been made.
Add The Repository to Github
If you wish, you can now send your repository to Github. Login to your Github account and create a new repository using the menu on the top-right of the page. Follow the instructions on the add repository form and create it.
Important - don't create any files (README.md
.etc) in the Github repo creation or you will get an error when trying to push your local version.
Now back in your local project folder in terminal do the following:
Create a README.md
file
echo "# MyRepoNameOnGithub" >> README.md
Add the README.md
to the Staging Level
git add README.md
Commit the new change
git commit -m "first commit"
Create a master branch
git branch -M main
Add the address to your repo as a remote origin
git remote add origin https://github.com/username/your_repo
Push the commit to the Github Repository
git push -u origin main
Conclusion
You now know how to create a Git repository, make commits and push them to Github.