Predictive Hacks

How to Delete a File from a Remote Git Repository

When we work with Git and GitHub/GitLab, we can accidentally push a file to a remote Git Repository and as a result, there is a need to remove it. As usual, we will work with walk-through examples. We will create a remote GitHub repo and then we will clone it locally.

$ git clone https://github.com/pipinho13/delete_example.git

Then we change the working directory to the cloned repository and as we can see there is the README.md and the .git file

Now we will create a file called “wrong.txt” that we will push to the remote repo.

$ ls -ltra
total 6
drwxr-xr-x 1 gpipis 1049089  0 Jan 13 18:57 ../
-rw-r--r-- 1 gpipis 1049089 16 Jan 13 18:57 README.md
drwxr-xr-x 1 gpipis 1049089  0 Jan 13 18:57 .git/
drwxr-xr-x 1 gpipis 1049089  0 Jan 13 19:12 ./
-rw-r--r-- 1 gpipis 1049089 21 Jan 13 19:12 wrong.txt

Let’s push the “wrong.txt”.

$ git add wrong.txt
$ git commit -m "pushing the wrong.txt file"
$ git push origin main

The “wrong.txt” file has been added to the remote repository.

How to Delete the File

Now, let’s say that we want to delete the file from both the remote directory and the local file system. Then we should run the following commands.

$ git rm wrong.txt
$ git commit -m "remove the wrong.txt file"
$ git push origin main
 

As we can see, the file has been removed from both the local file system and remote repository.

$ ls -ltra
total 5
drwxr-xr-x 1 gpipis 1049089  0 Jan 13 18:57 ../
-rw-r--r-- 1 gpipis 1049089 16 Jan 13 18:57 README.md
drwxr-xr-x 1 gpipis 1049089  0 Jan 13 19:23 ./
drwxr-xr-x 1 gpipis 1049089  0 Jan 13 19:24 .git/

How to Delete the File from the Remote Git Repo but Keep it Locally

We will repeat the process again by creating the “wrong.txt” file, where at this time we will delete it from the remote repo only.

$ ls -ltra
total 6
drwxr-xr-x 1 gpipis 1049089  0 Jan 13 18:57 ../
-rw-r--r-- 1 gpipis 1049089 16 Jan 13 18:57 README.md
drwxr-xr-x 1 gpipis 1049089  0 Jan 13 19:24 .git/
drwxr-xr-x 1 gpipis 1049089  0 Jan 13 19:33 ./
-rw-r--r-- 1 gpipis 1049089 13 Jan 13 19:33 wrong.txt

Let’s push the wrong.txt file.

$ git add wrong.txt
$ git commit -m "pushing the wrong.txt file again"
$ git push origin main

Now, let’s remove the “wrong.txt” file only from the remote repo. For this task, we must use the tag “cached

$ git rm --cached wrong.txt
$ git commit -m "remove the wrong.txt file only from remote repo"
$ git push origin main
$ ls -ltra
total 6
drwxr-xr-x 1 gpipis 1049089  0 Jan 13 18:57 ../
-rw-r--r-- 1 gpipis 1049089 16 Jan 13 18:57 README.md
drwxr-xr-x 1 gpipis 1049089  0 Jan 13 19:33 ./
-rw-r--r-- 1 gpipis 1049089 13 Jan 13 19:33 wrong.txt
drwxr-xr-x 1 gpipis 1049089  0 Jan 13 19:38 .git/

Voilà, we removed the “wrong.txt” only from the remote repo!

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.

Python

Intro to Chatbots with HuggingFace

In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s