How to use Git - The Basics

The Basics

The following commands should be executed in the command prompt on your computer.

  • To start using git, you need to make sure you have it installed (you can find instructions here) and that you've signed up for a GitHub account.

  • You can create a new git repository (collection of source code files) from your own file system by navigating to the correct folder and using the command

git init
  • You can also clone a repository from the internet, by specifying the URL of the remote origin (where the files where initially downloading from)

git clone <repository-url>
  • You can set up Git with your name and email so that you'll be identified as the author for each commit

git config --global user.name "<your-full-name>"
git config --global user.email "<your-email-address>"

Making changes to your code

Assuming you're in the correct file directory, here's how you can add changes to your code to Git

  1. Add your file(s) to the staging area from the working directory. The working directory is the area you're currently working in and the changes you've made to your code on your file system. The staging area is when Git starts tracking and saving changes that occur in your files.

git add <files>

2. Commit your changes in the staging area to your local repository. This is everything in your .git directory, containing all your commits. It's the area that saves all the changes to your code. It's good practice to add a commit message which describes the changes that you've made.

git commit -m <your-commit-message>

3. Push your changes from your local repository to your remote repository. This means that your code will be viewable on GitHub and stored in the cloud. Anyone with access to your remote repository can view these changes. This command below will upload your branch to the origin, which is the remote repository you created earlier.

git push origin <branch-name>

You can update your local repository to the newest version in the remote repository using the pull command.

git pull 

The above command updates changes in your current branch. If you want to pull from a specific branch,

git pull <remote-name> <branch-name>

For example,

git pull origin master

Using git pull also merges remote changes, which could lead to merge conflicts. We'll explore this on the next page.

Congratulations - you've made a great start on the basics of using Git! There's lots more including branching, merging and other useful commands that we'll cover briefly on the next page. Make sure you check out the further resources page to get a better understanding of using Git.

Last updated