git commit

Save Changes with 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 is to update the repository for changes done on a cloned copy of the project. After commit, 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, later git push command, sends the change to the central repository.

Commit in a git repository

I this tutorial we will learn how to commit a new and modified file using a command-line interface. Along with commit for all updated files.


How to Commit a new file?

In software development life-cycle, a new resource (e.g. new file in the source code ) is always added. 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 (called example.c)  in the git repository.

[[email protected]_6_64-152 my_git_respositorty]#  git add example.c

[[email protected]_6_64-152 my_git_respositorty]# git commit -m "example.c is added"

To see the commit history

[email protected]_6_64-152 my_git_respositorty]# git log
commit 0371cf00c44f1ed73b107976a70866eb5c2f62b0
Author: root <[email protected]_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, the code changes are common. Git provides a way to find out all changes and to 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 next commit with git add command. In this example, a file file1.txt is the change file.

Git list commits:

[[email protected]_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 working directory)
#
# modified: file1.txt

Add the file for next commit:

[[email protected]_6_64-152 my_git_respositorty]# git add file1.txt

Commit again with new list to commit:

[[email protected]_6_64-152 my_git_respositorty]# git commit -m “Updated files commit”
[master 6e9a7ad] Updated files commit

Verify the commit or git list commits:

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

Updated files commit

commit 0371cf00c44f1ed73b107976a70866eb5c2f62b0
Author: root <[email protected]_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 list of all files and its ok to commit all files,  git provides option for committing all files without git add.

[[email protected]_6_64-152 my_git_respositorty]# git commit -a -m “Commit without git add”
[master 88792a1] Commit without git add

Git list commits:

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

Commit without git add

commit a6506b29b55e09ad76378d26336984ce6e63fbea
Author: root <[email protected]_6_64-152.(none)>
Date: Sun Jan 28 02:32:02 2018 -0500

New file2

commit 6e9a7ad3181bf5d8a627611ad2150bc570878c26
Author: root <[email protected]_6_64-152.(none)>
Date: Sun Jan 28 02:26:27 2018 -0500

Updated files commit

commit 0371cf00c44f1ed73b107976a70866eb5c2f62b0
Author: root <[email protected]_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.

Leave a Comment