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 (you don't have to include anything after #—they are just comments and don't affect anything):

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

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 contains '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!

To recap use

  • git status to learn about the status of your project (that is, which files were created/modified/deleted)
  • git add {filename} in order to track a file (you have to do this every time you modify it). If you don't want to list each modified file manually, simply type git add . (the period is a wildcard for all files).
  • git commit -m "{message}" in order to create a new "checkpoint" for your project which will include the entire current state of the repository.

Pushing Our Sample Project to GitHub

When programmers talk about "pushing" projects to GitHub, they essentially mean that they are synchronizing their changes with a repository hosted in the cloud where other people can see the code they have written. We will push our sample project to GitHub:

  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

From here on, you will need to type only git push whenever you want to synchronize your Github repository with your local repository.

You have now succesfully pushed your project to Github!