Stage only selected files and commit them separately

Description: You changed index.html, about.html and style.css. Make two commits: one with the two html files and one with only the css file.

Hint:

  • Stage the html files first with a pattern, commit, then stage the css file.

Solution

Terminal output

$ git add *.html
$ git commit -m "Update html pages"
[main 3f16559] Update html pages
2 files changed, 2 insertions(+), 2 deletions(-)
$ git add style.css
$ git commit -m "Update style"
[main 3ce526e] Update style
1 file changed, 1 insertion(+), 1 deletion(-)
$ git log –oneline
3ce526e (HEAD -> main) Update style
3f16559 Update html pages
333d1be Start

How it works

  • The staging area lets us choose which files go in a commit.
  • Two small commits are easier to review than one big commit.

Read Git Add.


Learn more about the similar topics:
Tutorials
Git Init
Git Status
Git Add
Git Commit
Git Log
Git Diff
Git Ignore (.gitignore)
Exercises & Assignments
Check the status of files in a Git repository
Stage only selected files and commit them separately
Show the commit history in one line per commit
See the changes of a file before staging and before commit
Ignore all .log files with .gitignore
Unstage a file
Delete a file with git rm
Interview Questions & Answers
How can you add files to Git?
How can you commit a file in Git?
What are the common files to ignore in Git?
How to remove untracked files in Git?
What is the staging area in Git?
What is the difference between git add and git commit?
What is HEAD in Git?