Gitを使用してライティングプロジェクトを管理する方法

前書き

バージョン管理はコードのためだけではありません。 コンテンツを含め、追跡したいあらゆるものに対応します。 Gitを使用して次の執筆プロジェクトを管理すると、複数の下書きを同時に表示したり、それらの下書きの違いを確認したり、前のバージョンにロールバックしたりすることができます。 そして、そうするのが快適であれば、GitHubまたは他の中央Gitリポジトリで他の人と作業を共有できます。

このチュートリアルでは、Gitを使用して小さなMarkdownドキュメントを管理します。 初期バージョンを保存し、コミットし、変更を加え、それらの変更の違いを表示し、以前のバージョンを確認します。 完了したら、独自の執筆プロジェクトに適用できるワークフローが作成されます。

前提条件

[[step-1 -—- writing-a-workspace-for-your-writing-project]] ==ステップ1—ライティングプロジェクト用のワークスペースを作成する

変更を管理するには、ローカルGitリポジトリを作成します。 Gitリポジトリは既存のディレクトリ内にあるため、記事用の新しいディレクトリを作成することから始めます。

mkdir article

新しいarticleディレクトリに切り替えます。

cd article

git initコマンドは、現在のディレクトリに新しい空のGitリポジトリを作成します。 今すぐそのコマンドを実行します。

git init

リポジトリが作成されたことを確認する次の出力が表示されます。

OutputInitialized empty Git repository in /Users/sammy/article/.git/

.gitignoreファイルを使用すると、一部のファイルを無視する必要があることをGitに通知できます。 これを使用して、テキストエディタが作成する可能性のある一時ファイル、またはオペレーティングシステムファイルを無視できます。 たとえば、macOSでは、Finderアプリケーションはディレクトリに.DS_Storeファイルを作成します。 それらを無視する.gitignoreファイルを作成します。

nano .gitignore

ファイルに次の行を追加します。

gitignore
# Ignore Finder files
.DS_store

最初の行はコメントであり、今後無視する内容を特定するのに役立ちます。 2行目は無視するファイルを指定します。

ファイルを保存し、エディターを終了します。

無視したいファイルがさらに見つかったら、.gitignoreファイルを開き、無視したいファイルまたはディレクトリごとに新しい行を追加します。

リポジトリが設定されたので、作業を開始できます。

[[step-2 -—- saving-your-initial-draft]] ==ステップ2—最初のドラフトを保存する

Gitは、あなたがそれについて伝えるファイルについてのみ知っています。 リポジトリを保持するディレクトリにファイルが存在するからといって、Gitがその変更を追跡するわけではありません。 リポジトリにファイルを追加してから、変更をコミットする必要があります。

article.mdという名前の新しいMarkdownファイルを作成します。

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

次の出力が表示されます。

OutputOn branch master

No commits yet

Untracked files:
  (use "git add ..." 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を実行して、これらのファイルが追加されたことを確認します。

OutputOn branch master

No commits yet

Changes to be committed:
  (use "git rm --cached ..." 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

出力は、追加またはコミットする必要のある変更がないことを示しています。

OutputOn branch master
nothing to commit, working tree clean

次に、変更を処理する方法を見てみましょう。

[[step-3 -—- saving-revisions]] ==ステップ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

出力は、変更があることを示しています。

OutputOn branch master
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." 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 diffgit statusを使用すると、変更内容を確認するのに役立ちます。 差分をファイルに保存して、次のコマンドで後で表示できるようにすることもできます。

git diff article.md > article_diff.diff

.diff拡張子を使用すると、テキストエディタで適切な構文の強調表示を適用するのに役立ちます。

リポジトリへの変更の保存は、2段階のプロセスです。 まず、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
OutputOn branch master
nothing to commit, working tree clean

記事を修正するときにこのプロセスを続けます。 変更を行い、それらを検証し、ファイルを追加し、詳細メッセージを使用して変更をコミットします。 変更を頻繁にコミットするか、快適に感じるのと同じくらい少なくコミットします。 各ドラフトを終了した後、または記事の構造を大幅に変更する直前にコミットを実行する場合があります。

文書の下書きを他の誰かに送信し、その文書に変更を加えた場合は、その文書のコピーを取り、そのファイルでファイルを置き換えます。 次に、git diffを使用して、行った変更をすばやく確認します。 Gitは、直接入力したか、Web、電子メール、または他の場所からダウンロードしたファイルでファイルを置き換えたかにかかわらず、変更を確認します。

次に、記事のバージョンの管理を見てみましょう。

[[step-4 -—- managing-changes]] ==ステップ4—変更の管理

以前のバージョンのドキュメントを確認すると役立つ場合があります。 git commitを使用するたびに、実行した内容を要約した役立つメッセージを提供しました。

git logコマンドは、リポジトリのコミット履歴を表示します。 コミットしたすべての変更には、ログにエントリがあります。

git log
Outputcommit 1fbfc2173f3cec0741e0a6b21803fbd0be511bc4
Author: Sammy Shark 
Date:   Thu Sep 19 16:35:41 2019 -0500

    add prerequisites section

commit 95fed849b0205c49eda994fff91ec03642d59c79
Author: Sammy Shark 
Date:   Thu Sep 19 16:32:34 2019 -0500

    Add gitignore file and initial version of article

各コミットには特定の識別子があります。 この番号を使用して、特定のコミットの変更を参照します。 ただし、識別子の最初の数文字のみが必要です。 git log --onelineコマンドは、より短い識別子を持つログの要約バージョンを提供します。

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

ファイルの初期バージョンを表示するには、git showとコミット識別子を使用します。 リポジトリ内の識別子は、これらの例の識別子とは異なります。

git show 95fed84 article.md

出力には、コミットの詳細と、そのコミット中に発生した変更が表示されます。

Outputcommit 95fed849b0205c49eda994fff91ec03642d59c79
Author: Sammy Shark 
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にプッシュすると、他の人を招待して作業の編集を支援できます。