首页 文章

从Git提交中删除文件

提问于
浏览
1196

我正在使用Git,我已经提交了几个文件

git commit -a

后来,我发现错误地将一个文件添加到了提交中 .

如何从上次提交中删除文件?

21 回答

  • 11

    只是想补充最佳答案,因为我必须运行一个额外的命令:

    git reset --soft HEAD^
    git checkout origin/master <filepath>
    

    干杯!

  • 0

    我认为这里的其他答案是错误的,因为这是一个将错误提交的文件从前一次提交移回暂存区域的问题,而不取消对它们所做的更改 . 这可以像Paritosh Singh建议的那样完成:

    git reset --soft HEAD^
    

    要么

    git reset --soft HEAD~1
    

    然后重置不需要的文件,以便将它们从提交中删除:

    git reset HEAD path/to/unwanted_file
    

    现在再次提交,您甚至可以重用相同的提交消息:

    git commit -c ORIG_HEAD
    
  • 33

    ATTENTION !如果您只想从之前的提交中删除文件,并将其保留在磁盘上,请阅读上面的juzzlin's answer .

    如果这是您的最后一次提交,并且您想 completely delete the file from your local and the remote repository ,您可以:

    • 删除文件 git rm <file>

    • 提交修正标志: git commit --amend

    修正标志告诉git再次提交,但是“合并”(不是在合并两个分支的意义上)这个提交与最后一次提交 .

    正如评论中所述,在这里使用 git rm 就像使用 rm 命令一样!

  • 3

    对我有用的东西,但仍然认为应该有更好的解决方案:

    $ git revert <commit_id>
    $ git reset HEAD~1 --hard
    

    只需在其他提交中保留要丢弃的更改,请检查其他提交

    $ git commit --amend // or stash and rebase to <commit_id> to amend changes
    
  • 116

    git reset --soft HEAD^ 收回您的提交,当您键入 git status 时,它会告诉您该怎么做:

    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
  • 83

    实际上,我认为更快更简单的方法是使用git rebase交互模式 .

    git rebase -i head~1
    

    (或者~~ 4,你想走多远)

    然后,使用'编辑'而不是'选择' . 我没有意识到“编辑”有多强大 .

    https://www.youtube.com/watch?v=2dQosJaLN18

    希望你会发现它很有帮助 .

  • 8

    现有的答案都在谈论从 last 提交中删除不需要的文件 .

    如果要从 old 提交中删除不需要的文件(甚至推送),并且不希望创建新的提交,这是不必要的,因为操作:

    1 .

    找到您希望文件符合的提交 .

    git checkout <commit_id> <path_to_file>
    

    如果要删除多个文件,可以多次执行此操作 .

    2 .

    git commit -am "remove unwanted files"
    

    3 .

    找到提交 on which the files were added mistakenly 的commit_id,让我们说"35c23c2"在这里

    git rebase 35c23c2~1 -i  // notice: "~1" is necessary
    

    此命令根据您的设置打开编辑器 . 默认的是vim .

    将最后一次提交(应为"remove unwanted files")移动到错误提交的下一行(在我们的例子中为"35c23c2"),并将命令设置为 fixup

    pick 35c23c2 the first commit
    fixup 0d78b28 remove unwanted files
    

    保存文件后你应该很好 .

    完成 :

    git push -f
    

    如果您不幸遇到冲突,则必须手动解决 .

  • 35

    如接受的答案所示,您可以通过重置整个提交来完成此操作 . 但这是一个相当沉重的方法 .
    更简洁的方法是保持提交,只需从中删除更改的文件即可 .

    git reset HEAD^ -- path/to/file
    git commit --amend --no-edit
    

    git reset 将获取上一次提交中的文件,并将其放在索引中 . 工作目录中的文件不受影响 .
    然后 git commit 将提交并压缩索引到当前提交 .

    这基本上采用了先前提交中的文件版本,并将其添加到当前提交中 . 这导致没有净更改,因此文件有效地从提交中删除 .

  • 1

    如果您尚未在服务器上推送更改,则可以使用

    git reset --soft HEAD~1
    

    它将重置所有更改并恢复为一次提交

    如果您已推送更改,请按照@CharlesB所述的步骤进行操作

  • 3

    使用rm删除文件将删除它!

    你总是在git中添加提交而不是删除,所以在这个实例中将文件返回到第一次提交之前的状态(如果文件是新的,这可能是删除'rm'动作)然后重新提交,文件将继续 .

    要将文件返回到某个先前的状态:

    git checkout <commit_id> <path_to_file>
    

    或将其返回到远程HEAD的状态:

    git checkout origin/master <path_to_file>
    

    然后修改提交,你应该发现文件已从列表中消失(并且没有从磁盘中删除!)

  • 2305
    git checkout HEAD~ path/to/file
    git commit --amend
    
  • 5

    以下将仅展示您想要的文件,这是OP所要求的 .

    git reset HEAD^ /path/to/file
    

    你会看到类似下面的东西......

    要提交的更改:(使用“git reset HEAD ...”取消)已修改:/ path / to / file更改未进行提交的更改:(使用“git add ...”更新将提交的内容)(使用“git checkout - ...”来丢弃工作目录中的更改)修改:/ path / to / file

    • "Changes to be committed"是提交前的文件的先前版本 . 如果文件从不存在,这将看起来像删除 . 如果您提交此更改,则会有一个修订版本将更改恢复为分支中的文件 .

    • "Changes not staged for commit"是您提交的更改以及文件的当前状态

    此时,您可以对文件执行任何操作,例如重置为其他版本 .

    当你准备提交时:

    git commit --amend -a
    

    或者(如果你还有其他一些你不想提交的修改)

    git commit add /path/to/file
    git commit --amend
    
  • 2

    我将以示例向您解释 .
    设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>
    
  • 235

    如果你想保留你的提交(也许你已经花了一些时间编写详细的提交消息而不想丢失它),你只想从提交中删除文件,而不是完全从存储库中删除:

    git checkout origin/<remote-branch> <filename>
    git commit --amend
    
  • 28

    使用git GUI可以简化从先前提交中删除文件的过程 .

    假设这不是't a shared branch and you don',那么跑:

    git gui citool --amend
    

    您可以取消选中错误提交的文件,然后单击“提交” .

    enter image description here

    该文件将从提交中删除,但将保留在磁盘上 . 因此,如果您在错误地添加文件后取消选中该文件,它将显示在您未跟踪的文件列表中(如果您在错误地修改文件后取消选中该文件,则会在您的更改中显示未提交的提交列表) .

  • 4
    git rm --cached <file_to_remove_from_commit_<commit_id>_which_added_file>
    git commit -m "removed unwanted file from git"
    

    将保留本地文件 . 如果您不想在本地使用该文件,则可以跳过--cached选项 .

    如果所有工作都在您的本地分支上,您需要将文件保留在以后的提交中,并且就像拥有干净的历史记录一样,我认为更简单的方法可能是:

    git rm --cached <file_to_remove_from_commit_<commit_id>_which_added_file>
    git commit --squash <commit_id>
    git add <file_to_remove_from_commit_<commit_id>_which_added_file>
    git commit -m "brand new file!"
    git rebase --interactive <commit_id>^
    

    然后,您可以轻松完成rebase,而无需记住更复杂的命令或提交消息或输入更多内容 .

  • 35

    执行以下命令序列:

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

    有同样的问题,我在本地分支中有更改,我想只恢复一个文件 . 对我有用的是 -

    feature/target_branch 下面是我所有更改的地方,包括我想要为特定文件撤消的更改)

    origin/feature/target_branch 是我要将更改推送到的远程分支)

    feature/staging 是我的临时暂存分支,我将从我所有想要的更改中推送,不包括对该文件的更改)

    • 从我的 origin/feature/target_branch 创建一个本地分支 - 称之为 feature/staging

    • 将我的工作本地分支 feature/target_branch 合并到 feature/staging 分支

    • 已检出 feature/staging 然后 git reset --soft ORIG_HEAD (现在,功能/暂存的所有更改都将暂存但未提交 . )

    • 取消了我之前签入的文件,并进行了不必要的更改

    • feature/staging 的上游分支更改为origin / feature / target_branch

    • 承诺其余的分阶段更改并将上游推送到我的远程origin / feature / target_branch

  • 8

    如果您不再需要该文件,您可以这样做

    git rm file
    git commit --amend
    git push origin branch
    
  • 9

    如果您正在使用GitHub并且尚未推送提交,GitHub Desktop可以轻松解决此问题:

    • 选择存储库 - >撤消最近提交

    • 取消选择您错误添加的文件 . 您之前的提交消息已经在对话框中 .

    • 按下提交按钮!

  • 0

    这对我有用,可以从bit bucket repo中删除文件,我最初将文件推送到分支 .

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

相关问题