Git init
5 minutesModule: Local Git Workflow in Action: From `git init` to Your First Commit
Do you have a project that is not yet under version control, and you’ve probably noticed by now that it should be? It’s never too late to put your project under version control, but starting version control early – ideally at the very beginning – gives you the clearest history and the most flexibility.
In the following, we’ll have a look at the whole local Git workflow in action,
from git init to the first commit, and beyond. We’ll do this by setting up
version control for a new project containing a minimal project structure.
One note before we get started: There are many different ways you can use
Git: at the command line or using different graphical user interfaces (GUIs)
of varying capabilities. In this course, we’ll be using something all Git
users have access to and can use to run all possible Git commands: the command
line you get by opening a terminal (in Windows, Command Prompt or PowerShell)
to type commands instead of using a graphical interface. You’ll need Git
installed. I wrote this tutorial using Git version 2.53.0. Check your Git
version with git --version. Feel free to use any reasonably up-to-date
version of Git that works well with your setup – a newer version usually makes
things easier. If you don’t have Git installed, you can find it on the
official download page.
Initializing a repository in an existing directory
Let’s go back to our workflow and our minimal project on our machine.
Say our working directory – the local directory where we store our files – is
called minimal-project. To put it under version control, we need to do two
things in our Command-Line Interface (CLI):
- Navigate to our project directory, e.g.,
cd /path/to/minimal-project. - Type
git init.
> git init
Initialized empty Git repository in /path/to/minimal-project/.git/The command git init will create a hidden directory named .git inside our
minimal-project folder. This .git folder contains all Git needs for version
control – you won’t normally interact with it directly, but it serves as the
core of your repository.
We are now ready to use git status – our every other Git command at the
terminal – to see what’s going on in our repository.
> git status
On branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)The first line says On branch main, though you might also see master instead
of main. This is Git showing us the name of the branch we’re currently working
on. Discussing branching is beyond the scope of basic local Git usage and will
be covered later. For now, you only need to know that Git creates a default
branch for every new repository. This is typically called main, but older
setups may still call it master.
Afterwards, Git is simply telling us that the repository is brand new:
- There have been no commits yet.
- There are no files in the project to commit.
In other words, our working directory minimal-project is completely empty,
except for the hidden .git folder that Git created during git init.
Also notice the message: create/copy files and use git add to track. Git is
giving us a hint about what to do next. The message means two things:
- First, put some files into your project folder, whether by creating or copying them – Git can’t track something that doesn’t exist yet.
- Then, once the files are there, use
git addto tell Git: “These are the files I want you to start tracking.”
We’ll explore this in detail in the next lesson.