如何使用 Git 管理写作项目

介绍

版本控制不仅仅是用于代码,它适用于您想要跟踪的任何内容,包括内容。使用 Git来管理您的下一个写作项目,您可以同时查看多个草案,看到这些草案之间的差异,甚至回到以前的版本。

在本教程中,您将使用 Git 来管理一个小型的 Markdown 文档. 您将存储一个初始版本,承诺它,进行更改,查看这些更改之间的差异,并审查以前的版本。

前提条件

步骤 1 – 为您的写作项目创建工作空间

要管理你的更改,你会创建一个本地的 Git 存储库. 一个 Git 存储库生活在一个现有目录中,所以开始为你的文章创建一个新的目录:

1mkdir article

转到新的文章目录:

1cd article

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

1git init

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

1[secondary_label Output]
2Initialized empty Git repository in /Users/sammy/article/.git/

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

1nano .gitignore

将以下行添加到文件中:

1[label .gitignore]
2# Ignore Finder files
3.DS_store

第一行是评论,这将有助于您识别未来您正在忽略的内容,第二行则指定要忽略的文件。

保存文件并离开编辑器。

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

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

步骤2 - 保存您的初始草案

Git 只知道你告诉它的文件,仅仅因为文件存在于存储库中的目录,并不意味着 Git 会跟踪其更改,你必须将文件添加到存储库,然后进行更改。

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

1nano article.md

将文本添加到文件中:

1[label article.md]
2# How To Use Git to Manage Your Writing Project
3
4### Introduction
5
6Version 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.
7
8In 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 可以跟踪它们。

1git status

你会看到这个输出:

 1[secondary_label Output]
 2On branch master
 3
 4No commits yet
 5
 6Untracked files:
 7  (use "git add <file>..." to include in what will be committed)
 8
 9    .gitignore
10    article.md
11
12nothing added to commit but untracked files present (use "git add" to track)

在输出中,未穿过的文件部分显示了Git不查看的文件,这些文件需要添加到存储库中,以便Git可以查看它们的更改。

1git add .gitignore
2git add article.md

现在运行git status来验证这些文件已被添加:

 1[secondary_label Output]
 2On branch master
 3
 4No commits yet
 5
 6Changes to be committed:
 7  (use "git rm --cached <file>..." to unstage)
 8
 9    new file:   .gitignore
10    new file:   article.md

现在这两个文件都列在要进行的更改部分中。Git 知道它们,但尚未创建工作截图。

当你创建一个新的 commit 时,你需要提供一个 commit 消息. 一个好的 commit 消息表示你的更改是什么。

使用git commit命令来执行您的更改:

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

该命令的输出显示文件已被执行:

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

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

1git status

输出显示没有需要添加或承诺的更改。

1[secondary_label Output]
2On branch master
3nothing to commit, working tree clean

现在让我们看看如何与变化一起工作。

步骤3 - 保存修订

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

在你的编辑器中打开文章:

1nano article.md

添加更多文本到文件的末尾:

