How To Create a Git Repository
Last Updated on 2023-02-14
[#quick-reference]Quick reference to setup a repository[#quick-reference]
To get started with [.inline-code]git[.inline-code], you need to make sure you have it installed on your computer. You can download it from the official website and follow the instructions there to install it.
[#new-repository-locally]Create a new repository locally[#new-repository-locally]
To then create a new repository you need to first create a directory or folder for your project. In the command line with can be done using the [.inline-code]mkdir[.inline-code] command followed by the name of the folder you want to create:
You can then navigate into that folder using the command line [.inline-code]cd[.inline-code] command. This allows you to change directories:
Once you are in that directory a [.inline-code]git[.inline-code] repository can be initialised using the [.inline-code]git init[.inline-code] command. This will be:
This will create a new repository based on the folder you are currently in. A [.inline-code].git[.inline-code] folder should appear in the directory. Note: [.inline-code].git[.inline-code] will initially be hidden; you can use the [.inline-code]ls -a[.inline-code] command to show all hidden files in a directory.
This will mean you have a git repository for your project and can start making branches and tracking changes on files you’ve added using [.inline-code]git add[.inline-code] and [.inline-code]git commit[.inline-code].
[#create-repo-existing-folder]Create a repository in an existing folder[#create-repo-existing-folder]
If instead you already have folder that you have created you can navigate to that directory:
Then use the [.inline-code]git init[.inline-code] command as before:
If you then want to make sure that the repository is tracking your files you first need to use the [.inline-code]git add[.inline-code] command to add them to the repository. To add all the current files and folders you use:
After adding the files you then need to commit the changes to the repository. This is similar to taking a snapshot of your repository at a particular point in time. To commit changes use the command:
For this, replace “initial commit” with a meaningful commit message that would describe the changes that you have made or the state of the current repository.
[#connect-to-remote-repo]Connect to a remote repository[#connect-to-remote-repo]
First you need to make a remote repository. To do this, we’ll use GitHub as an example, but other remote repository options like GitLab and BitBucket would work similarly. On GitHub, create a new empty repository. Give it a name and set whether you want to make it public or private. This should look something like this:
And the new repository should look something like this:
You can then link your local repository to the remote one using the command:
Where the remote url is found on your remote repository. This will often take the form of [.inline-code]email@example.com:<your_username>/<your_repo_name>.git[.inline-code]. This will then connect your local repository to your remote one. You can verify that this connection has been made using the command:
Which should print the remote URL that was specified.
When pushing your work to the new remote repository you need to then set up where exactly you want to push to. This can be done using the command:
This command creates a branch named [.inline-code]main[.inline-code] in our remote repository (if it doesn’t exist) and sets our local [.inline-code]main[.inline-code] branch to track and send changes to this remote branch. To then push your changes to the remote repository, you use the command:
In the case of an older repository, where the default primary branch may be named [.inline-code]master[.inline-code] instead of [.inline-code]main[.inline-code], the command would be: