如何从最新提交中删除文件?
当前回答
我将用例子向你解释。设A、B、C为3次连续提交。提交B包含一个不应提交的文件。
git log # take A commit_id
git rebase -i "A_commit_ID" # do an interactive rebase
change commit to 'e' in rebase vim # means commit will be edited
git rm unwanted_file
git rebase --continue
git push --force-with-lease <branchName>
其他回答
我认为这里的其他答案是错误的,因为这是一个将错误提交的文件从上一次提交移回临时区域的问题,而不取消对它们所做的更改。这可以像Pariosh Singh建议的那样:
git reset --soft HEAD^
or
git reset --soft HEAD~1
然后重置不需要的文件,以便将它们从提交中删除(旧方法):
git reset HEAD path/to/unwanted_file
注意,自从Git 2.23.0以来,人们可以(以新的方式):
git restore --staged path/to/unwanted_file
现在再次提交,您甚至可以重复使用相同的提交消息:
git commit -c ORIG_HEAD
我们可以采取的另一种方法是:
删除文件使用删除的文件进行新提交执行交互式重基并压缩两个提交推
这是我从比特桶回购中删除文件的工作,我最初将文件推送到分支。
git checkout origin/develop <path-to-file>
git add <path-to-file>
git commit -m "Message"
git push
如果您想保留提交(可能您已经花了一些时间编写了详细的提交消息,不想丢失它),并且您只想从提交中删除文件,而不是完全从存储库中删除文件:
git checkout origin/<remote-branch> <filename>
git commit --amend
我将用例子向你解释。设A、B、C为3次连续提交。提交B包含一个不应提交的文件。
git log # take A commit_id
git rebase -i "A_commit_ID" # do an interactive rebase
change commit to 'e' in rebase vim # means commit will be edited
git rm unwanted_file
git rebase --continue
git push --force-with-lease <branchName>
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别