Creating a new repository is not difficult, but does not happen frequently. Normally you will be contributing to a repository that has already been created on GitHub.
There are two main ways of creating a new repository: GitHub-first, or local-first. They are only slightly different, so which method you use is largely down to preference, but the focus here is on GitHub-first as overall it is simpler.
GitHub First
This way you will create the GitHub repository first. Then you will clone the repository to get a local copy. Cloning and working in this way is covered in the Git & GitHub Working Practices lesson.
Create the remote on GitHub
To create a new repository on GitHub:
- Under the “+” menu in the top-right corner of any GitHub page, click New repository
- Choose an appropriate owner, name, and visibility
- Private: only you
- Internal (organisations only): read permissions to anyone in the organisation
- Public: read permissions to anyone
- Tick the box to initialise with a README file (unless creating a local repository first)
Clone the GitHub remote
$ git clone git@github.com:<organisation>/<repository>.git
$ cd <repository>
Local First
If you already have files you wish to version control this approach is best:
- Change into the directory containing files you want to version control
- Initialise the directory as a git repository
$ git init
- Make an initial commit, where “Repository Name” is replaced with a suitable name for the repository
$ echo "# Repository Name" > README.md
$ git add README.md
$ git commit -m "Initial commit"
- Check if you are on the
main
branch withgit status
or your terminal prompt if you have Git Autocomplete setup. Rename the default branch frommaster
tomain
(master is considered outdated terminology) if your current branch ismaster
.
$ git branch -M master main
- Create a new GitHub repository with no files.
- Set up links to the new GitHub repository
git remote add origin git@github.com:<organisation>/<repository>.git
git push
Adapted from the work of Violet Sherratt (Met Office).