如何从最新提交中删除文件?
当前回答
一些对我有用的东西,但仍然认为应该有更好的解决方案:
$ git revert <commit_id>
$ git reset HEAD~1 --hard
只需将要放弃的更改保留在另一个提交中,检查其他人
$ git commit --amend // or stash and rebase to <commit_id> to amend changes
其他回答
我认为这里的其他答案是错误的,因为这是一个将错误提交的文件从上一次提交移回临时区域的问题,而不取消对它们所做的更改。这可以像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
执行以下命令序列:
//to remove the last commit, but preserve changes
git reset --soft HEAD~1
//to remove unneded file from the staging area
git reset HEAD `<your file>`
//finally make a new commit
git commit -m 'Your message'
如果你还没有将你的更改推送到git
git reset --soft HEAD~1
它将重置所有更改并恢复为一次提交
如果这是您最后一次提交,并且您希望从本地和远程存储库中删除文件,请尝试以下操作:
git rm <file>
git commit --amend
甚至更好:
先重置
git reset --soft HEAD~1
重置不需要的文件
git reset HEAD path/to/unwanted_file
再次提交
git commit -c ORIG_HEAD
一些对我有用的东西,但仍然认为应该有更好的解决方案:
$ git revert <commit_id>
$ git reset HEAD~1 --hard
只需将要放弃的更改保留在另一个提交中,检查其他人
$ git commit --amend // or stash and rebase to <commit_id> to amend changes
这是我从比特桶回购中删除文件的工作,我最初将文件推送到分支。
git checkout origin/develop <path-to-file>
git add <path-to-file>
git commit -m "Message"
git push
推荐文章
- 如何点Go模块的依赖在Go。Mod到回购中的最新提交?
- 为什么调用git分支——unset-upstream来修复?
- Windows git“警告:LF将被CRLF取代”,这是警告尾巴向后吗?
- git中的哈希冲突
- git可以自动在空格和制表符之间切换吗?
- Git暂存文件列表
- 如何将git配置存储为存储库的一部分?
- 如何修改GitHub拉请求?
- 如何在Github和本地删除最后n次提交?
- 我如何调试git/git-shell相关的问题?
- 错误:无法使用rebase进行拉取:您有未分阶段的更改
- Git隐藏未缓存:如何把所有未分期的变化?
- 真实的恶魔
- 如何从另一个分支获得更改
- Git:权限被拒绝(publickey)致命-无法从远程存储库读取。克隆Git存储库时