03. Adding and Commiting Files

Let's now talk about adding and committing files. Since this can get wildly abstract and confusing, refer to the figure below!

Three stages of Git.
It'd be very helpful to memorize this figure!

Adding files to be tracked

As any other well-mannered developer would, let's start by creating a README file. This will be to keep track of any notes that you make along the way that we want any users to know.

$ touch README
$ git status
On branch master Initial commit Untracked files: (use "git add <file>..." to include in what will be committed) README nothing added to commit but untracked files present (use "git add" to track)

Upon checking the status, you'll see that your README file is untracked. This means that any modifications made to this file won't be noticed by Git. Additionally, untracked files are not in the staging area, so are not ready to be snapshotted by Git. To move the file to the staging area, simply use the add subcommand.

$ git add README
$ git status
On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: README

The README file has now gone from its untracked state to the staging area, ready to be snapshotted. Remember that the fancy word for Git taking a snapshot is committed. We'll use this term henceforth.

Editing tracked files

Note that if we edit our files in the staging area, its edited version will move out of the staging area; the original version that was placed when running add will still be in the staging area, but the newly updated copy will not. To place the most updated copy, we have to run the add subcommand again. For example, let's try adding some text in our README.

$ echo 'Hello world! This is my fun project!' > README
$ git status
On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: README 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: README

Notice that the new edits made to the README file are not staged for the commit. We see that we must re-add the README file to place the most recent copy into the staging area. So if you have run add on your files, but decided to make some more edits, make sure to run add (if you'd like to stage the most recent copy)!

$ git add README
$ git status
On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: README

Committing files

The next step is to commit our file to our Git repository. By committing, Git will take a snapshot of the files in our staging area.

When committing, it's mandatory to add the -m option, followed by a comment. This comment serves as a brief summary the updates you made to the code. Although you may see it as an inconvenience, a good commit message will pay off when you're trying to rollback changes in the future.

$ git commit -m 'First commit. Including a README file.'
[master (root-commit) df292d0] First commit. Including a README file. 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 README

Great! Now you've committed for the first time. You may now access this specific point in time with the checksum df292d0.

Shortcut to add and commit

One more thing - we can use the -a option with the commit subcommand to automatically stage all the files that were modified and deleted. However, new files that are added that you have not told Git about with not be affected.

$ git commit -a -m "Commit message here."

Practice Makes Perfect

Try making your own edits, and use the status subcommand to continually check up on your files. Ask yourself where they are in the diagram above and try modifying the states for practice.

Ace your Technical Interview

Cracking the Coding Interview

Ace your Technical Interview Try Algorithms

Cracking the Coding Interview will tell you exactly what you need to know to land your dream job at Google, Amazon, Microsoft or any other big tech companies. Packed with over 189 interview questions and detailed solutions, this book will help you train for the classic white-board technical interview. A must have for any CS major.

$ Check price
39.9539.95Amazon 4.5 logo(266+ reviews)

More Algorithms resources

Aching back from coding all day?

Foam Seat Cushion

Aching back from coding all day? Try Back Problems

This foam seat cushion relieves lowerback pain, numbness and pressure sores by promoting healthy weight distribution, posture and spine alignment. Furthermore, it reduces pressure on the tailbone and hip bones while sitting. Perfect for sitting on the computer desk for long periods of time.

$ Check price
99.9599.95Amazon 4.5 logo(9,445+ reviews)

More Back Problems resources

Ad