UNDERSTANDING THE GIT WORKFLOW

UNDERSTANDING THE GIT WORKFLOW

理解 Git 工作流

Git is a version control system. Version control, also known as source control, is the practice of tracking and managing changes to software code. Version control systems are software tools that help software teams manage changes to source code over time. Git 是一个版本控制系统。版本控制(也称为源代码控制)是跟踪和管理软件代码变更的实践。版本控制系统是帮助软件团队随时间推移管理源代码变更的软件工具。

Git is used for: Tracking code changes, Tracking who made changes, Coding collaboration. Git 的用途包括:跟踪代码变更、跟踪变更的贡献者、以及协同编码。

Setting up a new Repository: A Git repository is a folder that Git tracks for changes. The repository stores all your project’s history and versions. Add files to the folder. The following describes how to set up a new repository: 设置新仓库:Git 仓库是一个由 Git 跟踪变更的文件夹。该仓库存储了你项目的所有历史记录和版本。将文件添加到该文件夹中。以下是设置新仓库的方法:

Git Init: Initializes git user@localhost $ git init Git Init:初始化 git user@localhost $ git init

This creates a hidden folder called .git inside your project. This is where Git stores all the information it needs to track your files and history. To see which files are in your project folder, use the ls command: user@localhost $ ls 这会在你的项目内创建一个名为 .git 的隐藏文件夹。Git 在这里存储跟踪文件和历史记录所需的所有信息。要查看项目文件夹中的文件,请使用 ls 命令: user@localhost $ ls

To Check if Git is tracking your new files: user@localhost $ git status 要检查 Git 是否正在跟踪你的新文件: user@localhost $ git status

The files here could either be tracked or untracked:

  • Untracked Files: Files you’ve created or copied into the folder, but haven’t told Git to watch.
  • Tracked Files: Files that Git is watching for changes. To make a file tracked, you need to add it to the staging area. 此处的这些文件可能处于“已跟踪”或“未跟踪”状态:
  • 未跟踪文件:你创建或复制到文件夹中,但尚未告知 Git 进行监视的文件。
  • 已跟踪文件:Git 正在监视其变更的文件。 要使文件变为已跟踪状态,你需要将其添加到暂存区。

Git Staging: Tells Git exactly which files you want to include in your next commit. user@localhost $ git add . Git 暂存:明确告知 Git 你希望在下一次提交中包含哪些文件。 user@localhost $ git add .

Common Commands:

  • git add .: Stages all new, modified, and deleted files in the current directory and its subdirectories.
  • git add <file>: Stages a specific file.
  • git add -A (or --all): Stages all changes across the entire repository, regardless of your current folder location.
  • git add -u: Stages modifications and deletions of already-tracked files, ignoring completely new (untracked) files.
  • git add *.txt: Stages all files matching a specific pattern (e.g., all text files). 常用命令:
  • git add .:暂存当前目录及其子目录中所有新增、修改和删除的文件。
  • git add <file>:暂存特定文件。
  • git add -A (或 --all):暂存整个仓库中的所有变更,无论你当前位于哪个文件夹。
  • git add -u:仅暂存已跟踪文件的修改和删除,忽略全新的(未跟踪)文件。
  • git add *.txt:暂存所有符合特定模式的文件(例如,所有文本文件)。

Git Commit: A commit is like a save point in your project. It records a snapshot of your files at a certain time, with a message describing what changed. user@localhost $ git commit -m "Describe your changes" Git 提交:提交就像是你项目中的一个保存点。它记录了文件在特定时间的快照,并附带一条描述变更的信息。 user@localhost $ git commit -m "描述你的变更"

Pushing Changes: After you commit, your changes are only in your local repository. Pushing sends your commits to a remote repository like GitHub. user@localhost $ git push 推送变更:提交后,你的变更仅存在于本地仓库中。推送操作会将你的提交发送到 GitHub 等远程仓库。 user@localhost $ git push

Over 70% of developers use Git! Git does not store a separate copy of every file in every commit, but keeps track of changes made in each commit! For best use case, commit often and write clear commit messages. 超过 70% 的开发者使用 Git!Git 不会在每次提交时存储每个文件的独立副本,而是跟踪每次提交中所做的变更!为了获得最佳实践,请频繁提交并编写清晰的提交信息。