An introduction to Git (part 3): Adding and Committing

By shane
Thu, 2015-01-29 00:00
1 comments

Share with Others

In An Introduction to Git Part 1, you learned what Git is and how to download it on your computer. In An Introduction to Git Part 2, you learned how to configure Git and create your first Git repository. In this section you will learn how to add and commit files to your new Git repository.

Git is one of the secrets from my 5 secrets to becoming a Drupal 7 Ninja ebook, and much of the following posts are from this ebook. To learn more about Git and the other secrets, please consider purchasing the ebook or signing up for the 5 secrets email newsletter which will give you more information about the 5 secrets.

Viewing Your Project Status

Git Command What does it do?
git status View status information about your current Git repository.

The git status command is a command you will run early and often. It tells you the basics of what has changed with your Git repository. If you run the command now, you will see that there is nothing to commit yet. The command does tell you the branch that you are on (which we will cover later), as well as text telling you this is the “Initial commit”.

Git Status

Notice how the last line of the git status command tells you to create/copy files and use git add to track. Git is full of these helpful hints that tell you what you need to do. This can especially be helpful when you don’t know what to do or it has been awhile. I have spent way too much time searching the internet for answers, when the answer was often in the command output of the Git command I previously ran.

Ninja Lesson: Read the output of Git commands. It will save you time and headaches.

Open up a text or code editor and create a test file. Name your test file test.txt and keep the text really simple for now.

Example Text File

You will see what my current git_test directory looks like with the new test.txt file created.

Git Directory

Now re-run the git status command to see what has changed.

Git Status New File

Notice how the test.txt shows up under the “Untracked files” section. Also notice the line above the test.txt line that tells you how to add this file to include it in what will be committed.

You may have noticed the .git hidden folder inside the git_test folder. This hidden folder is created when we created the Git repository with the git init command. It is used to track everything about our Git repository. If you delete this folder, you are deleting your local Git repository.

Ninja Lesson: Do not delete the .git folder or you will delete your entire local Git repository.

Adding Files to your Git Repository

The next step in the process is to add the files to the Git staging area. The Git staging area is a middle ground between what has changed, and what has been committed to your Git repository. You can add files to this area and when you are ready, commit these files into one Git commit.

Git Command What does it do?
git add [file] Add a specific file to the Git staging area of your repository.
git add . Add all new/modified files inside the current directory to the staging area of your repository.

We are going to run the git add test.txt command to add the test.txt file to our Git staging area. We will then run the git status command to see that our file is now ready to be committed.

Git Add

If we had multiple files to commit, or we did not want to type in the file name, we can use the git add . command. The . (period) indicates to Git to add all new/modified files in the current directory or any subdirectories in the current directory (it does this recursively so even files in multiple levels of subdirectories would get added). We will use this command in future sections to provide a better idea of how it works.

Committing Your Changes to your Git Repository

A Git commit is a way to finalize and log the changes that we have added to our Git staging area. This essentially creates a new revision of your project at this particular point in time.

Git Command What does it do?
git commit Commits all changes from the Git staging area, and launches a text editor to create a commit message. Save and close the text editor to complete the commit.
git commit -m “My commit message goes here” Commits all changes from the Git staging area with the corresponding commit message.

You can use the git commit command to commit all of your staged changes. You will then need to fill out a commit message after the text editor is opened. After you save and close the file, the commit will be finalized.

You can also use the git commit -m command along with an inline commit message to simplify the process into one command. I prefer using this method as it is simpler than having to use a separate text editor tool. I also show the git status command after the commit which lets us know that we have nothing new to commit (our working directory is clean).

Git Commit

The commit message is much more important than it originally seems. The commit message provides a way for you to describe what has changed in the project. This makes it easy for you or others to quickly look at a history of commits to see how the project has changed over time.

Ninja Lesson: Commit early and commit often.

Viewing What Has Changed

Now that we have our first Git commit under our belt, we will make a few more changes. Let’s add an additional line to our test.txt file.

Example text file with new line

We will also create a new subfolder inside our git_test folder. Let’s call this directory test_folder.

Git test_folder

Inside this new test_folder directory, we will create a new file called test2.txt.

Second test file

We can now view the status of our git repository with the git status command.

Git Status

Notice the command output of the git status command lets us know that the test.txt file has been modified. It also lets us know about an untracked directory called test_folder.

Git Command What does it do?
git commit Commits all changes from the Git staging area, and launches a text editor to create a commit message. Save and close the text editor to complete the commit.
git diff Shows the changes between the last commit and the current working tree. This will only show changes in files that have been added to the repository.

Use the git diff command to see the specific changes of any files that have been modified.

Git Diff

Ninja Lesson: The git diff command only shows changes to files that are already being tracked by your Git repository.

We can use the previously mentioned git add test.txt command and a git add test_folder/test2.txt command to add the two files to the staging area, or we can use the git add . command to add both files for us automatically.

Git Add All

Ninja Lesson: The git add . command can be a real time saver, just be careful not to commit files you were not intending to commit.

You can now commit these changes. In this example, we will just use the git commit command without the -m to add the commit message.

Git Commit

Running this command will bring up a text editor to add a commit message. The text editor might vary depending on your system.

Git Commit editor

Now we simply add a commit message for this specific commit.

Git Commit Message in editor

We now save and close the text editor. This will complete the commit with your new commit message. This is just an alternative to adding the message directly within the commit command.

Git Commit Finished

Intro to Git Part 3 summary

You now know how to add and commit files to your Git repository. In An introduction to Git part 4 you will learn about Git branches and how they can be used within a Git repository. If you are looking for how this all relates to your Drupal projects, don't worry, we are getting there soon.

Comments

Trying to save and exit the git commit message:
hit escape and type " :wq " and then enter

Post new comment