Git Tutorial menu

Git First Repository

A repository is a folder that Git watches. In this lesson you create one and make your first commits.

Create a Repository

Make a folder for a small website and turn it into a repository:

Example

bash
mkdir my-site
cd my-site
git init
Output
Initialized empty Git repository in /home/ali/my-site/.git/

Git creates a hidden .git folder. It holds the whole history. Never edit or delete it by hand.

The Three Areas

Git has three places where your files can be:

AreaWhat it is
Working directoryYour normal files, as you edit them
Staging areaThe changes you have chosen for the next commit
RepositoryThe saved commits (history)

The usual flow is: edit files, stage the changes with git add, then commit them with git commit.

Think of the staging area like a shopping basket: you pick which changes go in, then you pay (commit) for all of them together.

git status

Create a file index.html with any text, then run:

Example

bash
git status
Output
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	index.html

nothing added to commit but untracked files present (use "git add" to track)

"Untracked" means Git sees the file but is not saving it yet. Run git status often; it always tells you what is going on.

git add

Example

bash
git add index.html
git status
Output
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   index.html
CommandStages
git add index.htmlOne file
git add css/Everything in a folder
git add .All changes in the current folder

git commit

A commit saves the staged changes with a message that explains them:

Example

bash
git commit -m "Add home page"
Output
[main (root-commit) 3f2a9c1] Add home page
 1 file changed, 1 insertion(+)
 create mode 100644 index.html

3f2a9c1 is the start of the commit's unique ID (a hash). Every commit has one.

Making More Commits

Add a stylesheet and change the page:

Example

bash
echo "body { font-family: Arial; }" > style.css
git add .
git commit -m "Add basic stylesheet"
Output
[main 8b41d07] Add basic stylesheet
 1 file changed, 1 insertion(+)
 create mode 100644 style.css

For files Git already tracks, you can stage and commit in one step:

Example

bash
git commit -am "Update home page heading"

The -a flag stages all changed tracked files. It does not add new files.

Good Commit Messages

  • Short (about 50 characters) and clear
  • Say what the commit does: "Add contact form", "Fix menu on mobile"
  • One commit for one logical change

Bad messages like "changes", "update" or "asdf" make history useless later.

Exercise

Exercise

What does git add do?

Learn Git with a teacher

Join the Full Stack Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp