JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Git >

Deleting committed files in Git

Author:JIYIK Last Updated:2025/04/02 Views:

Sometimes we realize that we need to manually find out which files are important for the current commit and remove the unneeded files from the working directory. The manual process can be difficult, especially when we are building a large project with many developers in a team.

Suppose we have committed a wrong file in our repository and want to remove it from our branch. git resetThe command is a useful tool when dealing with Git. This command is used to remove a specific file from the working directory in a specific Git commit or branch. For example, if you want to HEADremove a file from the commit pointed to by "README.md", you can run the following command.

git reset -soft HEAD@{1}

We can remove the files from a specific commit while keeping the changes in the working directory by running the following command.

git reset HEAD@{1}

As we know, the command used to remove files from a commit is git reset. This command can be used with the -pand -uoptions. -pThe option allows us to keep the changes in the working directory but reset the index. The option -uresets both the index and the working directory.

Let's practice removing a file from a commit with the following example. Suppose we have created a new commit and added a file1file called . Now, we want to remove this file from the commit. We can start this operation using the following command.

git reset -p HEAD~1

This command instructs Git to reset the index and working directory back to the commit HEAD~1. Before we created the commit, we file2added to the working directory. Now, we want to remove this file from the working directory but keep it in the index. We can start it with the following command:

git reset -u HEAD~1

This command instructs Git to reset the index and working directory to the commit HEAD~1. Now, suppose we have added three files to the index, and we want to remove file3all but one from the index. We can start with the following command:

git reset -- '-p -- file3'

This command instructs Git to keep file3and remove other files from the index within the same branch. The command git resetcan be used to remove files from any commit. The command can be -pused with the -p option to keep the changes in the index and working directory and remove files from the commit.

The command can also be -uused with the -reset option to reset the index and working directory but keep the files in the commit.

If we want to remove files from a specific commit, we can use the command with -hardthe -p option git resetand HEADspecify the commit after -p. For example, if we want to remove files from commit test "README.md", we can run the following command.

git reset -hard test

Keep in mind that the command git reset --harddiscards any changes you made to your working directory. If you edited some files and deleted others, the last command will remove any changes you made to the files that are still in your staging area.

However, let's say we don't want that file anymore and want to remove it from the staging area and make it an untracked file. In that case, we would use the --cached-whash command with the -dwhash option git rm. If we also wanted to remove it from the working directory, we would skip --cachedthe -whash option in the command.

$ git rm --cached <file-name>

Here we are almost done with the file recovery process. The irrelevant files have been removed from the staging area and all other changes have been staged. Now, we have to git commitcommit these changes to the branch using the command . We have now reverted our commit, but this does not include the unwanted files.

$ git commit -m "commit message"

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Git installation and establishment of local warehouse service

Publish Date:2025/04/05 Views:89 Category:Git

Git is a distributed version control system: the client does not only extract the latest version of the file snapshot, but also completely mirrors the original code repository. It has the following advantages: a. Since every extraction oper

git remote operation——multiple remote repositories for one project

Publish Date:2025/04/05 Views:131 Category:Git

Multiple remote repositories for a git project In our git project, the command to operate the remote repository information is $ git remote # 查看当前所有的远程仓库的名称 $ git remote -v # 查看远程仓库的名称和远程仓

Git cherry pick command usage

Publish Date:2025/04/05 Views:190 Category:Git

git cherry-pick is a powerful command that allows us to select an arbitrary Git commit by reference and attach it to the HEAD of the current working branch. Cherry picking is the act of picking a commit from one branch and applying it to an

Comparison between Git merge and Git rebase

Publish Date:2025/04/05 Views:171 Category:Git

The git rebase command may seem like Git wizardry to beginners, but if used carefully, it can actually make life easier for your development team. In this article, we compare git rebase with the related git merge command and identify all th

How to fix Git error Error: src refspec master does not match any

Publish Date:2025/04/05 Views:124 Category:Git

When using Git, we may encounter the error "src refspace master does not match any". Here's what the error means and how to fix it. What does src refspec master does not match any Mean in Git mean? We may encounter this error when we try to

Undo Git Stash

Publish Date:2025/04/04 Views:187 Category:Git

This article explains how to make and save changes to a repository. Git allows you to save changes locally and push them to a server when needed. In Git, we don't use the term save , but commit . We use git add , git commit , and git stash

View a list of cache entries in Git

Publish Date:2025/04/04 Views:59 Category:Git

We often need to pause our work and focus on something else in our development environment. Therefore, we may need to temporarily save our current work and focus on a different one. We may want to resume our original work later. git stash T

Git stores specific files

Publish Date:2025/04/04 Views:115 Category:Git

This article will cover storing changes to only specific files in Git. In Git, when we make some changes in our working tree, we may have some changes which may or may not be staged in our local repo. We may now wish to save these changes f

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial