Code has been added to clipboard!

How to Choose and Save Changes in Git

Git Commit Command

In most cases, developers want to save the changes they have made no matter how small they are. Git has two commands for this process: Git commit command, and Git add. There are two of them because saving files at Git is a two-step process as well.

Git doesn't save files immediately. First of all, they are brought to the so-called staging area. It can be described as a temporary storage where the files and their changes can be reviewed. The files are brought to staging by Git add command. If the user is satisfied with the changes he has made, he must execute Git commit command in order to confirm saving completely. After he has executed Git commit command, the changes in the files are saved.

This is quite enough to describe the basic functionality behind Git commit command; however, there is more than that to learn about it. For example, a user can choose which part of the changes in the staging he wants to save finally. By using additional commands described by simple alphabet letters, he or she can move among the hunks of changes and decide whether or not to continue to Git commit command.

Fundamentals of Git commit command are covered in this tutorial. Start reading now!

Git Commit Command: Main Tips

  • There are two phases which files go through until they are saved.
  • In order to save changes in the repository, you need to add and commit them. Git add moves the changes from a working directory to the staging areas. Git commit command saves the changes in the repository.
  • Saving files is the same process for both, new and already existing files.

What Is Stage in Git?

Before getting to the actual Git commit and Git add commands, it is important to understand what stage and staging in Git are.

Both these terms refer to an intermediate phase between choosing a file to commit and completing the action. It can be described as a buffer between the working area and the history of a project. Staging allows developers to select and review the changes that have been made before committing, making it very convenient to organize them.

Basic Steps of Saving Files

To save a change in the repository you are working in, you need to use a couple of simple commands and understand the concepts of adding and committing changes.

First, you will use the git add command to add a change into the staging area, instructing Git to include the updates to a specific file on the following update.

You should keep in mind that to actually affect the repository and solidify the changes, you must run git commit.

So essentially, the steps of committing changes look like the following:

  • Edit the files.
  • Stage the changes by using git add.
  • If you are satisfied with the snapshot you staged, use git commit to finalize the changes.

Additionally, you may use git status command to check the status of the files and the staging area.

Note: Don't confuse git add with svn add command. svn add creates a Git clone from any repository that belongs to Subversion while Git commit command finalizes the changes.

Saving Changes in Code Examples

It might be a good idea to take a closer look at the Git add, and Git commit commands.

Staging Changes

As mentioned before, this command adds changes to the staging area, but they are not final. This moment can be considered as the first stage of saving changes to a file.

An example of the code looks like the following:

git add <file>

In this case, all the changes will be staged for <file> in the following commit.

Here, the whole <directory> is added to the stage:

git add <directory>

During the Git add session, you can pick the changes you would like to commit. It can be done by initiating an interactive session:

git add -p

You can use y to stage a hunk and n to deny it. e will allow you to edit the current hunk manually and q to stop the staging session.

Committing Changes

This command commits the snapshot representing the changes to the history of the project and makes them solid.

git commit

After initiating the Git commit command, a text editor will be launched, asking you to input a commit text. Once you do it, you have to save it and close the text editor.

However, you can skip entering Git commit message separately by doing it together with the commit command. You will be able to input Git commit message by using this command:

git commit -m "<message>"

Sometimes it might be useful to add all the files in a directory at once, without picking them separately. Git add all is a command for that. Git add all will commit all the changes in the working directory but will apply changes to those that have been added by using Git during the work history.

Here is the code line for that:

git commit -a

Git Commit Command: Summary

  • There are two stages at Git to save files.
  • Git add and Git commit are the commands responsible for the two saving phases. Git add initiates the saving process by adding the desired changes to the staging area while Git commit confirms and finalizes their saving.
  • No matter if a file is newly created or existing for a while, the same process will be applied in order to save their changes.