Git Common Operation
此内容尚不支持你的语言。
In this guide, I’m going to show how to create a local repository and use it.”
Initialzing a git repository
Section titled “Initialzing a git repository”Create a work directory. (Optional)
Section titled “Create a work directory. (Optional)”First, create a folder called frontend in /tmp directory and navigate into it.
mkdir frontendcd frontend
Initializing the repository
Section titled “Initializing the repository”Within the frontend directory, run git init to initialize the repository.
git init
The following output confirms successful initialization:
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:
git init html
Working Directory Operations
Section titled “Working Directory Operations”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 files in working directory
Section titled “Create files in working directory”Create two files named index.html and style.css. These two files are in the working directory.
touch index.html style.cssgit status
Run git status, the following output shows two untracked files:
git statusOn 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)
Stage the files for tracking
Section titled “Stage the files for tracking”Now we use git add command to track files:
git add index.html style.css
We can also use git add . to track all files in current directory.
git add .
Run git status again, the following output shows two tracked files:
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.
Modifying tracked files
Section titled “Modifying tracked files”Now we add some content to the index.html.
echo ‘<!DOCTYEP html> > index.html
Then run git status:
$ git statusOn 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:
git add index.html
Run git status again:
git statusOn 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:
Committing changes
Section titled “Committing changes”Creating the initial commit
Section titled “Creating the initial commit”Commit staged files to the repository by using git commit -m “description info”:
git commit -m “First commit”
Successful commit output:
[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.
Verifying commit history
Section titled “Verifying commit history”Run git log command to verify commit history, we should see the message:
git logcommit 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.