如何从最新提交中删除文件?


当前回答

这是我从比特桶回购中删除文件的工作,我最初将文件推送到分支。

git checkout origin/develop <path-to-file>
git add <path-to-file>
git commit -m "Message"
git push

其他回答

git reset --soft HEAD~1. 

这将撤消本地回购中的最后一次提交,并将所有内容移回提交前的阶段区域。然后,只需正常使用任何Git UI工具(如TortoiseGit、Git UI、Git Extensions…)来取消我们不想提交的文件的页面,然后再次提交。

最好的方法是使用SourceTree(也可以根据需要从命令行执行),并且:

检查新分支机构(出于安全原因)然后在要更改的提交之前使用gitreset-soft进行提交它将允许您删除(暂存/取消暂存)不需要的文件:)

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)

我将用例子向你解释。设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>    

执行以下命令序列:

//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'