How to stage and commit files using Git Bash

Home > Search > How-to
  by

By default, git will not track files. You need to tell git which files you want to track. Create a file named example.txt, and add Hello World to the file.

 

The git status command will show that example.txt is not being tracked.

$ git status
. . .
Untracked files:
. . .
    example.txt

 


Stage

Use the git add filename command to stage a file for commitment. In this example, the example.txt file is being staged.

$ git add example.txt

 

The git status command will now show that example.txt is ready to be committed.

$ git status
. . .
Changes to be committed:
. . .
  new file: example.txt

 


Commit

Use the git commit command with the -m (message) option to commit a file. In this example,1 file changed is displayed.

$ git commit -m 'First Commitment'
. . .
1 file changed, 1 insertion(+)
create mode 100644 example.txt

 

The git log command will show the First Commitment.

$ git log
Author: John Doe <john.doe@example.com>
Date:   Tue May 30 20:26:36 2017 -0500

  First commitment

 


Changes

Let's make a change to example.txt. The text How are you today is added to example.txt.

 

When a change is made to a staged or commited file, the file will be listed as modified in the git status command.

$ git status
. . .
  modified: example.txt

 

After staging and commiting example.txt again, the git log command will show the two commitments.

$ git log
. . .
  Second commitment
. . .
  First commitment

 


Working with numerous files

Let's say you have numerous files that you want to stage and commit. Instead of staging and commiting each file one by one, the -a option or a single period can be used to stage all of the files in the directory. For example, let's say C:\Users\john.doe\Sample contains example1.txt, example2.txt and example3.txt. The git add -A, or git add --all, or git add . command can be used to stage all of the files in the directory.

$ git add .

 

The git commit command will commit all three files that have been staged.

$ git commit -m 'First Commitment'
. . .
3 files changed, 3 insertion(+)
create mode 100644 example1.txt
create mode 100644 example2.txt
create mode 100644 example3.txt

 

There will typically be some files you do not want to stage and commit. To exclude files, create the .gitignore file.

$ touch .gitignore

 

Let's say you do not want to stage and commit files ending in .log. Add the following to the .gitignore file. You will probably want to stage and commit the .gitignore file.

*.log

 

Directories can be ignore by entering the directory name followed by a forward slash.

directoryname/

 



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 in the box below so that we can be sure you are a human.




Comments