Moonlit Torii

Git Basics

Git version control, working tree, staging area, repository, and ignored files.

01 Why Version Control Matters

The Importance of Version Control

Suppose your manager asks you to write a proposal.

You write the document and may give it a filename like this:

Figure 1 for The Importance of Version Control

You send it to your manager, who says the proposal is mostly fine but asks you to revise a few parts and send it again.

Without much experience, you might edit the original file directly. Your manager asks for another revision, so you edit it again.

Later, your manager decides that the first version may have been better and asks you to use it instead.

How would you recover that first version now?

With some experience, you would probably make a copy before every revision, like this:

Figure 2 for The Importance of Version Control

If you have done this before, you already understand why version control matters.

However, this approach has many problems:

  1. Too many files quickly become disorganized.
  2. Copying files and directories is an awkward way to manage versions.
  3. The history is difficult to understand.
  4. It does not support collaboration.

Instead, consider this question:

Could we keep a history that records every change made to a file?

For example:

Go
版本一:创建文件xxx   三天前
版本二:修改了第xx行   两天前
版本三:修改错别字xxx   两天前
版本四:领导说要改xxx   一天前
版本五:最终版   三小时前

The system should do more than keep a textual log. For every commit, it should record and store the differences from the previous version.

That would let us return to any earlier commit.

This is where Git comes in.

How Git Began

Developers before us encountered the same problems.

Linus Torvalds, the creator of Linux, was dissatisfied with the version-control tools available at the time, such as CVS. He created Git in about ten days, and within a month the Linux source code was being managed with it.

With Git, the basic workflow looks like this:

Go
git init  // 初始化一个本地仓库
git add .  // 将目录下的所有文件提交到暂存区
git commit -m 提交信息  // 提交到git本地仓库
git log // 打印提交信息
git reset --hard xxx // 快速回到其他提交节点

At this point, you may wonder what the staging area is and why Git needs it.

What the Staging Area Does

  1. Separates the current working tree from the repository.
  2. Lets you commit in steps: commit records only staged content.
  3. Helps prevent accidental commits.

02 Git's Three Main Areas

The working tree, the staging area, and the local repository.

Figure 1 for 02 Git's Three Main Areas

Working tree → staging area: git add

Staging area → local repository: git commit

Check the current status:

git status

Viewing Commit History

Go
git log

Configure a more readable log format:

vim ~/.gitconfig

Go
[alias]
  lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --branches

Figure 1 for Viewing Commit History

Move back to an earlier version:

Go
git reset --hard commitID

Recover and move forward to another commit:

Go
git reflog  // 历史记录
git reset --hard commitID

03 Ignoring Files with Git

.gitignore

Go
.idea

A commonly used .gitignore for front-end projects:

Go
.DS_Store
node_modules
/dist


# local env files
.env.local
.env.*.local

# Log files
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*

# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

package-lock.json
yarn.lock

Common patterns for Go projects:

Common patterns for Python projects:

https://blog.csdn.net/weixin_45515850/article/details/108340443

What Should Git Ignore?

  1. Compiled output
  2. Sensitive files
  3. Third-party dependencies or source packages

References

Detailed .gitignore guide: https://blog.csdn.net/nyist_zxp/article/details/119887324