0

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.

1
  • 1
    This is where the fact that Git makes new commits from whatever is in the index, not whatever is in the work-tree, is helpful. The index is also called the staging area, perhaps because you arrange files on/in it like you would on a stage for filming or photography, before you use git commit to make the snapshot. Commented Aug 23, 2018 at 17:43

2 Answers 2

2

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"
Sign up to request clarification or add additional context in comments.

Comments

1

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.