Git Add和Commit选择的文件

问题描述 投票:1回答:2

我在整个项目中进行了大约30次文件更改,以便使用调试语句和一些变量进行测试。现在我的代码正在按照我的要求工作。

我的代码更改只有3个文件。我已经完成了以下命令来推送到我的分支机构

 1.  git status 
 2.  git add .
 3.  git commit -m "Commited file changes"
 4.  git push origin MyBranch (This push is to a sub branch development of Master Branch)

现在,当我查看GitHub存储库时,可以在pull请求中看到这30个文件。

我只想提交3个文件。我删除了之前调试的其他27个文件,它们与master中的相同。我该怎么做只提交3个文件。

我试过了

 git add file1 file2 files3  

但它没有用。

git github
2个回答
1
投票

您需要先将索引重置为上次提交之前的状态(最后一次提交包含30个文件而不是3个)

git reset @~

@用于HEAD,~用于第一个父提交)

然后你可以再试一次

git add file1 file2 files3  
git commit -m "commit only 3 files"

你需要强制推送,这将更新PR,并使用新的提交覆盖其历史记录:

git push --force -u origin myBranch

1
投票

如果您尝试以下步骤,它将工作:

1.  git status 
2.  git add file1
3.  git add file2
4.  git add file3
5.  git commit -m "Commited file changes"
6.  git push origin MyBranch or master

通过逐个添加文件可以减少失败的机会。

© www.soinside.com 2019 - 2024. All rights reserved.