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:
- Go to the official Git website: https://git-scm.com/download/win
- The download should start automatically. If it doesn't, click on the download link.
- Once the download is complete, run the installer.
- 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."
- Complete the installation.
For macOS:
- If you have Homebrew installed (a popular package manager for macOS), you can install Git by opening Terminal and typing:
brew install git
- If you don't have Homebrew, you can download Git from here: https://git-scm.com/download/mac
- 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.
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.
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.
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).
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.
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.
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.
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.
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.
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.
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.
- 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".
- Now, let's stage all changes:
git add .
This stages our new oops.txt file along with any other changes.
- Check the status:
git status
You should see oops.txt listed under "Changes to be committed".
- 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.
- 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:
- Make changes to your files
- Stage the changes (
git add
) - (Optional) Unstage changes if needed (
git restore --staged
) - 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.