Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 23, 2019 03:41 pm GMT

How to Use Git to Manage Your Writing Project

Introduction

Version control isnt just for code. Its 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 youre comfortable doing so, you can then share your work with others on GitHub or other central Git repositories.

In this tutorial youll use Git to manage a small Markdown document. Youll store an initial version, commit it, make changes, view the difference between those changes, and review the previous version. When youre done, youll have a workflow you can apply to your own writing projects.

Prerequisites

Step 1 Creating a Workspace for Your Writing Project

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

mkdir article

Switch to the new article directory:

cd article

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

git init

Youll see the following output which confirms your repository was created:

OutputInitialized 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:

nano .gitignore

Add the following lines to the file:

# Ignore Finder files.DS_store

The first line is a comment, which will help you identify what youre 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.

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

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 doesnt mean Git will track its changes. You have to add a file to the repository and then commit the changes.

Create a new Markdown file called article.md:

nano article.md

Add some text to the file:

# How To Use Git to Manage Your Writing Project### IntroductionVersion 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

Youll see this output:

OutputOn branch masterNo commits yetUntracked files:  (use "git add <file>..." to include in what will be committed)    .gitignore    article.mdnothing added to commit but untracked files present (use "git add" to track)

In the output, the Untracked files section shows the files that Git isnt 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:

git add .gitignoregit add article.md

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

OutputOn branch masterNo commits yetChanges 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 hasnt created a snapshot of the work yet. Use the git commit command to do that.

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

Use the command git commit to commit your changes:

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

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

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

OutputOn branch masternothing to commit, working tree clean

Now lets look at how to work with changes.

Step 3 Saving Revisions

Youve added your initial version of the article. Now youll add more text so you can see how to manage changes with Git.

Open the article in your editor:

nano 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

The output shows there are changes:

OutputOn branch masterChanges 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.mdno changes added to commit (use "git add" and/or "git commit -a")

As expected, the article.md file has changes.

Use git diff to see what they are:

git diff article.md

The output shows the lines youve added:

diff --git a/article.md b/article.mdindex 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 youve changed. You can also save the diff to a file so you can view it later with the following command:

git diff article.md > article_diff.diff

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

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.

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

git add article.mdgit commit -m "add prerequisites section"

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. Youll see that theres nothing else to do.

git statusOutputOn branch masternothing 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 articles 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.

Now lets look at managing the versions of your article.

Step 4 Managing Changes

Sometimes its helpful to look at a previous version of a document. Whenever youve used git commit, youve supplied a helpful message that summarizes what youve done.

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

git logOutputcommit 1fbfc2173f3cec0741e0a6b21803fbd0be511bc4Author: Sammy Shark <sammy@digitalocean>Date:   Thu Sep 19 16:35:41 2019 -0500    add prerequisites sectioncommit 95fed849b0205c49eda994fff91ec03642d59c79Author: 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 commits 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
Output1fbfc21 add prerequisites section95fed84 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 95fed84 article.md

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

Outputcommit 95fed849b0205c49eda994fff91ec03642d59c79Author: Sammy Shark <sammy@digitalocean>Date:   Thu Sep 19 16:32:34 2019 -0500    Add gitignore file and initial version of articlediff --git a/article.md b/article.mdnew file mode 100644index 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

Youll see the content of that file, at that revision:

Output# How To Use Git to Manage Your Writing Project### IntroductionVersion 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

As you make more changes, your log will grow, and youll be able to review all of the changes youve 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.


Original Link: https://dev.to/digitalocean/how-to-use-git-to-manage-your-writing-project-2ge8

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To