---
title: "Save the Day With Git and Remove From Commit History"
description: "Avoid the loss of sensitive data and secrets by learning to use git and remove from commit commands and save the day!"
canonical: "https://adamtheautomator.com/git-and-remove-from-commit/"
---

# Save the Day With Git and Remove From Commit History

> Avoid the loss of sensitive data and secrets by learning to use git and remove from commit commands and save the day!

Source: https://adamtheautomator.com/git-and-remove-from-commit/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Save the Day With Git and Remove From Commit History](https://adamtheautomator.com/wp-content/uploads/2022/05/Save-the-Day-With-Git-and-Remove-From-Commit-History.jpg)

# Save the Day With Git and Remove From Commit History

[![](https://secure.gravatar.com/avatar/2beb65fca997135120ed98dc6a2e57dcdf1a7d7d2f5ff687b5d91dc7ccd7a6b5?s=192&d=mm&r=g)Sagar](https://adamtheautomator.com/author/shanky-mendiratta/)26 May 20225 min. read

Categories: [DevOps](/category/devops/)

Tags:[Git](/tag/git/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Removing a File from Git Commit](#removing-a-file-from-git-commit)
*   [Removing All Recently Committed Files](#removing-all-recently-committed-files)
*   [Removing Specific Commits from Git History](#removing-specific-commits-from-git-history)
*   [Deleting a Directory from Git Commit History](#deleting-a-directory-from-git-commit-history)
*   [Conclusion](#conclusion)

Have you encountered any errors or faced issues with Git commits while working with Git? Perhaps an errant secret or sensitive data has been saved. Luckily, you can process your files with Git and remove from commit and fix the history.

If you’ve made several commits or cloned a repository with an already existing commit history, you’d typically want to look back and see what all commits were made. And in this tutorial, you’ll learn how to remove an entry from your Git history.

Read on and save the day with Git!

## Prerequisites

This tutorial comprises step-by-step instructions. If you’d like to follow along, be sure you have the following in place:

*   A local machine to test the Git commands – This tutorial uses Windows 10, but later versions will also work.
*   [Git Bash](https://adamtheautomator.com/git-bash/) – This tutorial uses Git Bash 2.36.

Related:[How to Get Started with Git Bash on Windows](https://adamtheautomator.com/git-bash/)

## Removing a File from Git Commit

When you work on Git as a team, you make hundreds of commits daily. As a result, there are high chances that you might need to undo a particular commit, such as due to an incorrect file or multiple files. This demand is usually caused by a change in code or configuration and becomes an issue.

To solve this issue, you’ll need to remove the file or multiple files from Git commit by running Git commands. You’ll use one of the popular tools to remove a single file from the Git commit, the Git Bash tool.

1\. Open the Git Bash tool on your Windows machine.

2\. Next, run the below command to create a repository folder and switch to that folder. These commands don’t provide output on the terminal. But be sure to replace ~/Desktop/Repository/repo1 with your preferred folder path and name.

```bash
# Creates the Repository folder and repo1 sub-folder
mkdir -p ~/Desktop/Repository/repo1
# Changes working directory to repo1
cd ~/Desktop/Repository/repo1
```

3\. Run the following commands to initialize your repository and create two text files named _test1.txt_ and _test2.txt._

```bash
# Initializes the local repository (creating a .git hidden folder)
git init
# Creates two text files
touch test_file1.txt test_file2.txt
```

![Initializes the local repository ](https://adamtheautomator.com/wp-content/uploads/2022/05/image-370.png)

Initializes the local repository

4\. Now, execute the below command to check the status of the Git commit.

```bash
git status
```

After you execute the command, you’ll notice that the commits are untracked and not yet committed, as shown below.

![Checking the status of Git commits on the Git terminal](https://adamtheautomator.com/wp-content/uploads/2022/05/image-371.png)

Checking the status of Git commits on the Git terminal

5\. Run the [git add](https://www.atlassian.com/git/tutorials/saving-changes) command below to add the two text files you created in step three (_test\_file1.txt_ and _test\_file2.txt_) to your local repository.

This command doesn’t provide an output but adds the files without pushing them to the remote repository.

```bash
git add .
```

6\. Next, run the git commit command below so that Git keeps track of the progress and changes you made in the local repository.

```powershell
git commit -m "Adding two new files test_file1.txt and text_file2.txt"
```

![Committing Changes to Repository](https://adamtheautomator.com/wp-content/uploads/2022/05/image-372.png)

Committing Changes to Repository

Related:[How to Check out a Remote Git Branch \[Step-by-Step\]](https://adamtheautomator.com/git-checkout-remote-branch/)

7\. After committing, run the below command to remove a specific file (test\_file2.txt) from Git commit.

```powershell
git rm --cached test_file2.txt
```

![Removing a Specific File from the Git Commit](https://adamtheautomator.com/wp-content/uploads/2022/05/image-373.png)

Removing a Specific File from the Git Commit

8\. Finally, rerun the git status command below to verify the status of the repository.

```powershell
git status
```

As you can see below, the file has been removed from the staging area (the Git commit) and shows the file (test\_file2.txt) under Untracked files.

![Verifying the Removed Git Commit (test\_file2.txt)](https://adamtheautomator.com/wp-content/uploads/2022/05/image-374.png)

Verifying the Removed Git Commit (_test\_file2.txt_)

## Removing All Recently Committed Files

You’ve just learned to remove a specific file from the Git commit. But typically, you’ll have to delete multiple files, and Git is up to the task with the [git reset](https://www.atlassian.com/git/tutorials/undoing-changes/git-reset) command. The git reset command is a complex but, at the same time, versatile tool for undoing changes in the repository.

To see how the git reset works, you’ll commit a couple of files, then reset the changes to remove recently committed files stored in Git commit:

1\. Create two new text files named _test\_file3.txt_ and _test\_file4.txt i_n the _~/Desktop/Repository/repo1_ folder.

```powershell
touch test_file3.txt test_file4.txt
```

2\. Next, run the git add command below, which doesn’t provide an output but adds the new text files to your local repository (_~/Desktop/Repository/repo1_).

```powershell
git add .
```

3\. Run the git commit command below to commit the files so that Git keeps track of the progress and changes you made in the repository.

```powershell
git commit -m "Adding two more files"
```

![Committing Changes to the Repository](https://adamtheautomator.com/wp-content/uploads/2022/05/image-375.png)

Committing Changes to the Repository

4\. Now, run the below command to verify the status of your local repository.

```powershell
git status
```

As you can see below, the output shows the nothing to commit message, indicating everything is in place in the repository.

![Checking the Status of Git Commits](https://adamtheautomator.com/wp-content/uploads/2022/05/image-376.png)

Checking the Status of Git Commits

5\. Next, run the git reset command below to revert the recently made commits in step three. This command doesn’t provide output but removes the files _test\_file3.txt_ and _test\_file4.txt_.

```powershell
git reset --soft HEAD~1
```

> _You can also use git reset –soft HEAD^ to remove all the files that were committed until now._

6\. Next, rerun the git status command below to verify the status of the repository.

```powershell
git status
```

Below, the output shows files test\_file3.txt and test\_file4.txt that need to be committed. This output signifies the files to be removed from Git commit.

![Verifying the Recent Commits were Removed](https://adamtheautomator.com/wp-content/uploads/2022/05/image-377.png)

Verifying the Recent Commits were Removed

## Removing Specific Commits from Git History

Removing all committed files works like a charm if you like to start with a clean slate. But what if you only need to remove a particular commit? The [git revert](https://www.atlassian.com/git/tutorials/undoing-changes/git-revert) command will do the trick so long as you know the commit’s ID.

In the last step of the “Removing All Recently Committed Files” section, you noticed that the commits were not committed but changed. You’ll now commit the changes to see the `git revert` command in action.

Run the `git commit` command below to commit the recent changes on your local repository.

```powershell
git commit -m "Hello, I am committing those two files again"
```

After running the command, you’ll notice that both the text files are committed again, and the commit ID is **0315b85,** as shown below.

![Committing the Changes in the Local Repository](https://adamtheautomator.com/wp-content/uploads/2022/05/image-378.png)

Committing the Changes in the Local Repository

Now, run the `git revert` command below to revert the commit you did in the previous step. Be sure to replace `0315b85` with your commit’s ID.

```powershell
git revert 0315b85
```

Below, you can see two files you recently committed have been deleted.

![Reverting the Changes in the Local Repository](https://adamtheautomator.com/wp-content/uploads/2022/05/image-379.png)

Reverting the Changes in the Local Repository

## **Deleting a Directory from Git Commit History**

By now, you already know how to delete a committed file, but how about a directory? Deleting a directory from a Git commit is similar to deleting files.

1\. Run the below commands, which don’t provide output but perform the following:

*   Create a folder named `myfolder` inside your local repository (_~/Desktop/Repository/repo1_).
    
*   Create a file named l.txt inside the _~/Desktop/Repository/repo1/myfolder_ folder.
    

```bash
mkdir myfolder && touch myfolder/l.txt
```

2\. Next, run the git commands below to add the new folder (_~/Desktop/Repository/repo1/myfolder_) to your local repository.

```bash
git add .
git commit -m "Adding folder"
```

![Adding the new folder (folder) to the Local Repository](https://adamtheautomator.com/wp-content/uploads/2022/05/image-380.png)

Adding the new folder (folder) to the Local Repository

3\. Run the following git rm command to remove the folder (myfolder) you committed in step two.

```powershell
git rm -r myfolder/
```

![Removing the New Folder (folder) in Git Commit](https://adamtheautomator.com/wp-content/uploads/2022/05/image-381.png)

Removing the New Folder (folder) in Git Commit

4\. Finally, run the git status command to verify the status of the repository and see if the folder has been removed.

```powershell
git status
```

Below, you can see that the entire folder (myfolder) has been deleted successfully, along with the l.txt file inside.

![Verifying the Folder (myfolder) Has Been Removed Successfully](https://adamtheautomator.com/wp-content/uploads/2022/05/image-382.png)

Verifying the Folder (_myfolder_) Has Been Removed Successfully

## Conclusion

In this tutorial, you’ve learned to work with Git commits, such as removing a specific commit, a file, or multiple files. You’ve also touched on deleting an entire folder within the local repository and committing the changes.

At this point, you can confidently commit changes on your repository and remove commits you deem unnecessary without messing things up.

With what you’ve learned in this tutorial, which method do you think could be excellent to work with commits for your team?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fgit-and-remove-from-commit%2F&text=Save%20the%20Day%20With%20Git%20and%20Remove%20From%20Commit%20History)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fgit-and-remove-from-commit%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fgit-and-remove-from-commit%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2024/01/gitops.jpg)

### [A Getting Started Guide to GitOps](/gitops/)

Embark on your GitOps journey—seamless, automated, and efficient. Explore the power of GitOps for streamlined workflows in this tutorial!

![](https://adamtheautomator.com/wp-content/uploads/2021/12/How-To-Combine-Branches-With-Git-Merge.jpg)

### [Git Merge Simplified: Merging Branches Like a Pro](/git-merge/)

Master Git merge with this tutorial: Learn seamless branch combination techniques and best practices to enhance your development workflow, avoiding common errors.

![](https://adamtheautomator.com/wp-content/uploads/2023/07/gitea-docker.jpg)

### [Gitea Docker: Your Ultimate Self-Hosted Git Solution](/gitea-docker/)

Learn how to install a Gitea Docker instance and self-host Git repositories securely in this ATA Learning tutorial!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
