如何使用 Git 管理你的写作项目

2025-05-27

如何使用 Git 管理你的写作项目

介绍

版本控制不仅仅适用于代码。它适用于任何你想追踪的内容,包括内容。使用Git管理你的下一个写作项目,你可以同时查看多个草稿,查看这些草稿之间的差异,甚至可以回滚到之前的版本。如果你愿意,还可以在 GitHub 或其他中央 Git 仓库上与他人分享你的工作。

在本教程中,你将使用 Git 管理一个小型 Markdown 文档。你将存储初始版本、提交、进行更改、查看更改之间的差异以及查看之前的版本。完成后,你将拥有一个可以应用于你自己的写作项目的工作流程。

先决条件

步骤 1 — 为你的写作项目创建工作区

要管理更改,您需要创建一个本地 Git 仓库。Git 仓库位于现有目录中,因此首先为您的文章创建一个新目录:

mkdir article

切换到新article目录:

cd article

git init命令在当前目录中创建一个新的空 Git 存储库。立即执行该命令:

git init

您将看到以下输出,确认您的存储库已创建:

Output
Initialized empty Git repository in /Users/sammy/article/.git/

.gitignore文件用于告诉 Git 应忽略某些文件。您可以使用它来忽略文本编辑器可能创建的临时文件或操作系统文件。例如,在 macOS 上,Finder 应用程序会.DS_Store在目录中创建文件。创建一个.gitignore忽略这些文件的文件:

nano .gitignore

将以下行添加到文件:

# Ignore Finder files
.DS_store

第一行是注释,它将帮助你识别将来要忽略的内容。第二行指定要忽略的文件。

保存文件并退出编辑器。

当您发现更多想要忽略的文件时,请打开该.gitignore文件并为想要忽略的每个文件或目录添加新行。

现在您的存储库已配置完毕,您可以开始工作了。

第 2 步 — 保存初稿

Git 只识别你指定给它的文件。仅仅因为某个文件存在于仓库所在目录中,并不意味着 Git 会跟踪它的更改。你必须将文件添加到仓库,然后提交更改。

创建一个名为的新 Markdown 文件article.md

nano article.md

向文件添加一些文本:

# How To Use Git to Manage Your Writing Project

### Introduction

Version control isn't just for code. It's for anything you want to track, including content. Using Git to manage your next writing project gives you the ability to view multiple drafts at the same time,  see differences between those drafts, and even roll back to a previous version. And if you're comfortable doing so, you can then share your work with others on GitHub or other central git repositories.

In this tutorial you'll use Git to manage a small Markdown document. You'll store an initial version, commit it, make changes, view the difference between those changes, and review the previous version. When you're done, you'll have a workflow you can apply to your own writing projects.

保存更改并退出编辑器。

git status命令将显示存储库的状态。它将显示需要添加哪些文件以便 Git 可以跟踪它们。运行以下命令:

git status

您将看到以下输出:

Output
On branch master

No commits yet

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

    .gitignore
    article.md

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

在输出中,此Untracked files部分显示了 Git 未查看的文件。这些文件需要添加到存储库,以便 Git 可以监视它们的更改。使用以下git add命令执行此操作:

git add .gitignore
git add article.md

现在运行git status以验证这些文件是否已添加:

Output
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   .gitignore
    new file:   article.md

现在这两个文件都已列在Changes to be committed部分中。Git 知道它们,但尚未创建工作的快照。请使用git commit以下命令创建快照。

创建新提交时,需要提供提交消息。好的提交消息应该能够清晰地说明你的修改。与他人协作时,提交消息越详细越好。

使用以下命令git commit提交您的更改:

git commit -m "Add gitignore file and initial version of article"

命令的输出显示文件已提交:

Output
[master (root-commit) 95fed84] Add gitignore file and initial version of article
 2 files changed, 9 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 article.md

使用git status命令查看存储库的状态:

git status

输出显示没有需要添加或提交的更改。

Output
On branch master
nothing to commit, working tree clean

现在让我们看看如何处理变化。

第 3 步 — 保存修订

您已添加文章的初始版本。现在,您将添加更多文本,以便了解如何使用 Git 管理更改。

在编辑器中打开文章:

nano article.md

在文件末尾添加一些文本:

## Prerequisites

