跳转到内容

Git Common Operation

此内容尚不支持你的语言。

In this guide, I’m going to show how to create a local repository and use it.”

First, create a folder called frontend in /tmp directory and navigate into it.

Terminal window
mkdir frontend
cd frontend

Within the frontend directory, run git init to initialize the repository.

Terminal window
git init

The following output confirms successful initialization:

Terminal window
Initialized empty Git repository in /tmp/frontend/.git/

Alternative Method:

You can initialize a repository directly without manually creating the folder by specifying a directory name:

Terminal window
git init html

A local git repository consists of three primary areas:

  • The working directory or working tree (your current file system location)
  • The staging area or index (where tracked file changes are prepared for commit)
  • The git repository proper (the .git directory storing version history)

Create two files named index.html and style.css. These two files are in the working directory.

Terminal window
touch index.html style.css
git status

Run git status, the following output shows two untracked files:

Terminal window
git status
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
index.html
style.css
nothing added to commit but untracked files present (use "git add" to track)

Now we use git add command to track files:

Terminal window
git add index.html style.css

We can also use git add . to track all files in current directory.

Terminal window
git add .

Run git status again, the following output shows two tracked files:

Terminal window
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: index.html
new file: style.css

These two files are in the staging area.

Now we add some content to the index.html.

Terminal window
echo ‘<!DOCTYEP html> > index.html

Then run git status:

Terminal window
$ git status
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: index.html
new file: style.css
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: index.html

Output reflects dual status. Run git add command again:

Terminal window
git add index.html

Run git status again:

Terminal window
git status
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: index.html
new file: style.css

Both files remain staged with the latest changes. Here’s a illustrator about the process:

git staging illustrator

Commit staged files to the repository by using git commit -m description info:

Terminal window
git commit -m “First commit”

Successful commit output:

Terminal window
[main (root-commit) a51bee4] First commit
2 files changed, 1 insertion(+)
create mode 100644 index.html
create mode 100644 style.css

This command puts the snapshot in the staging area to local repository.

git committing illustrator

Run git log command to verify commit history, we should see the message:

Terminal window
git log
commit a51bee47036d73f491973097696df52af569d60f (HEAD -> main)
Author: Bob <example@iters365.com>
Date: Tue Jul 15 09:15:30 2025 +0800
First commit

Finally, we initialize a repository, track files and keep the files in the repository.