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

时间:2018-08-23 11:36:50

标签: 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?我曾考虑过创建一个新分支,然后将所做的更改移至该分支,然后提交它们,但是我不知道如何仅移动这些文件的一部分。

2 个答案:

答案 0 :(得分:2)

执行该操作,就像执行其他任何提交一样:

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

答案 1 :(得分:1)

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

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"