Arian
Arian

Reputation: 7719

Git: Commit a subset of modified/untracked files

I made some changes on my master branch. Now I want to persist my changes in several commits.

Let's say this is the result of git status:

On branch master
Your branch is up-to-date with 'origin/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:   ../../../.gitignore
    modified:   ../java/file1.java
    modified:   ../java/file2.java
    modified:   package-lock.json
    modified:   package.json
    modified:   src/index.js

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    src/components/authentication/
    src/components/route/
    src/helpers/

How can I commit file1 and file2 in one commit, and package.json and package-lock.json in the next commit? I thought of creating a new branch and moving my changes to that branch and then commit them, but I don't know how to move only a subset of these files.

Upvotes: 0

Views: 2413

Answers (2)

Bouke
Bouke

Reputation: 1577

Just only add the files to the index you want to commit.

git add ../java/file1.java
git add ../java/file1.java
git commit -m "First commit"

git add package-lock.json
git add package.json
git commit -m "Second commit"

Upvotes: 1

gogaz
gogaz

Reputation: 2400

Do it like you would do any other commit :

git add ../java
git commit -m "Updated file1 and file2"
git add package*.json
git commit -m "Updated package.json"

Upvotes: 2

Related Questions