Git在我的情况下提交

Lee*_*fin 2 git github

我在分支" feature".在我完成所有更改之后,我决定在这个分支上完成我的工作.

我首先检查了执行命令所做的所有更改:

git status
Run Code Online (Sandbox Code Playgroud)

上面的git命令打印出以下结果:

On branch feature
# 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:   MyApp/src/Main.java
#   modified:   MyApp/src/Model.java

# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   MyApp/src/other/file1
#   MyApp/src/other/file2
Run Code Online (Sandbox Code Playgroud)

如上所示,有两个未跟踪的文件file1file2.我根本不想将它们放在我的git存储库中.但出于某种原因,我忘记了这两个文件并通过以下方式直接提交了我

git add .
git commit -m "Some changes"
Run Code Online (Sandbox Code Playgroud)

此时,包括file1&的所有更改都将file2被提交.现在,我怎样才能拯救自己以排除 file1&file2仍然保留其他修改后的更改?

(请不要建议我删除file1file2磁盘,我需要他们在我的本地磁盘上)

Sto*_*ica 5

在您提交并意识到您不想添加这些文件之后,您可以像这样修复:

git reset HEAD^ --soft
git reset MyApp/src/other/file1 MyApp/src/other/file2
git commit -m "Some changes"
Run Code Online (Sandbox Code Playgroud)

git reset HEAD^ --soft将撤消上次提交:您的工作目录将恢复到提交之前的状态.由于该--soft标志,暂存区域将保持原样,因此您可以使用git reset并再次提交来取消暂存这两个文件.

如果您不想将这些文件添加到版本控制中,那么您可能希望通过将它们添加到.gitignore项目文件中来忽略它们:

echo MyApp/src/other/file1 >> .gitignore
echo MyApp/src/other/file2 >> .gitignore
Run Code Online (Sandbox Code Playgroud)

PS:谢谢你@hekdev--soft提示.