How to Start Using Git for Beginners

How to Start Using Git for Beginners

Overview

This article will explain how to use a few commands to get started using Git in the Terminal. Those commands are:

  • git init

  • git add

  • git commit

  • git branch

  • git checkout

  • git remote

  • git push

  • git pull

Materials

  • Computer

  • Git (1)

  • GitHub and have a GitHub account (2)

  • Terminal or PowerShell or Command Prompt

Downloading Git and Making a GitHub Account

Git is a version control software that makes it easier to store files in a repository and also add, delete, update or revert file changes in the repository with Git commands. The repository can be accessed through the Terminal or on their GitHub website. To download Git, visit their official website and download the right installation files for your operating system (1). GitHub is the website where there is a more friendly user interface that can also help interact with the repository that way. Visit the GitHub website to make an account and make repositories (2). After making a repository on the GitHub website, a GitHub URL is made with the repository.

Make a Repository in GitHub

  • When making a repository in GitHub, log in into your account and click the “New“ button

  • Another way to make a repository from the home page, is in the top corner after you can click your profile picture, click “Your Repositories” from the drop-down menu.

  • After clicking “Your Repositories”, click the “New” button.

  • After clicking “New”, fill out the information in order to make a new repository.

    • Under “Repository name” you must give every new repository a unique name and its name cannot be shared with another repository.

    • The repository can be selected as “Public” which allows other people to view it or “Private” which makes only the creator or you to view the repository

    • It is recommended to “Add a README file”. This file has a “.md” extension and a description can be written and displayed for the repository. However, when an empty repository is made or if the repository has no files, it shows instructions how to add files to the repository

    • Once done, click the “Create Repository“ button to make the repository

  • Once again, an empty repository shows instructions how to add files to the repository

  • After taking notes of these commands, it is recommended that every repository has a “README.md” file, and this file can be created from GitHub or from the Terminal.

Terminal Commands

In the Terminal, change directory to a specified directory, preferably an empty or new directory on your computer and then run “git init”. “git init” initializes a git directory (3) to run git commands.

git init

Next, it is time to add a file to git. To make a file in the Terminal, use either the “touch” or “nano” to make a new file, write a few sentences and then save it afterwards with CTRL + S. To download nano on Windows, first install the chocolatey package (4) and then use the chocolatey package to download nano (5). Don’t forget to include its extension.

If there is a file already in your directory, then proceed by running the next git command: “git add README.md”. This command adds every specified file after “git add” from the desktop directory onto the git directory. Specifying files are also case-sensitive. The command “git add .” makes sure to add every file from the desktop directory to the git repository, since the “.” adds every file.

git add README.md

Next, is to write a message when uploading files onto the git repository. The command “git commit -m …” allows to write a message, which is useful to explain changes in certain times of the repository.

git commit -m "Message"

Press “Enter” after writing the commit command to see what changes, or in this case, which files or insertions will occur.

Usually, when a Git repository is made, there is a main branch. If files are wanted to be added onto the main branch or another named branch, first create the branch in the Terminal with “git branch -M …”, where … is the name of the branch.

git branch -M "name_of_branch"

There can be more than one branch in a repository. To switch from one branch to another branch, use git checkout …, where … is the name of the branch again.

git checkout "name of branch"

Specify, where the files will be uploaded to with the “git remote add origin …” command, where … is the HTTPS URL of the repository, which can be found on GitHub. If there are files already in the repository, the layout will be different to find the HTTPS URL. Click the green “Code” button and the HTTPS URL will be there to copy from.

git remote add origin "Repository HTTPS URL"

Lastly, to upload the “README.md” file onto the Git repository, use “git push -u origin …”, where … is the name of the branch

git push -u origin "name of branch"

To retrieve data from any branch and edit it onto your computer, use the command “git pull origin …”, where … is the name of the branch

git pull origin "name_of_branch"

Sources

https://git-scm.com/ (1)

https://github.com/ (2)

https://git-scm.com/docs/git-init (3)

https://chocolatey.org/install (4)

https://community.chocolatey.org/packages/nano (5)