Setting Up Git and Your First Repository Locally

Now, it's time to roll up our sleeves and get our hands dirty with Git. By the end of this section, you'll have Git installed on your computer and your first Git repository set up. Excited? Let's dive in!

Installing Git

Before we can start using Git, we need to install it on your computer. The process is a bit different depending on your operating system, so follow the instructions that match your setup.

For Windows:

  1. Go to the official Git website: https://git-scm.com/download/win
  2. The download should start automatically. If it doesn't, click on the download link.
  3. Once the download is complete, run the installer.
  4. You can leave most of the options at their defaults, but here are a few you might want to pay attention to:
    • When asked about adjusting your PATH environment, choose "Git from the command line and also from 3rd-party software."
    • For line ending conversions, choose "Checkout Windows-style, commit Unix-style line endings."
  5. Complete the installation.

For macOS:

  1. If you have Homebrew installed (a popular package manager for macOS), you can install Git by opening Terminal and typing:
    brew install git
    
  2. If you don't have Homebrew, you can download Git from here: https://git-scm.com/download/mac
  3. Once downloaded, run the installer and follow the prompts.

For Linux:

Most Linux distributions come with Git pre-installed. If yours doesn't, you can install it using your distribution's package manager. For example:

  • For Ubuntu or Debian:
    sudo apt-get install git
    
  • For Fedora:
    sudo dnf install git
    

Configuring Git

Now that Git is installed, we need to set it up with your name and email. Git uses this information to identify who made each commit (remember, a commit is like a save point in your project).

Open your terminal (Command Prompt or PowerShell on Windows, Terminal on macOS and Linux) and type these commands, replacing the placeholders with your actual name and email:

git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"

Great! Git is now installed and configured on your system.

Creating Your First Git Repository

Now for the exciting part – creating your first Git repository! A repository (often shortened to "repo") is like a project folder that Git keeps track of.

  1. First, create a new directory for your project. In your terminal, type:

    mkdir my_first_repo
    cd my_first_repo
    

    This creates a new directory called "my_first_repo" and moves you into it.

  2. Now, let's initialize this directory as a Git repository:

    git init
    

    You should see a message saying that an empty Git repository has been initialized.

  3. Let's create a simple file in this repository. You can use any text editor you like, or if you're comfortable with command line editors, you can use something like nano:

    nano hello.txt
    

    Type a simple message in this file, like "Hello, Git!", then save and close the file (in nano, you can do this by pressing Ctrl+X, then Y, then Enter).

  4. Now, let's see what Git thinks of our new file. Type:

    git status
    

    You should see a message telling you that there's an untracked file called hello.txt.

  5. To tell Git that we want to include this file in our next commit, we need to add it to the staging area:

    git add hello.txt
    

    The staging area is like a prep zone for your commits. It lets you group related changes together before you commit them. Think of it as a loading dock where you're preparing a shipment (your commit) to be sent out.

  6. If we run git status again, we'll see that hello.txt is now staged for commit.

    But wait, there's more! What if you have multiple files you want to stage? Typing git add for each file could get tedious. Luckily, Git has a shortcut. If you want to stage all changes in your current directory, you can use:

    git add .
    

    The dot (.) means "current directory," so this command tells Git to stage all changes in the current directory and its subdirectories.

    Be careful with this power, though! Make sure you know what changes you're staging. You might accidentally include files you didn't mean to.

  7. Now, let's say we've made changes to multiple files and used git add . to stage them all. Before we commit, it's a good idea to double-check what we're about to commit. We can do this with:

    git status
    

    This will show you all the files that are staged and ready to be committed.

  8. Finally, let's make our first commit:

    git commit -m "My first commit"
    

    The -m flag allows us to add a commit message directly in the command line. Always try to write clear, concise commit messages that explain what changes you made.

    When you make a commit, Git takes a snapshot of all the files in your staging area and stores it in its history. It's like taking a photograph of your project at this specific moment in time.

  9. After you've made your commit, it's a good idea to check the status again:

    git status
    

    You should see a message saying "nothing to commit, working tree clean". This means all your changes have been safely committed.

  10. Want to see a log of your commits? Try this:

git log

This shows you a history of all the commits in your repository, with the most recent at the top. You'll see the commit hash (a unique identifier for each commit), the author, date, and commit message for each commit.

Congratulations! You've just created your first Git repository, staged changes, and made your first commit. You're officially on your way to becoming a Git wizard!

Unstaging Files

Oops! Did you accidentally stage a file you didn't mean to? No worries! Git has got your back. Let's learn how to unstage files.

  1. First, let's create a new file that we'll stage and then unstage. In your terminal, type:
echo "This is a mistake" > oops.txt

This creates a new file called oops.txt with the content "This is a mistake".

  1. Now, let's stage all changes:
git add .

This stages our new oops.txt file along with any other changes.

  1. Check the status:
git status

You should see oops.txt listed under "Changes to be committed".

  1. Uh-oh! We didn't actually want to stage oops.txt. Let's unstage it:
git restore --staged oops.txt

This command removes oops.txt from the staging area, but doesn't delete the file or undo any changes you've made to it.

  1. Check the status again:
git status

Now you should see oops.txt listed under "Untracked files".

The git restore --staged command is an eraser for your staging area. It lets you unstage files without losing any changes you've made. This is super handy when you accidentally stage files you didn't mean to, or when you decide you're not ready to commit a particular change yet.

Wrapping Up

In this chapter, we've covered a lot of ground! We installed Git, set it up with our personal information, and created our first repository. We learned how to add files to the staging area (both individually and all at once), make commits, check the status and history of our repository, and even how to unstage files if we make a mistake.

Remember, the basic workflow we followed is:

  1. Make changes to your files
  2. Stage the changes (git add)
  3. (Optional) Unstage changes if needed (git restore --staged)
  4. Commit the changes (git commit)

This workflow is the heart of using Git, and you'll use it countless times as you work on projects.

In the next chapter, we'll explore the Git workflow more deeply and learn about some more advanced commands. We'll also start exploring how to use Git for collaboration.

GitBeginner
Avatar for Niall Maher

Written by Niall Maher

Founder of Codú - The web developer community! I've worked in nearly every corner of technology businesses: Lead Developer, Software Architect, Product Manager, CTO, and now happily a Founder.

Loading

Fetching comments

Hey! 👋

Got something to say?

or to leave a comment.