FreeKB - Git (Version Control) git commit command
Git (Version Control) - git commit command

Clone

This assumes you are already cloned a repository from the source Git repo (typically called "origin") to a directory on your PC, such as the foo.git or bar.git repos.

 


Branch

After cloning a repository, by default, you will have a branch called "master" on your PC.

 


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

git log foo.txt

 

Notice in this example that "third commitment" is the latest commit.

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

 

The git commit command is used to create a new commit in your branch. In this example, a new commit of foo.txt will be created.

git commit foo.txt

 

Your default editor will open, where you will be prompted to enter a commit message. Enter a message, and save. If the commit is successful, the following will 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 your branch on your PC will be different from foo.txt in the origin Git repository, because you have not yet pushed the commit from your branch to the origin Git repository.

 


--message

The -m or --message option can be used to include the commit message, so that your default editor is not used to enter the commit message.

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

 


--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)

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




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




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |