Signup/Sign In

GIT: Committing Changes to your Repository

Now that we’ve understood what repositories are, lets understand how to commit changes to a repository. For this purpose, let’s make use of our repository 'awesome_project'.

It’s always a good idea for your project to have a 'README' file since it describes what your project is about and any additional information you might want to share. Okay then, let’s use nano to create our README file (You can do this with any editor you please, Windows users recall that you can use Notepad by typing notepad README at the command line).

The nano editor gives you options at the bottom of the screen and will ask you to save the file when you press Cntrl + X (Cmd + X for Mac users). Check the filename and go ahead and save it.

Committing Changes to your Repository

Well, we’ve now added this file in our directory, but it’s not inside our repository as yet. Git lets you add files to repository by the 'git add' command, as you might have guessed, so go on and type $ git add README at the command line.

You might feel that this is an unnecessary step and Git ought to do it automatically. But it’s a good thing that Git does not automatically add files to repository. Consider you have a file of sensitive information such as passwords in your directory. Oops! You certainly don’t want that to be shared with your colleagues, do you?

Our next job is interesting and the whole purpose of this chapter, committing this file to our repository so Git can track changes in it

Committing a change, as overwhelming as it sounds, is actually pretty easy. Go on and type $ git commit README and uh-oh. Hmm, Git doesn’t really know who you are so it won’t allow you to commit a change. Smart, eh?

Committing Changes to your Repository

But don’t worry, you can use git configuration commands to tell Git about yourself. It doesn’t need to know everything about you, just your name and email ID will do fine.

$ git -config --global user.name “name”
$ git -config --global user.email “email”

The --global option changes your identity for all your repositories. There problem solved! Try running the commit command again. Excellent Work! Well now let’s edit the README and add some more information in there.

The important question here is should we commit this change? Is there a meaningful commit message that we can add? Sure, we can type in something like “Added info to README” but it doesn’t really tell us what information was added. Right now, we can add a message saying “Added course objective to README”. So let’s go right ahead and commit the change. But committing a change involved getting to the commit console and adding a message there with too much unnecessary information on the screen. Wouldn’t it be great if we could do it all with just one command?

Good News! There is something we can do. Go ahead and type the following on the command line:

$ git commit -a -m “Added Course Objective to README”

The -a option tells Git to commit ALL of the changes and the -m option is for providing a message right there in a single command.

Committing Changes to your Repository

Now it’s time to move on to the backend of Git, the Staging area. Typically, a staging area is an area which contains data before it is actually put to use. Similarly, the Git Staging Area keeps track of new and/or modified files in the repository which have not been committed yet.