# Git and GitHub Practice Session

## Assignment

In this project, we’ll walk through the basic Git workflow that you will use in all your projects.

**Create the Repository**

1. Create a new repository by clicking the button shown in the screenshot below.

![](https://4042228299-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRebciU3NcuLgsX3ijf%2F-MaOkhFrEJ22zVjuJRH-%2F-MaOmGvD2c5pZEGKXHfj%2Fimage.png?alt=media\&token=b7b4b3c0-373f-41d4-9f49-689da3a55071)

&#x20;**2.** Give your repository the name “git\_test” in the repository name input field, and create the repository by clicking the green “Create repository” button at the bottom of the page.

**3.** This will redirect you to your new repository on GitHub. To copy this repository onto your local machine, select the HTTPS option and copy the line next to it.

![](https://4042228299-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRebciU3NcuLgsX3ijf%2F-MaOkhFrEJ22zVjuJRH-%2F-MaOmf1yI-7tReRBRJwz%2Fimage.png?alt=media\&token=5da94e24-6c76-44d2-a634-9f4bcfc8d047)

**4.** In the command line on your local machine, navigate to where you want to store this project, and then clone your repository on GitHub onto your computer with `git clone` followed by the URL you copied in the last step. The full command should look similar to `git clone https://github.com/USER-NAME/REPOSITORY-NAME.git`.

**Note:** In the image below SSH link is used instead of HTTPS which is not important for us at this point. You can continue with HTTPS link.

![](https://4042228299-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRebciU3NcuLgsX3ijf%2F-MaOkhFrEJ22zVjuJRH-%2F-MaOmlw8VU5Z1bQpd6k1%2Fimage.png?alt=media\&token=e6b4b7ec-dace-43ef-9e20-6d258ea32e5c)

**5.** That’s it! You have successfully connected the repository you created on GitHub to your local machine. To test this, you can `cd` into the new **git\_test** folder that was downloaded and then enter `git remote -v` in your command line. This will display the URL of the repository you created in GitHub, which is the remote for your local copy. You may have also noticed the word **origin** at the start of the `git remote -v` output, which is the name of your remote connection. The name “origin” is both the default and the convention for the remote repository, but it could have just as easily been named “party-parrot” or “dancing-banana”. (Don’t worry about the details of origin for now; it will come up again near the end of this tutorial.)&#x20;

![](https://4042228299-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRebciU3NcuLgsX3ijf%2F-MaOkhFrEJ22zVjuJRH-%2F-MaOmx3m9vzpR7guVBnV%2Fimage.png?alt=media\&token=91388d9b-7403-45ae-9723-86aa2a91b794)

**Use the Git Workflow**

Create a new file in the `git_test` folder called “README.md” with the command `touch README.md`.

![](https://4042228299-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRebciU3NcuLgsX3ijf%2F-MaOkhFrEJ22zVjuJRH-%2F-MaOnDiXtaKFCRJiUf5f%2Fimage.png?alt=media\&token=d21e69c9-b4d7-4e48-a6dd-908fcc43ed2e)

Type `git status` in your terminal. In the output, notice that your README.md file is shown in red, which means that this file is not staged.

![](https://4042228299-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRebciU3NcuLgsX3ijf%2F-MaOkhFrEJ22zVjuJRH-%2F-MaOnG3US7JD8IV25MNS%2Fimage.png?alt=media\&token=67c8ecbd-9bdc-4e4f-9688-09cb04237ed5)

Type `git add README.md`. This command adds your README.md file to the staging area in Git. Now, type `git status` again. In the output, notice that your file is now shown in green, which means that this file is now in the staging area.

![](https://4042228299-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRebciU3NcuLgsX3ijf%2F-MaOkhFrEJ22zVjuJRH-%2F-MaOnImzz6zPnhiicPrJ%2Fimage.png?alt=media\&token=0a24f98e-2a1f-4e3e-bfad-e699d3321c3a)

Type `git commit -m "Add README.md"` and then type `git status` once more. The output should now say, “*nothing to commit, working tree clean*”, indicating that your changes have been committed. Don’t worry about the `upstream is gone` message you may see, this is totally normal and only showing because your cloned repository currently has no branches. It will be resolved once you have followed the rest of the steps in this project.

![](https://4042228299-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRebciU3NcuLgsX3ijf%2F-MaOkhFrEJ22zVjuJRH-%2F-MaOnLSnzvcc91utQ6hc%2Fimage.png?alt=media\&token=6c72b350-22fb-4c85-a622-64c93d818d16)

Type `git log` and look at the output. You should see an entry for your “Add README.md” commit. You will also see details on the author who made the commit and the date and time for when the commit was made.

**Add Another File**

Create a new file in the `git_test` folder called `hello_world.txt`. In the terminal, type `git status`, and notice `hello_world.txt` is not staged.

![](https://4042228299-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRebciU3NcuLgsX3ijf%2F-MaOkhFrEJ22zVjuJRH-%2F-MaOnQNrMH-FhJ2hfuuS%2Fimage.png?alt=media\&token=eec3c52b-61c3-4826-b048-fca7e109c9ee)

Open README.md in your text editor of choice and add the text “This is (YourUsername)’s first git project!” and then save the file.

![](https://4042228299-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRebciU3NcuLgsX3ijf%2F-MaOkhFrEJ22zVjuJRH-%2F-MaOncrBmOriApvjHsfu%2Fimage.png?alt=media\&token=dd643ab4-40c2-4351-9aeb-6fdf61b033e4)

Back in your terminal, type `git status`, and notice that README.md is now shown as modified, and not staged or committed. This is because you made a change to it, and it is already a tracked file.

![](https://4042228299-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRebciU3NcuLgsX3ijf%2F-MaOkhFrEJ22zVjuJRH-%2F-MaOnh-6MHFbnVbR8dZA%2Fimage.png?alt=media\&token=d1cbb6a9-06a9-4714-ab60-e08c02388272)

Add README.md to the staging area with `git add README.md`.

Can you guess what `git status` will output now? README.md will be displayed in green text, while hello\_world.txt will still be in red. This means that only README.md has been added to the staging area.

![](https://4042228299-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRebciU3NcuLgsX3ijf%2F-MaOkhFrEJ22zVjuJRH-%2F-MaOnku0V-PmRhvUf63V%2Fimage.png?alt=media\&token=58d80ddb-d0b7-4730-a795-ef2afc61ea6b)

Now, add hello\_world.txt to the staging area with a slightly different command: `git add .`, where the full stop means to add all files **in the current directory** that are not staged. Then, type `git status` once more, and everything should now be in the staging area. *(Note: You can use `git add -A` to add ALL unstaged files to the staging area within the repository)*

![](https://4042228299-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRebciU3NcuLgsX3ijf%2F-MaOkhFrEJ22zVjuJRH-%2F-MaOnp9OkL8rnKnKkUJR%2Fimage.png?alt=media\&token=4e508f6a-73ca-40c7-8d75-32976f6c3772)

Finally, let’s commit all of the files that are in the staging area and add a descriptive commit message `git commit -m "Add hello_world.txt and edit README.md"`. Then, type `git status` once again, which will output “*nothing to commit*”.

![](https://4042228299-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRebciU3NcuLgsX3ijf%2F-MaOkhFrEJ22zVjuJRH-%2F-MaOnsRDpOStl7xtcICo%2Fimage.png?alt=media\&token=56fab2aa-da55-44d5-b501-e703713f8963)

Take one last look at your commit history by typing `git log`. You should now see two entries.

**Push Your Work to GitHub**

Finally, let’s upload your work to the GitHub repository you created at the start of this tutorial.

Type `git push origin main`.

![](https://4042228299-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRebciU3NcuLgsX3ijf%2F-MaOkhFrEJ22zVjuJRH-%2F-MaOo-TfyC9My8SyOslA%2Fimage.png?alt=media\&token=e0c848cd-a610-48fe-808d-dcb18e5004c3)

Type `git status` one final time. It should output “*nothing to commit, working tree clean*”.

![](https://4042228299-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRebciU3NcuLgsX3ijf%2F-MaOkhFrEJ22zVjuJRH-%2F-MaOo3t6mgGoKdXEcTSz%2Fimage.png?alt=media\&token=9427ed74-2816-4f8e-b89c-be8163c66161)

When you reload the repository on GitHub, you should see the README.md and hello\_world.txt files that you just pushed there from your local machine.

![](https://4042228299-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRebciU3NcuLgsX3ijf%2F-MaOkhFrEJ22zVjuJRH-%2F-MaOo8IRKN9ZXCIsG9O7%2Fimage.png?alt=media\&token=f1c9ed6c-b079-4969-916c-fba9d66097bc)

### Conclusion

The main take away from the past lessons is how to use Git and GitHub for your projects. You now have this very powerful skill that will help you immensely when we get into the coding projects. You will be able to share your work with others for code reviews and to get help with your code if you’re stuck.

### Extra Practice

Follow the instructions on this [link](https://github.com/Migracode-Barcelona/learn-git) for more practice.
