Chapter 1.2.3


Creating a Git Project

Let's create a new directory for our sample Git project.

In Mac or Linux, open Terminal and type the following commands (exclude the comments from each line, these are the ''#' character and words following them):

cd # takes you to home directory
mkdir test1 # creates subfolder called 'test1'
cd test1 # takes you to 'test1' subfolder

In Windows, open Command Prompt and type:

cd %HOMEPATH%
mkdir test1
cd test1

Now, put the directory under git revision control:

git init

You have successfully created your first Git repo!

Creating a File and Making Changes

Now, let's add a file, make some changes, and commit them to Git.

Type the following command into your Terminal to create a text file that has the words 'This is some text' inside:

echo This is some text > myfile.txt

Let's see what git thinks about what we're doing:

git status

The git status command reports that myfile.txt is 'Untracked'. Let's allow git to track our file by adding it to the staging area.

git add myfile.txt

Run git status again. It now reports that my file.txt is "a new file to be committed." Let's commit it and type a useful comment:

git commit -m "Added myfile.txt program."

We have successfully put our first coding project under git revision control!

Pushing Our Sample Project to GitHub

When programmers talk about "pushing" projects to GitHub, they essentially mean that they have sent their changes to the cloud where other people can see the code they have written. We will push our sample project to GitHub so that it is accessible to others.

  1. Create a new repository on GitHub. To avoid errors, do not initialize the new repository with README, license, or gitignore files. You can add these files after your project has been pushed to GitHub.

  2. At the top of your GitHub repository's Quick Setup page, click to copy the remote repository URL. Copy remote repository URL field

  3. In Terminal or Command Prompt, add the URL for the remote repository where your local repository will be pushed.

$ git remote add origin  <REMOTE_URL> 
# Sets the new remote
$ git remote -v
# Verifies the new remote URL
  1. Push the changes in your local repository to GitHub.
$ git push -u origin main
# Pushes the changes in your local repository up to the remote repository you specified as the origin

You have now succesfully pushed your project to the cloud!