Introduction

Git ready!

Hey! In this course, we'll dive into Git, a system for tracking changes in files and coordinating work in teams.

$ git init

//Output Below

Initialized empty Git repository in /Users/user/project/.git/

While Git is very popular for software projects, we can use it to keep track of changes in any file. But what exactly did we just do?

Repositories

We've just created a repository, which is a special directory that imports our work to Git. Every project we want to track needs such a repository.

$ mkdir project
$ cd project
$ git init

//Output Below

Initialized empty Git repository in /Users/user/project/.git/

Sweet! With git init, we can turn an ordinary directory into a repository and start tracking changes.

Psst: repositories are also referred to as repos.

Checking the status

Up until now, we haven't made any changes to our repository, have we? In order to confirm that, let's check its status!

$ git status

//Output Below

On branch master
Initial commit
nothing to commit (create/copy files and use "git add" to track)

Everything in Git starts with the git command. Don't worry about branches for now. All we need to know is that we're working on the master branch!

Psst: when there aren't any files with changes in the working directory, we say that the directory is clean.

Making changes

>

It's time to make some changes. Let's create a text file and check the status of the repository again!

$ touch cookie.txt
$ git status 

//Output Below

On branch master
Initial commit
Untracked files:
 cookie.txt
nothing added to commit but untracked files present (use "git add" to track)

Right there! The git status command reveals that cookie.txt is currently untracked.

Tracking files

Now, in order to keep track of cookie.txt, we need to add it to the list of tracked files

$ git add cookie.txt
$ git status

//Output Below

On branch master
Initial commit
Changes to be committed:
 new file: cookie.txt

Perfect! When we add a file to the list of tracked files using git add, we're said to stage it.

Unstaged changes

Let's add something to cookie.txt so we can see what happens to its status!

$ echo Hello world! > cookie.txt
$ git status

//Output Below

On branch master
Initial commit
Changes to be committed:
 new file: cookie.txt
Changes not staged for commit:
 modified: cookie.txt
Hello world!

Sweet! cookie.txt is still tracked but now it's not staged anymore because its content has changed. We need to use git add to stage it again.

Tracking multiple files

Sometimes we want to stage multiple files. While it would take a while to do this file by file, there's a great shortcut.

$ touch cookie.txt
$ touch cake.txt
$ git add . 
$ git status

//Output Below

On branch master
Changes to be committed:
 new file: cookie.txt
 new file:   cake.txt

Nice! Using git add with ., we can add all the unstaged files in the current directory (and deeper), making this a great way to add multiple files.

Time to commit!

When we run git add, Git captures a snapshot of the changes that we can make permanent by committing them to the repository.

$ git commit -m "Add cookie and cake" 

//Output Below

[master (root-commit) cf010b1] Add cookie and cake
2 files changed, 1 insertion(+)
 create mode 100644 cookie.txt
 create mode 100644 cake.txt

That's it! After the -m, we can provide a commit message, which is useful to keep note of our commits.

The Git workflow

As we've seen, there's a workflow that we almost always follow when we work with Git.

  1. Make changes in the working directory
  2. Use git add to stage them
  3. Use git commit to save the changes to repository

Sweet! We make changes in the working directory, stage the files we want to commit in the staging area, and make them permanent by committing them.

Installing Git (macOS)

If you're on macOS, you can get Git along with the Xcode Command Line Tools.

You can run xcode-select --install or try to run git from the Terminal; if you don’t have it, you'll be prompted to install it.

$ xcode-select --install 

//Output Below

xcode-select: note: install requested for command line developer tools

Great! After installing the Xcode Command Line Tools, we're all set to run git from the Terminal.

Installing Git (Windows)

If you're using Windows, it's a great idea to download Git for Windows.

After you've installed it, you can use the Command Prompt to check if it's in place.

C:\>git --version 

//Output Below

git version 2.8.4

There! Git for Windows is ready.

Psst: please note that some commands in this course only work on macOS and other Unix-based systems.