如何从最新提交中删除文件?
当前回答
一些对我有用的东西,但仍然认为应该有更好的解决方案:
$ git revert <commit_id>
$ git reset HEAD~1 --hard
只需将要放弃的更改保留在另一个提交中,检查其他人
$ git commit --amend // or stash and rebase to <commit_id> to amend changes
其他回答
目前的答案都不合理。听起来有足够的需求,应该提出一个真正的解决方案:https://github.com/git/git/blob/master/Documentation/SubmittingPatches
git--取消提交<文件名>
很好。我知道我们不想修改历史记录,但如果我是本地人,无意中添加了本地“黑客”文件,并希望将其从提交中删除,这将非常有用。
以下内容将显示您想要的文件,这是OP要求的。
git reset HEAD^ /path/to/file
您将看到以下内容。。。
要提交的更改:(使用“git reset HEAD…”取消页面)修改:/path/to/file未提交的更改:(使用“git add…”更新将提交什么)(使用“gitcheckout--…”放弃工作目录中的更改)修改:/path/to/file
“要提交的更改”是提交之前文件的先前版本。如果文件从未存在,这看起来像是删除。如果您提交此更改,则会有一个修订将更改还原为分支中的文件。“未提交的更改”是您提交的更改,以及文件的当前状态
此时,您可以对文件执行任意操作,例如重置为不同的版本。
当您准备好承诺时:
git commit --amend -a
或者(如果您还有其他不想提交的更改)
git commit add /path/to/file
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>
Here is the step to remove files from Git Commit.
>git reset --soft HEAD^1(either commitid ) -- now files moved to the staging area.
>git rm --cached filename(it will removed the file from staging area)
>git commit -m 'meaningfull message'(Now commit the required files)
如果尚未在服务器上推送更改,则可以使用
git reset --soft HEAD~1
它将重置所有更改并恢复为一次提交
如果您已经推动了更改,请按照@CharlesB回答的步骤操作
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的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之间的区别