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

介绍 (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.

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

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

先决条件 (Prerequisites)

第1步-为写作项目创建工作区 (Step 1 — Creating a Workspace for Your Writing Project)

To manage your changes, you’ll create a local Git repository. A Git repository lives inside of an existing directory, so start by creating a new directory for your article:

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

  • mkdir article

    mkdir文章

Switch to the new article directory:

切换到新的article目录:

  • cd article

    cd文章

The git init command creates a new empty Git repository in the current directory. Execute that command now:

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

  • git init

    git init

You’ll see the following output which confirms your repository was created:

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


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

The .gitignore file lets you tell Git that some files should be ignored. You can use this to ignore temporary files your text editor might create, or operating systems files. On macOS, for example, the Finder application creates .DS_Store files in directories. Create a .gitignore file that ignores them:

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

  • nano .gitignore

    纳米.gitignore

Add the following lines to the file:

将以下行添加到文件中:

.gitignore
.gitignore
# Ignore Finder files
.DS_store

The first line is a comment, which will help you identify what you’re ignoring in the future. The second line specifies the file to ignore.

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

Save the file and exit the editor.

保存文件并退出编辑器。

As you discover more files you want to ignore, open the .gitignore file and add a new line for each file or directory you want to ignore.

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

Now that your repository is configured, you can start working.

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

第2步-保存您的初稿 (Step 2 — Saving Your Initial Draft)

Git only knows about files you tell it about. Just because a file exists in the directory holding the repository doesn’t mean Git will track its changes. You have to add a file to the repository and then commit the changes.

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

Create a new Markdown file called article.md:

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

  • nano article.md

    纳米article.md

Add some text to the file:

在文件中添加一些文本:

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.

Save the changes and exit the editor.

保存更改并退出编辑器。

The git status command will show you the state of your repository. It will show you what files need to be added so Git can track them. Run this command:

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

  • git status

    git状态

You’ll see this output:

您将看到以下输出:


   
   
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)

In the output, the Untracked files section shows the files that Git isn’t looking at. These files need to be added to the repository so Git can watch them for changes. Use the git add command to do this:

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

  • git add .gitignore

    git添加.gitignore
  • git add article.md

    git添加article.md

Now run git status to verify those files have been added:

现在运行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

Both files are now listed in the Changes to be committed section. Git knows about them, but it hasn’t created a snapshot of the work yet. Use the git commit command to do that.

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

When you create a new commit, you need to provide a commit message. A good commit message states what your changes are. When you’re working with others, the more detailed your commit messages are, the better.

创建新提交时,需要提供一个提交消息。 一个好的提交消息指出您所做的更改。 与他人一起工作时,提交消息越详细,效果越好。

Use the command git commit to commit your changes:

使用命令git commit提交更改:

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

    git commit -m“添加gitignore文件和文章的初始版本”

The output of the command shows that the files were committed:

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


   
   
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

Use the git status command to see the state of the repository:

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

  • git status

    git状态

The output shows there are no changes that need to be added or committed.

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


   
   
Output
On branch master nothing to commit, working tree clean

Now let’s look at how to work with changes.

现在让我们看一下如何处理更改。

第3步-保存修订 (Step 3 — Saving Revisions)

You’ve added your initial version of the article. Now you’ll add more text so you can see how to manage changes with Git.

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

Open the article in your editor:

在编辑器中打开文章:

  • nano article.md

    纳米article.md

Add some more text to the end of the file:

在文件末尾添加更多文本:

## 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.

Save the file.

保存文件。

Use the git status command to see where things stand in your repository:

使用git status命令查看事物在存储库中的位置:

  • git status

    git状态

The output shows there are changes:

输出显示有更改:


   
   
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")

As expected, the article.md file has changes.

与预期的一样, article.md文件进行了更改。

Use git diff to see what they are:

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

  • git diff article.md

    git diff article.md

The output shows the lines you’ve added:

输出显示您添加的行:

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.

In the output, lines starting with a plus (+) sign are lines you added. Lines that were removed would show up with a minus (-) sign. Lines that were unchanged would have neither of these characters in front.

在输出中,以加号(+)开头的行是您添加的行。 删除的行将显示一个减号(-)。 未更改的行前面都不会包含这些字符。

