Save Changes with the GIT commit command | Commits for New, Modified, and Multiple Files.


Git commit is a necessary, essential command to work with a repository. Commit means in version control to update the repository for changes done on a cloned copy of the project. After committing, other developers need to update the local copy for new code for changes.

In a distributed subversion control (e.g., git), the commit command updates the local repository, and later, the git push command sends the change to the central repository.

Commit in a git repository

This tutorial will teach us how to commit a new and modified file using a command-line interface. Along with how to commit for all updated files.


How to Commit a new file?

A new resource (e.g., a new file in the source code ) is always added to the software development life-cycle. First, the git add command is executed to make a file available for the next commit. Following is an example of adding a new file (example.c) to the git repository.

[root@CentOS_6_64-152 my_git_respositorty]#  git add example.c

[root@CentOS_6_64-152 my_git_respositorty]# git commit -m "example.c is added"

To see the commit history

root@CentOS_6_64-152 my_git_respositorty]# git log
commit 0371cf00c44f1ed73b107976a70866eb5c2f62b0
Author: root <root@CentOS_6_64-152.(none)>
Date: Sun Jan 28 02:12:45 2018 -0500

example.c is added

git commit for modified files:

In software development, code changes are common. Git provides a way to discover all changes and commit the changes in the repository. The git status command file lists the changed files. Once a list of changed files is ready. The files are added for the next commit with the git add command. In this example, file file1.txt is the modified file.

Git list commits:

[root@CentOS_6_64-152 my_git_respositorty]# git status
# On branch master
# Changed but not updated:
# (use “git add <file>…” to update what will be committed)
# (use “git checkout — <file>…” to discard changes in the working directory)
#
# modified: file1.txt

Add the file for the next commit:

[root@CentOS_6_64-152 my_git_respositorty]# git add file1.txt

Commit again with a new list to commit:

[root@CentOS_6_64-152 my_git_respositorty]# git commit -m “Updated files commit”
[master 6e9a7ad] Updated files commit

Verify the commit or git list commits:

[root@CentOS_6_64-152 my_git_respositorty]# git log
commit 6e9a7ad3181bf5d8a627611ad2150bc570878c26
Author: root <root@CentOS_6_64-152.(none)>
Date: Sun Jan 28 02:26:27 2018 -0500

Updated files commit

commit 0371cf00c44f1ed73b107976a70866eb5c2f62b0
Author: root <root@CentOS_6_64-152.(none)>
Date: Sun Jan 28 02:12:45 2018 -0500

example.c is added

How to commit all changed files (no add)?

When there is a list of all files, and it’s ok to commit all files, it provides an option for committing all files without git add.

[root@CentOS_6_64-152 my_git_respositorty]# git commit -a -m “Commit without git add.”
[master 88792a1] Commit without git add

Git list commits:

[root@CentOS_6_64-152 my_git_respositorty]# git status
# On branch master
nothing to commit (working directory clean)
[root@CentOS_6_64-152 my_git_respositorty]# git log
commit 88792a16175280aab940c0a65008d8e8682443c0
Author: root <root@CentOS_6_64-152.(none)>
Date: Sun Jan 28 02:33:23 2018 -0500

Commit without git add

commit a6506b29b55e09ad76378d26336984ce6e63fbea
Author: root <root@CentOS_6_64-152.(none)>
Date: Sun Jan 28 02:32:02 2018 -0500

New file2

commit 6e9a7ad3181bf5d8a627611ad2150bc570878c26
Author: root <root@CentOS_6_64-152.(none)>
Date: Sun Jan 28 02:26:27 2018 -0500

Updated files commit

commit 0371cf00c44f1ed73b107976a70866eb5c2f62b0
Author: root <root@CentOS_6_64-152.(none)>
Date: Sun Jan 28 02:12:45 2018 -0500

newfile.c is added

How to remove or undo an existing commit from a branch?

The git revert command removes a commit from a branch. The original commit does not remove from history. But a new commit takes place on the head of the branch with undo changes.