Basic Git Commands to get started - 14 days of Git

So far on my 14 days of Git learning journey I have dug into:

Now it's time to start using Git and get familiar with some of the basic commands.

Git clone

Git clone is the command that will help you download an existing remote repository (like GitHub for example) to your machine.

For example if you head over to any GitHub repository and click on the green Code button.  It will display a URL.  Take a copy of that URL.

GitHub repository

Open up your command line tool and type in:

git clone https://repository-url.link

And Git will take a copy, or clone the entire project from the remote source to your machine.

Git Clone in action

Git add

When we work on a local copy of a repository we will be creating, modifying, or delete files.  When we do that we need to add those changes to our next commit so they are copied to the remote repository.

Git add starts to prepare our changes ready to be saved and sent to the remote repository.

We can add a single file with the command:

git add filename

Or we can add all the files and changes we made with the command:

git add -A
Git add command

Your changes are now ready to be committed.

Git commit

There comes a point when you are working on your local repository that you want to save your changes.

Git commit is like setting a checkpoint.  You can come back to this checkpoint at a later point if you need to.

When we issue the git commit command we need to issue a message to explain the changes we have made.  This will help you or the next person along understand what has changed.

An example of using git commit can be seen below:

git commit -m "I have made changes to the code to add a new feature"
Git commit command

Git push

Once we have committed our changes we need to send them to the remote repository.  We can do this using the push command.

The git push command uploads the commits you have made to the remote repository.  Only committed changes will be uploaded.

There are several ways to use the git push command, the most common will be:

git push
Git push command

Git pull

Git pull is the command you would use to get any updates from the remote repository.

This command actually does two things in the background. It is a combination of two other commands, git fetch and git merge.

  • First of it it gets the updates from the remote repository (git fetch)
  • Applies the changes to your local copy of the repository (git merge)
git pull

There are times when you will use this command it will throw up errors or conflicts that you need to resolve but it will pull down any changes for you.

Git init

Git init is a great command to be aware of it.  IT can convert an existing project or folder into a Git repository.

Most git commands are not available until a repository is initialised, so typically this is the first command you will run when starting a new project on your local machine.

When you use git init it create a .git subdirectory in the working directory.  This contains all the necessary Git metadata for the new repository.

You can use git init in a couple of way.  

If you have an existing folder structure with files and code within it that you want to turn it into a git repository you can issue the command:

git init
Git init command

If you haven't created a folder or started creating any code you can issue the following command to create the folder and initialise it as a git repository:

git init NewProject

14 days of Git

These are some of the basic Git commands that you should know.  They really are the basics for getting involved with Git.

The next step of 14 days of Git is to dig deeper into the commands that we can use with Git and why we need to know them.  Be sure to subscribe and join us for that step in the learning journey!

You can follow along here: https://github.com/weeyin83/14daysofgit