Using git diff and git status is a helpful way to see what you’ve changed. You can also save the diff to a file so you can view it later with the following command:

使用git diffgit status是查看已更改内容的有用方法。 您还可以将diff保存到文件中,以便稍后使用以下命令查看它:

  • git diff article.md > article_diff.diff

    git diff article.md> article_diff.diff

Using the .diff extension will help your text editor apply the proper syntax highlighting.

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

Saving the changes to your repository is a two-step process. First, add the article.md file again, and then commit. Git wants you to explicitly tell it which files go in every commit, so even though you added the file before, you have to add it again. Note that the output from the git status command reminds you of that.

将更改保存到存储库是一个两步过程。 首先,再次添加article.md文件,然后提交。 Git希望您明确告诉它每次提交中包含哪些文件,因此即使您之前添加了文件,也必须再次添加它。 请注意, git status命令的输出提醒您这一点。

Add the file and then commit the changes, providing a commit message:

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

  • git add article.md

    git添加article.md
  • git commit -m "add prerequisites section"

    git commit -m“添加先决条件部分”

The output verifies that the commit worked:

输出验证提交是否有效:


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

Use git status to see your repository status. You’ll see that there’s nothing else to do.

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

  • git status

    git状态

   
   
Output
On branch master nothing to commit, working tree clean

Continue this process as you revise your article. Make changes, verify them, add the file, and commit the changes with a detailed message. Commit your changes as often or as little as you feel comfortable. You might perform a commit after you finish each draft, or right before you do a major rework of your article’s structure.

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

If you send a draft of a document to someone else and they make changes to it, take their copy and replace your file with theirs. Then use git diff to see the changes they made quickly. Git will see the changes whether you typed them in directly or replaced the file with one you downloaded from the web, email, or elsewhere.

如果您将文档草稿发送给其他人并且他们对其进行了更改,请复制他们的副本并用他们的文件替换您的文件。 然后使用git diff查看它们所做的快速更改。 无论您直接键入更改还是用从Web,电子邮件或其他地方下载的文件替换文件,Git都会看到更改。

Now let’s look at managing the versions of your article.

现在让我们看一下管理文章的版本。

步骤4 –管理变更 (Step 4 — Managing Changes)

Sometimes it’s helpful to look at a previous version of a document. Whenever you’ve used git commit, you’ve supplied a helpful message that summarizes what you’ve done.

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

The git log command shows you the commit history of your repository. Every change you’ve committed has an entry in the log.

git log命令向您显示存储库的提交历史记录。 您提交的每个更改都在日志中有一个条目。

  • git log

    git日志

   
   
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

Each commit has a specific identifier. You use this number to reference a specific commit’s changes. You only need the first several characters of the identifier though. The git log --oneline command gives you a condensed version of the log with shorter identifiers:

每个提交都有一个特定的标识符。 您可以使用该数字引用特定提交的更改。 不过,您只需要标识符的前几个字符。 git log --oneline命令为您提供了具有较短标识符的简明日志:

  • git log --oneline

    git log --oneline

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

To view the initial version of your file, use git show and the commit identifier. The identifiers in your repository will be different than the ones in these examples.

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

  • git show 95fed84 article.md

    git show 95fed84 article.md

The output shows the commit detail, as well as the changes that happened during that commit:

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


   
   
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.

To see the file itself, modify the command slightly. Instead of a space between the commit identifier and the file, replace with :./ like this:

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

  • git show 95fed84:./article.md

    git show 95fed84:./ article.md

You’ll see the content of that file, at that revision:

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


   
   
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.

You can save that output to a file if you need it for something else:

如果需要其他输出,可以将输出保存到文件中:

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

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

As you make more changes, your log will grow, and you’ll be able to review all of the changes you’ve made to your article over time.

当您进行更多更改时,您的日志将会增加,并且您将能够查看随着时间的推移您对文章所做的所有更改。

结论 (Conclusion)

In this tutorial you used a local Git repository to track the changes in your writing project. You can use this approach to manage individual articles, all the posts for your blog, or even your next novel. And if you push your repository to GitHub, you can invite others to help you edit your work.

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

翻译自: https://www.digitalocean.com/community/tutorials/how-to-use-git-to-manage-your-writing-project

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值