git-guide/getting-code.md

104 lines
4.6 KiB
Markdown
Raw Normal View History

2024-05-03 11:22:01 +00:00
# Creating and Cloning a Repository
## Setting up a new repository
2024-05-03 11:22:01 +00:00
Lets look at how you can set up a new remote repository on Gitlab/Github, so you are ready to start committing your code. the easist way is to create a new repository in Gitlab/Github, clone it locally then start adding your files to it. We'll look at this method first.
2024-05-03 10:16:51 +00:00
2024-05-03 11:22:01 +00:00
<details>
<summary>Using Gitlab</summary>
#### Gitlab
2024-05-03 10:16:51 +00:00
2024-05-03 11:22:01 +00:00
- Open Gitlab in your browser and log in
- Now click **new project** on the right hand side of the interface
2024-05-03 10:16:51 +00:00
2024-05-03 11:22:01 +00:00
![Create a New Project](./img/create-new-project.png)
2024-05-03 10:16:51 +00:00
2024-05-03 11:22:01 +00:00
- You'll now be asked what template do you want to use, I recommend a blank project unless your use case fits with the other options
![Select Template for Repo](./img/select-project-type.png)
- Now it's time to give your project a name. You can also set if the project is public or private at this time (although this can be changed later)
![Setup your project](./img/setup-project.png)
- Once done click **create project** at the bottom of the page
- You'll be taken to the next page which shows you a bare bones repository. (It should have a README.md file if you didn't change the setting to create one)
![New repository is now created](./img/project-created.png)
#### Get the project URL
- The next step is to get the URL for the git repository you want to clone. You can do this by going to the green **code** button and clicking it.
- If you intend to commit code back select the SSH URL.
2024-05-03 13:47:50 +00:00
![Get the SSH URL](./img/new-repo.png)
2024-05-03 11:22:01 +00:00
> If you want to just pull the code down to use locally you can use HTTPS, this is especially useful if you don't have permissions on the repository.
2024-05-03 13:47:50 +00:00
</details>
2024-05-03 13:48:39 +00:00
2024-05-03 13:47:50 +00:00
<details>
<summary>Using Github</summary>
- login into Github
- Click new on the left hand side of the page
![Create a new repository](./img/gh-new-repo.png)
- Now fill out the name of the repository in name and change any settings you need.
![Setup the project](./img/gh-setup-project.png)
> Make sure its a unique name in your namespace.
- Your repository is now created. You can copy the URL for it from the information in **quickstart** as you'll need this for the next section
![Repository created](./img/gh-new-repo.png)
2024-05-03 11:22:01 +00:00
</details>
2024-05-03 11:29:44 +00:00
2024-05-03 11:22:01 +00:00
### Clone the new repository
2024-05-03 11:29:44 +00:00
Now you have the URL you can go back to your terminal and clone the repository.
2024-05-03 11:22:01 +00:00
```bash
git clone git@gitlab.com:ric_harvey/demo-project.git
```
2024-05-03 11:29:44 +00:00
you are now ready to get going committing code! Head over to the [commit.md](./commit.md) file for information on what to do next.
2024-05-03 11:22:01 +00:00
### Adding existing files to a git repository
2024-05-03 11:29:44 +00:00
Now if you have already been working hard and you have files already you can import these files. By following the following instructions. You'll still need that SSH URL you copied.
2024-05-03 11:22:01 +00:00
```bash
2024-05-03 11:29:44 +00:00
cd existing_directory
git init
2024-05-03 11:22:01 +00:00
git remote add origin https://gitlab.com/ric_harvey/demo-project.git
git branch -M main
2024-05-03 11:29:44 +00:00
git add .
git commit -m "first commit"
2024-05-03 11:22:01 +00:00
git push -uf origin main
```
2024-05-03 10:16:51 +00:00
2024-05-03 11:29:44 +00:00
This will now sync all your your files up to the remote repository.
2024-05-03 15:06:40 +00:00
## Cloning an existing repository
If you want to pull code from an existing repository you have two options. SSH and HTTPS, if you have permissions on the project (you can push code to it) you should use SSH, if you are just pulling some code to run then HTTPS is fine but default to using SSH if you can.
The repository will have a code button on the right hand side of the page that when you click it, it will give you the options of cloning by SSH or HTTPS, you my also get options to download the repository as a zip or tgz file also.
![Clone Repository](./img/clone-repo.png)
Once you have the URL you can run **clone** command.
```bash
git clone <COPIED_GIT_URL>
```
Now you can change directory into that new folder and start working.
---
2024-05-10 16:36:48 +00:00
<div style='text-align: right;'>
##### Follow me for more guides
2024-05-10 16:36:48 +00:00
[<img src="./img/mastodon.png" width="4%">](https://awscommunity.social/@Ric) [<img src="./img/gitlab.png" width="4%">](https://gitlab.com/ric_harvey) [<img src="./img/linkedin.png" width="4%">](https://www.linkedin.com/in/richarvey/) [<img style="height:25px!important;margin-left:3px;margin-bottom:4px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/cc.svg?ref=chooser-v1" alt=""><img style="height:25px!important;margin-left:3px;margin-bottom:4px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/by.svg?ref=chooser-v1" alt=""><img style="height:25px!important;margin-left:3px;margin-bottom:4px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/nc.svg?ref=chooser-v1" alt="">](href="https://creativecommons.org/licenses/by-nc/4.0/?ref=chooser-v1)