dchhetri
dchhetri

Reputation: 7136

How can I remove all files in my git repo and update/push from my local git repo?

Is it possible to remove all files in a repository and update it with only the files I have in my local machine? The reason is that, there are certain files that is not necessary in my github and so I want to remove those files. Instead of removing the files one by one, I wanted to see if its possible to just remove all files in my git repo and update/push with only the files in my local machine. Hope its clear. Thanks.

Upvotes: 94

Views: 351701

Answers (16)

Wahab Khan Jadon
Wahab Khan Jadon

Reputation: 1196

  1. remove all files : git rm -r --cached .
  2. update .gitignore
  3. Readd all files back: git add .
  4. commit files with your message : git commit -m "Removed all ignored files from tracking"
  5. push: git push origin <branch>

Upvotes: 1

Valentin H
Valentin H

Reputation: 7458

Remove all files not belonging to a repositiory (e.g. for a clean-build after switching a branch):

 git status | xargs rm -rf

Upvotes: -1

Akash Kandpal
Akash Kandpal

Reputation: 3386

Delete all elements in repository:

git rm -r * -f -q

then:

git commit -m 'Delete all the stuff'

then:

git push -u origin master

then:

Username for : "Your Username" 
Password for : "Your Password"

Upvotes: 1

Bruno Yuzo
Bruno Yuzo

Reputation: 517

Just running it, solved for me:

git rm -r --cached .
git add .
git commit -m "remove gitignore files"
git push

as answered here: https://stackoverflow.com/a/50675909/3294762

Upvotes: 2

Varun P V
Varun P V

Reputation: 1233

First of all, Navigate to your Folder using cd ( change directory) command. Then make sure you are in the correct git branch which you want to work by using the command

git branch

If you want to delete the entire files. you can do the same by using

git rm -r .

for deleting a single file,

git rm file1.txt ( file1.txt - file Name )

for delete a folder,

git rm -r foldername

After deleting the files or folders, you should commit it:

git commit -m "your comment"

Then you can push the branch:

git push
// for example, 
git push origin develop

(it will update the origin repository)

Upvotes: 7

pascal
pascal

Reputation: 2713

You could do it like this:

cd /tmp
git clone /your/local/rep  # make a temp copy
cd rep
git rm -r *                # delete everything
cp -r /your/local/rep/* .  # get only the files you want
git add *                  # add them again
git status                 # everything but those copied will be removed
git commit -a -m 'deleting stuff'
cd /your/local/rep
git pull /tmp/rep          # now everything else has been removed

There's probably a oneliner for that…

Upvotes: 76

Chaman Bharti
Chaman Bharti

Reputation: 498

I have tried like this

git rm --cached -r * -f

And it is working for me.

Upvotes: 1

Daniel Miradakis
Daniel Miradakis

Reputation: 121

If you prefer using GitHub Desktop, you can simply navigate inside the parent directory of your local repository and delete all of the files inside the parent directory. Then, commit and push your changes. Your repository will be cleansed of all files.

Upvotes: 1

Jorgesys
Jorgesys

Reputation: 126563

Warning: this will delete your files, make sure you have a backup or can revert the commit.

Delete all elements in repository:

$ git rm -r *

then:

$ git commit -m 'Delete all the stuff'

Upvotes: 20

Luis Miguel Ramos
Luis Miguel Ramos

Reputation: 39

I was trying to do :

git rm -r *

but at the end for me works :

git rm -r .

I hope it helps to you.

Upvotes: 4

HoCo_
HoCo_

Reputation: 1372

In my case

git rm -r .

made the job

Upvotes: 14

user7379767
user7379767

Reputation: 1

Delete the hidden .git folder (that you can locate within your project folder) and again start the process of creating a git repository using git init command.

Upvotes: -3

pim
pim

Reputation: 12587

This process is simple, and follows the same flow as any git commit.

  1. Make sure your repo is fully up to date. (ex: git pull)
  2. Navigate to your repo folder on your local disk.
  3. Delete the files you don't want anymore.
  4. Then git commit -m "nuke and start again"
  5. Then git push
  6. Profit.

Upvotes: 6

Mohamed Taboubi
Mohamed Taboubi

Reputation: 7021

First, remove all files from your Git repository using: git rm -r *

After that you should commit: using git commit -m "your comment"

After that you push using: git push (that's update the origin repository)

To verify your status using: git status

After that you can copy all your local files in the local Git folder, and you add them to the Git repository using: git add -A

You commit (git commit -m "your comment" and you push (git push)

Upvotes: 50

First Zero
First Zero

Reputation: 22376

Yes, if you do a git rm <filename> and commit & push those changes. The file will disappear from the repository for that changeset and future commits.

The file will still be available for the previous revisions.

Upvotes: 37

Borealid
Borealid

Reputation: 98559

Do a git add -A from the top of the working copy, take a look at git status and/or git diff --cached to review what you're about to do, then git commit the result.

Upvotes: 1

Related Questions