序章

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

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

前提条件

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

ステップ1—ライティングプロジェクト用のワークスペースを作成する

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

  1. mkdir article

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

  1. cd article

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

  1. git init

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

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

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

  1. nano .gitignore

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

.gitignore
# Ignore Finder files
.DS_store

最初の行はコメントです。これは、将来無視しているものを特定するのに役立ちます。 2行目は、無視するファイルを指定しています。

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

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

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

ステップ2—最初のドラフトを保存する

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

と呼ばれる新しいMarkdownファイルを作成します article.md:

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

変更を保存して、エディターを終了します。

The git status コマンドは、リポジトリの状態を表示します。 Gitがそれらを追跡できるように、追加する必要のあるファイルが表示されます。 次のコマンドを実行します。

  1. 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 これを行うコマンド:

  1. git add .gitignore
  2. 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 変更をコミットするには:

  1. 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 リポジトリの状態を確認するコマンド:

  1. git status

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

Output
On branch master nothing to commit, working tree clean

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

ステップ3—リビジョンを保存する

記事の最初のバージョンを追加しました。 次に、テキストを追加して、Gitで変更を管理する方法を確認します。

エディターで記事を開きます。

  1. 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 リポジトリ内のどこにあるかを確認するコマンド:

  1. 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 それらが何であるかを確認するには:

  1. 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 diffgit status 何が変更されたかを確認するのに役立つ方法です。 次のコマンドで後で表示できるように、diffをファイルに保存することもできます。

  1. git diff article.md > article_diff.diff

を使用して .diff 拡張機能は、テキストエディタが適切な構文の強調表示を適用するのに役立ちます。

リポジトリへの変更の保存は2段階のプロセスです。 まず、を追加します article.md 再度ファイルしてからコミットします。 Gitでは、コミットのたびにどのファイルが含まれるかを明示的に指定する必要があるため、以前にファイルを追加した場合でも、再度追加する必要があります。 からの出力に注意してください git status コマンドはそれを思い出させます。

ファイルを追加してから変更をコミットし、コミットメッセージを提供します。

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

出力は、コミットが機能したことを確認します。

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

使用する git status リポジトリのステータスを確認します。 他に何もすることがないことがわかります。

  1. git status
Output
On branch master nothing to commit, working tree clean

記事を改訂しながら、このプロセスを続けます。 変更を加え、確認し、ファイルを追加し、詳細なメッセージで変更をコミットします。 変更は、快適に感じる頻度で、または少しだけコミットしてください。 各ドラフトを終了した後、または記事の構造を大幅に作り直す直前に、コミットを実行できます。

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

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

ステップ4—変更の管理

以前のバージョンのドキュメントを確認すると役立つ場合があります。 あなたが使用したときはいつでも git commit、あなたはあなたがしたことを要約する有用なメッセージを提供しました。

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

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

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

  1. git log --oneline
Output
1fbfc21 add prerequisites section 95fed84 Add gitignore file and initial version of article

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

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

ファイル自体を表示するには、コマンドを少し変更します。 コミット識別子とファイルの間のスペースの代わりに、次のように置き換えます :./ このような:

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

他の目的で必要な場合は、その出力をファイルに保存できます。

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

さらに変更を加えると、ログが大きくなり、時間の経過とともに記事に加えたすべての変更を確認できるようになります。

結論

このチュートリアルでは、ローカルのGitリポジトリを使用して、書き込みプロジェクトの変更を追跡しました。 このアプローチを使用して、個々の記事、ブログのすべての投稿、または次の小説を管理できます。 また、リポジトリをGitHubにプッシュすると、他の人を招待して作業の編集を手伝ってもらうことができます。