优秀的编程知识分享平台

网站首页 > 技术文章 正文

如何使用Git管理您的写作项目(怎么用git编写文档)

nanyue 2024-10-07 11:10:41 技术文章 8 ℃

翻译自 DigitalOcean

作者:Brian Hogan


介绍

版本控制不仅仅用于代码。它适用于您要跟踪的所有内容,包括内容。使用Git管理你的项目,使您能够同时查看多个草稿,查看这些草稿之间的差异,甚至回滚到以前的版本。而且,如果您愿意,可以在GitHub或其他服务端部署的Git存储库上与他人共享您的工作。

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

先决条件

  • 安装在本地计算机上的Git。本教程如何为开源做贡献:Git入门将指导您完成Git的安装过程,并介绍了一些您可能会觉得有用的背景信息。

第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

将以下行添加到文件中:

的.gitignore

# Ignore Finder files

.DS_store

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

保存文件并退出编辑器。

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

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

第2步-保存您的初稿

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

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

  • nano article.md

在文件中添加一些文本:

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是查看已更改内容的有用方法。您还可以将diff保存到文件中,以便稍后使用以下命令查看它:

  • 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

修改文章时,请继续此过程。进行更改,验证它们,添加文件并提交详细信息,并进行更改。尽可能轻松地进行更改。您可以在完成每个草稿之后,或者在对文章结构进行重大修改之前进行提交。

如果您将文档草稿发送给其他人并且他们对其进行了更改,请复制他们的文件并将其替换为您的文件。然后使用git diff来查看他们快速进行的更改。无论您直接键入更改还是用从Web,电子邮件或其他地方下载的文件替换文件,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://www.digitalocean.com/community/tutorials/how-to-use-git-to-manage-your-writing-project

Tags:

最近发表
标签列表