* Git installed on your local computer. The tutorial [How to Contribute to Open Source: Getting Started with Git](https://www.digitalocean.com/community/tutorials/how-to-contribute-to-open-source-getting-started-with-git) walks you through installing Git and covers some background information you may find useful. 

保存文件。

使用git status命令查看存储库中的内容:

git status

输出显示有变化:

Output
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   article.md

no changes added to commit (use "git add" and/or "git commit -a")

正如预期的那样,article.md文件已经发生变化。

用于git diff查看它们是什么:

git diff article.md

输出显示您添加的行:

diff --git a/article.md b/article.md
index 77b081c..ef6c301 100644
--- a/article.md
+++ b/article.md
@@ -5,3 +5,7 @@
 Version control isn't just for code. It's for anything you want to track, including content. Using Git to manage your next writing project gives you the ability to view multiple drafts at the same time,  see differences between those drafts, and even roll back to a previous version. And if you're comfortable doing so, you can then share your work with others on GitHub or other central git repositories.

 In this tutorial you'll use Git to manage a small Markdown document. You'll store an initial version, commit it, make changes, view the difference between those changes, and review the previous version. When you're done, you'll have a workflow you can apply to your own writing projects.
+
+## Prerequisites
+
+* Git installed on your local computer. The tutorial [How to Contribute to Open Source: Getting Started with Git](https://www.digitalocean.com/community/tutorials/how-to-contribute-to-open-source-getting-started-with-git) walks you through installing Git and covers some background information you may find useful. 

在输出中,以加号 (+) 开头的行表示您添加的行。被删除的行会显示减号 (-)。未更改的行前面不会出现这两个字符。

使用git diff“和”git status命令可以很方便地查看更改的内容。您还可以将差异保存到文件中,以便稍后使用以下命令查看:

git diff article.md > article_diff.diff

使用.diff扩展将帮助您的文本编辑器应用正确的语法突出显示。

将更改保存到仓库需要两个步骤。首先,article.md再次添加文件,然后提交。Git 希望你明确地告诉它每次提交需要添加哪些文件,所以即使你之前添加了该文件,也必须再次添加。请注意,命令的输出git status会提醒你这一点。

添加文件,然后提交更改,并提供提交消息:

git add article.md
git commit -m "add prerequisites section"

输出验证提交是否有效:

Output
[master 1fbfc21] add prerequisites section
 1 file changed, 4 insertions(+)

用于git status查看你的仓库状态。你会发现没有其他操作可做。

git status

Output
On branch master
nothing to commit, working tree clean

在修改文章时,请继续此过程。进行修改、验证修改、添加文件,然后提交修改并附上详细的修改信息。您可以根据自己的喜好,尽可能频繁地提交修改。您可以在完成每个草稿后提交,也可以在对文章结构进行重大修改之前提交。

如果您将文档草稿发送给其他人,而他们对其进行了修改,请获取他们的副本并用他们的文件替换您的文件。然后使用 Gitgit diff快速查看他们所做的更改。无论您是直接输入更改,还是使用从网络、电子邮件或其他地方下载的文件替换文件,Git 都能看到更改。

现在让我们看看如何管理文章的版本。

步骤 4 — 管理变更

有时查看文档的先前版本会很有帮助。每当您使用 时git commit,您都会提供一条有用的消息来总结您所做的事情。

git log命令会显示你的仓库的提交历史记录。你提交的每项更改都会在日志中记录下来。

git log

Output
commit 1fbfc2173f3cec0741e0a6b21803fbd0be511bc4
Author: Sammy Shark <sammy@digitalocean>
Date:   Thu Sep 19 16:35:41 2019 -0500

    add prerequisites section

commit 95fed849b0205c49eda994fff91ec03642d59c79
Author: Sammy Shark <sammy@digitalocean>
Date:   Thu Sep 19 16:32:34 2019 -0500

    Add gitignore file and initial version of article

每个提交都有一个特定的标识符。您可以使用这个编号来引用特定提交的更改。不过,您只需要标识符的前几个字符。该git log --oneline命令会为您提供包含较短标识符的精简版日志:

git log --oneline
Output
1fbfc21 add prerequisites section
95fed84 Add gitignore file and initial version of article

要查看文件的初始版本,请使用git show和提交标识符。您的仓库中的标识符将与这些示例中的标识符不同。

git show 95fed84 article.md

输出显示提交的详细信息以及提交期间发生的更改:

Output
commit 95fed849b0205c49eda994fff91ec03642d59c79
Author: Sammy Shark <sammy@digitalocean>
Date:   Thu Sep 19 16:32:34 2019 -0500

    Add gitignore file and initial version of article

diff --git a/article.md b/article.md
new file mode 100644
index 0000000..77b081c
--- /dev/null
+++ b/article.md
@@ -0,0 +1,7 @@
+# How To Use Git to Manage Your Writing Project
+
+### Introduction
+
+Version control isn't just for code. It's for anything you want to track, including content. Using Git to manage your next writing project gives you the ability to view multiple drafts at the same time,  see differences between those drafts, and even roll back to a previous version. And if you're comfortable doing so, you can then share your work with others on GitHub or other central git repositories.
+
+In this tutorial you'll use Git to manage a small Markdown document. You'll store an initial version, commit it, make changes, view the difference between those changes, and review the previous version. When you're done, you'll have a workflow you can apply to your own writing projects.

要查看文件本身,请稍微修改命令。将提交标识符和文件之间的空格替换为:./

git show 95fed84:./article.md

您将看到该文件的该修订版本的内容:

Output# How To Use Git to Manage Your Writing Project

### Introduction

Version control isn't just for code. It's for anything you want to track, including content. Using Git to manage your next writing project gives you the ability to view multiple drafts at the same time,  see differences between those drafts, and even roll back to a previous version. And if you're comfortable doing so, you can then share your work with others on GitHub or other central git repositories.

In this tutorial you'll use Git to manage a small Markdown document. You'll store an initial version, commit it, make changes, view the difference between those changes, and review the previous version. When you're done, you'll have a workflow you can apply to your own writing projects.

如果您需要将该输出保存到文件中用于其他用途:

git show 95fed84:./article.md > old_article.md

随着您做出更多更改,您的日志将会增长,并且您将能够查看一段时间内对文章所做的所有更改。

结论

在本教程中,您使用本地 Git 仓库来跟踪写作项目中的更改。您可以使用这种方法来管理单篇文章、博客的所有帖子,甚至是您的下一部小说。如果您将仓库推送到 GitHub,就可以邀请其他人来协助您编辑作品。

文章来源:https://dev.to/digitalocean/how-to-use-git-to-manage-your-writing-project-2ge8
PREV
Github 个人资料:Como fazer?
NEXT
第一个月:从零开始创业