Git Basics: Your First Repository

Version Control ~ 8 minutes

# Welcome to Git

Hello and welcome! Today we will make our first Git repository. Do not worry if you have never used Git before. We will go step by step.

Git is a tool that saves the history of your files. Imagine you write a school report. You save version one, then version two, then "final", then "final-final". This becomes messy very fast. Git solves this problem. It remembers every change, who made it and when. You can go back to an older version at any time. You can learn more on the official site, git-scm.com.

What you will learn

  • How to set your name and email in Git
  • How to create a repository
  • How to check status, stage files and make a commit
  • How to read your history
Check that Git is installed
Code Copied to Clipboard

# Read the result

The command above shows your Git version, for example git version 2.43.0. If you see a version number, Git is ready. If you see an error, please install Git first from the download page. Then open a new terminal window and try again.

Next, we will tell Git who you are. Git writes your name on every commit you make. This is very helpful when many people work on one project. We need to set only two things, your name and your email. You do this just one time on each computer.

Set your name
Code Copied to Clipboard

# Now set your email

Please replace Your Name with your real name. Keep the quotes around it. The word --global means this setting is used for all your projects on this computer.

Now, please set your email in the same way. Use the email you plan to use on sites like GitHub. This helps those sites link your commits to your account. The email is only text for Git. It does not send any message.

Set your email
Code Copied to Clipboard

# Make a project folder

Good job! Git now knows you. Let us make a small project. A repository, or repo, is just a normal folder that Git watches. We will first create the folder with the mkdir command. The name mkdir means make directory.

We will call our folder my-first-repo. You can choose another name if you like, but then please use your own name in every later step. Short names without spaces are best.

Create the folder
Code Copied to Clipboard

# Go inside the folder

The folder now exists. But our terminal is still in the old place. We need to move inside the new folder. The command for this is cd, which means change directory. After you run it, every command we type will work inside my-first-repo. If you ever feel lost, you can type pwd to see the current folder path.

Enter the folder
Code Copied to Clipboard

# Start the repository

Now comes the key step. The command git init turns a normal folder into a Git repository. It creates a hidden folder named .git. This hidden folder is the brain of Git. It stores all your history.

Please run the command in the next block. You should see a message that says an empty Git repository was initialized. You only need to run git init one time per project.

Initialize the repository
Code Copied to Clipboard

Never delete the .git folder

The .git folder is hidden on most systems. Please do not delete or edit it by hand. If you delete it, your project loses its whole history, and the folder becomes a normal folder again. Your files stay safe, but the saved versions are gone. If you want to see hidden folders, you can run ls -a in the terminal.

# Create your first file

Our repository is empty, so let us add a file. Please create a new file named notes.txt in the folder my-first-repo. You can use any text editor. Write the lines shown in the next block and save the file.

This file is very small, but it is perfect for learning. Git can track any kind of file, such as code, text, images and documents. It works best with plain text files, because it can show you exactly which lines changed.

notes.txt
Code Copied to Clipboard

# Check the status

Now we will ask Git what it sees. The command git status is your best friend. It tells you which files are new, which are changed and which are ready to save. Many developers run it again and again, so please use it often.

After you run it, you should see notes.txt under Untracked files. Untracked means Git sees the file, but it is not watching it yet.

See what Git sees
Code Copied to Clipboard

# Stage the file

Saving in Git has two steps. First, you stage a file. Staging means you choose what will go into the next save. Second, you commit, which makes the save.

Think of it like packing a box. First you put items into the box. That is staging. Then you close and label the box. That is committing. This design gives you control, because you can pack only some of your changes in one box.

Please stage our file with git add and the file name.

Stage notes.txt
Code Copied to Clipboard
Staged is not saved yet

After git add, your file is only staged. It is not saved in history yet. Many beginners forget the commit step. Please remember this simple order:

The basic Git cycle

  1. Change your files
  2. Stage them with git add
  3. Save them with git commit

If you run git status now, you will see notes.txt under Changes to be committed. This means your box is packed and waiting for the label.

# Make your first commit

Now we close the box. A commit is a saved snapshot of your staged files. Every commit needs a short message. The message tells your future self what changed.

We use the -m option to write the message on the same line. Please keep messages short and clear. Good examples are "Add notes file" or "Fix typo in title". Bad examples are "stuff" or "changes". Your team will thank you for good messages.

Create the commit
Code Copied to Clipboard

# Look at your history

Congratulations! You made your first commit. Now let us see the history. The command git log shows all commits, with the newest first. The full output is long, so we add --oneline. This prints one short line for each commit.

You should see a short code, called a hash, and your message. The hash is like an ID card. Each commit has its own unique hash. We will use hashes later to go back in time.

Show short history
Code Copied to Clipboard

# Ignore files you do not need

Sometimes a folder contains files that you do not want in Git. Examples are passwords, log files and big downloaded libraries. We can tell Git to skip them with a special file named .gitignore.

Create a file with this exact name in your project folder. Write one pattern on each line. The next block shows a small example. It ignores all log files, a folder named node_modules, and a file named .env. You can read more in the gitignore documentation.

.gitignore
Code Copied to Clipboard

# Summary and next steps

Very good work! You now know the heart of Git. Let us repeat the main points.

What we learned

  • git config tells Git who you are
  • git init starts a repository
  • git status shows the current state
  • git add stages files
  • git commit saves a snapshot
  • git log --oneline shows the history
  • .gitignore hides files from Git

Next steps

  • Change notes.txt and make a second commit
  • Learn branching in our next tutorial
  • Read the free Pro Git book

Please practise with a small project each day. Soon these commands will feel natural.