Bootstrap FreeKB - Git (Version Control) - Commit a file using the git commit command
Git (Version Control) - Commit a file using the git commit command

Updated:   |  Git (Version Control) articles

The most basic way to use Git is to use the git clone command to clone an origin Git repository (such as example.git) to a directory on your PC (such as /home/john.doe/git), make a change to a file in the cloned repository on your PC (such as example.txt), use the git commit command to commit the change to the file, and to then use the git push command to upload the file to the origin Git repository.

 


Let's say there is a file named foo.txt in the currently selected branch of the cloned repository. The git log command can be used to view the history of commits.

AVOID TROUBLE

If the git log command does not return any output, this might be because the git commit command has not yet been used to commit the file to the currently selected branch of the cloned repository.

git log foo.txt

 

Notice in this example "third commitment" is the latest commit. HEAD is usually the latest commit in the currently selected branch.

commit  mks910122020slsmm3lsosos020399489sl
Author: John Doe <john.doe@example.com>
Date:   Wed May 31 14:51:14 2020 -0500

  Third commitment

commit dkci85474fjfdkd9393934k49f9fk002kd01
Author: John Doe <john.doe@example.com>
Date:   Tue May 30 18:23:36 2020 -0500

  Second commitment

commit fj83m3ld0d0d3m3ld0389303l3ld0d0d39dl
Author: John Doe <john.doe@example.com>
Date:   Mon May 29 20:26:09 2020 -0500

  First commitment

 

Let's say you do not make any changes to foo.txt. In this example, the git status command returns nothing to commit, working directory clean since no changes have been made to foo.txt.

~]$ git status foo.txt
# On branch master
nothing to commit, working directory clean

 

Let's say you make a change to foo.txt. Now the git status command should return the following.

]$ git status foo.txt
# On branch master
# 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:   foo.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

 

The git commit command is used to create a new commit. Your default editor will open, where you will be prompted to enter a commit message. Enter a message, and save.

git commit foo.txt

 

Better yet, the -m or --message option can be used to include the commit message on the command line.

git commit --message 'First Commitment' foo.txt

 

If the commit is successful, something like this should be displayed.

1 file changed, 1 insertion(+)

 

Reissuing the git log command should show the new commit.

commit  4f92bf5356b525b282db6af250eb927824663500
Author: John Doe <john.doe@example.com>
Date:   Wed Jul 17 14:51:14 2020 -0500

  Fourth commitment

commit  mks910122020slsmm3lsosos020399489sl
Author: John Doe <john.doe@example.com>
Date:   Wed May 31 14:51:14 2020 -0500

  Third commitment

commit dkci85474fjfdkd9393934k49f9fk002kd01
Author: John Doe <john.doe@example.com>
Date:   Tue May 30 18:23:36 2020 -0500

  Second commitment

commit fj83m3ld0d0d3m3ld0389303l3ld0d0d39dl
Author: John Doe <john.doe@example.com>
Date:   Mon May 29 20:26:09 2020 -0500

  First commitment

 

Now, foo.txt in the currently selected branch of the cloned repository will be different from foo.txt in the origin Git repository, which can be seen with the git diff command, because you have not yet pushed the commit from your branch to the origin Git repository.

~]# git diff origin master
diff --git a/foo.txt b/foo.txt
index bcd9498..8932028 100755
--- a/foo.txt
+++ b/foo.txt
+ Hello World

 

 


--dry-run

The --dry-run option can be used to test a commit, to determine if the commit will be successful.

git commit --dry-run foo.txt

 

If the dry run believes that the commit will be successful, the following will be displayed.

Changes to be committed

 

If the dry run finds that the commit will fail, the following will be displayed.

nothing to commit (working directory clean)

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter ec7768 in the box below so that we can be sure you are a human.