Git:提交已修改/未跟踪文件的子集



我对我的主分支进行了一些更改。现在我想在几个提交中保留我的更改。

假设这是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/

如何在一次提交中提交file1file2,并在下一次提交中提交package.jsonpackage-lock.json?我想创建一个新分支并将我的更改移动到该分支,然后提交它们,但我不知道如何仅移动这些文件的子集。

像做任何其他提交一样做:

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

只需将文件添加到要提交的索引中即可。

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"

最新更新