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

提供:Dev Guides
移動先:案内検索

序章

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

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

前提条件

  • ローカルコンピューターにGitがインストールされています。 チュートリアルオープンソースに貢献する方法:Git入門では、Gitのインストールについて説明し、役立つと思われる背景情報について説明します。

ステップ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ファイルを開き、無視したいファイルまたはディレクトリごとに新しい行を追加します。

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

ステップ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 <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を実行して、これらのファイルが追加されていることを確認します。

OutputOn 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

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

OutputOn 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](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 <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](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拡張機能を使用すると、テキストエディタで適切な構文の強調表示を適用するのに役立ちます。

リポジトリへの変更の保存は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、電子メール、またはその他の場所からダウンロードしたファイルに置き換えたかにかかわらず、変更を確認します。

それでは、記事のバージョンの管理を見てみましょう。

ステップ4—変更の管理

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

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

git log
Outputcommit 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
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 <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にプッシュすると、他の人を招待して作業の編集を手伝ってもらうことができます。