Git

How to Undo rm in Git

Git is an independent versioning control system utilized for teamwork. It enables users to perform multiple operations, such as creating and deleting files, folders, and branches, to the Git repository. You can also revert the removed actions using the “$ git reset” command.

This write-up will elaborate on the procedure to undo the removing file operation(undo rm) in the Git directory.

How to Undo rm in Git?

To understand undo rm in Git, first, we will create a file and add it to the Git repository from the working area to the staging area. Then, commit changes, and remove the file from the directory utilizing the “$ git rm” command. After that, undo rm actions.

Let’s move toward the procedure!

Step 1: Launch Git Terminal

Launch the Git terminal using the “Startup” menu:

Step 2: Navigate to Folder

Next, move to the “Git” folder utilizing the “cd” command:

$ cd "C:\Users\nazma\Git"

Step 3: Create New Git Directory

Execute the “mkdir” command to create a new directory:

$ mkdir Linux_hint

Step 4: Move to Git Directory

Move to the newly created directory:

$ cd Linux_hint

Step 5: Create File

Now, create a new file using the “touch” command:

$ touch file

Step 6: Add File

Add the newly created file to the Git directory to track from the working directory to the staging area:

$ git add file

Step 7: Commit Changes

Save commit changes to the directory using the “git commit” command:

$ git commit -m "1 file created"

Step 8: Remove File

Utilize the “git rm” command to remove the file named “file” from Git index:

$ git rm file

As you can see, the file removed successfully from “Linux_hint” directory:

Step 9: Undo rm

After removing the file, revert it back with the help of the following command:

$ git reset

Here, the “file” is reverted successfully:

Step 10: Restore File

Run the “git checkout” command to reset the deleted file from the last check-in in the same repository:

$ git checkout file

We have illustrated the procedure to undo the removing file operation in the Git directory.

Conclusion

To undo rm in the Git repository, first, open the Git terminal. Move to the Git folder, create a new Git directory and add a new file. Then, add the file to track from the working area to the staging area. Next, commit changes to the Git repository. Now, delete the file from the repository and run the “$ git reset” command to undo rm. Lastly, run the “$ git checkout <file>” to restore the removed file from the last check-in. This write-up demonstrated the easiest way to undo rm in Git.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.