1## Prerequisites
2
3* Git installed on your local computer. The tutorial [How to Contribute to Open Source: Getting Started with Git](https://andsky.com/tech/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命令来查看存储库中的位置:

1git status

结果显示有变化:

1[secondary_label Output]
2On branch master
3Changes not staged for commit:
4  (use "git add <file>..." to update what will be committed)
5  (use "git checkout -- <file>..." to discard changes in working directory)
6
7    modified:   article.md
8
9no changes added to commit (use "git add" and/or "git commit -a")

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

使用git diff来查看它们是什么:

1git diff article.md

输出显示您添加的行:

 1diff --git a/article.md b/article.md
 2index 77b081c..ef6c301 100644
 3--- a/article.md
 4+++ b/article.md
 5@@ -5,3 +5,7 @@
 6 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.
 7
 8 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.
 9+
10+## Prerequisites
11+
12+* Git installed on your local computer. The tutorial [How to Contribute to Open Source: Getting Started with Git](https://andsky.com/tech/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.

在输出中,以 plus (+) 符号开始的行是您添加的行. 被删除的行将显示为 minus (-) 符号. 未变的行将没有这些字符在前面。

使用git diffgit status是查看您所更改的有用的方法,您也可以将diff保存到一个文件中,以便您可以使用以下命令稍后查看它:

1git diff article.md > article_diff.diff

使用.diff 扩展将有助于您的文本编辑器应用适当的语法突出。

将更改保存到您的存储库是一个为期两步的过程. 首先,再添加article.md文件,然后再 commit. Git 希望您明确地告诉它每个 commit 中哪个文件,所以即使您之前添加了该文件,您必须再次添加它。

添加文件,然后执行更改,提供一个 commit 消息:

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

输出验证了 commit 工作:

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

使用git status查看您的存储库状态,你会看到没有其他事情要做。

1git status
1[secondary_label Output]
2On branch master
3nothing to commit, working tree clean

继续这个过程,当你修改你的文章. 做出更改,验证它们,添加文件,并以详细的消息承诺更改. 承诺你的更改尽可能频繁或尽可能少,因为你觉得舒适。

如果您将文档草案发送给其他人,并且他们对其进行更改,请将他们的副本取代他们的文件,然后使用git diff快速查看他们所做的更改。

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

步骤4:管理变化

每次你使用了git commit,你已经提供了一个有用的消息,总结了你所做的。

git log 命令向您显示您的存储库的 commit 历史记录. 您所做的每一个更改都包含在日志中。

1git log
 1[secondary_label Output]
 2commit 1fbfc2173f3cec0741e0a6b21803fbd0be511bc4
 3Author: Sammy Shark <sammy@digitalocean>
 4Date:   Thu Sep 19 16:35:41 2019 -0500
 5
 6    add prerequisites section
 7
 8commit 95fed849b0205c49eda994fff91ec03642d59c79
 9Author: Sammy Shark <sammy@digitalocean>
10Date:   Thu Sep 19 16:32:34 2019 -0500
11
12    Add gitignore file and initial version of article

每个 commit 都有一个特定的标识符. 您使用这个号码来参考特定 commit 的更改. 您只需要标识符的前几个字符。 命令 git log --oneline 为您提供一个简化版本的日志,具有更短的标识符:

1git log --oneline
1[secondary_label Output]
21fbfc21 add prerequisites section
395fed84 Add gitignore file and initial version of article

要查看文件的初始版本,请使用git 显示和 commit 标识符. 您的存储库中的标识符将与这些示例中的标识符不同。

1git show 95fed84 article.md

输出显示了 commit 细节,以及该 commit 期间发生的更改:

 1[secondary_label Output]
 2commit 95fed849b0205c49eda994fff91ec03642d59c79
 3Author: Sammy Shark <sammy@digitalocean>
 4Date:   Thu Sep 19 16:32:34 2019 -0500
 5
 6    Add gitignore file and initial version of article
 7
 8diff --git a/article.md b/article.md
 9new file mode 100644
10index 0000000..77b081c
11--- /dev/null
12+++ b/article.md
13@@ -0,0 +1,7 @@
14+# How To Use Git to Manage Your Writing Project
15+
16+### Introduction
17+
18+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.
19+
20+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.

若要查看文件本身,请稍微修改命令,而不是在命令标识符和文件之间的空间,请用 :./ 替换,如下:

1git show 95fed84:./article.md

您将看到该文件的内容,在该修订:

1[secondary_label Output]
2# How To Use Git to Manage Your Writing Project
3
4### Introduction
5
6Version 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.
7
8In 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.

您可以将该输出保存到一个文件中,如果您需要它用于其他用途:

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

随着您进行更多的更改,您的日志将增长,您将能够通过时间审查您对文章所做的所有更改。

结论

在本教程中,你使用了本地的Git存储库来跟踪你的写作项目的变化,你可以使用这种方法来管理单个文章,你的博客的所有帖子,甚至你的下一个小说,如果你把你的存储库推到GitHub,你可以邀请其他人帮助你编辑你的工作。

Published At
Categories with 技术
Tagged with
comments powered by Disqus