1

I am new to Git, I created a repository and in it I made 2 branches, one master and another test, the master branch contains the following:

File1.php
File2.php

And the test branch contains the following:

File3.php
File4.php

If I do a pull from the test branch, it downloads the branch corresponding files, but I want remove them once I don't need the branch in the local repository. Is there a way to remove the files just choosing the branch to remove and not removing them one by one?

Example of what I want: git branch -d test --remove-files so Git deletes the branch and the files that belong to that branch.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Please provide a bit more context. What do you want to achieve exactly? Do you want to delete the branch or any of the files in the branch? (e.g. `git branch -d test` to delete the branch) – kapsiR Oct 27 '21 at 19:00
  • @kapsiR Hey, thanks for answer, i want to know if i can delete all the files from the branch without delete one by one the files from the branch, example: git branch -d test --remove-files something like that – Ernesto España Oct 27 '21 at 19:02
  • If you are on branch `master` and delete the branch `test`, the files are gone. (internally there are still there, but you don't see it anymore) So if you want to get rid of the whole branch: `git switch master && git branch -d test` – kapsiR Oct 27 '21 at 19:08
  • 2
    Does this answer your question? [How do I delete a Git branch locally and remotely?](https://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-locally-and-remotely) – kapsiR Oct 27 '21 at 19:24
  • Files do not exist in a *branch*. Files exist in *commits*. Git finds the commits using other commits and/or using branch names and/or tag names and/or other names, but Git is really all about the *commits* (which then contain files). – torek Oct 27 '21 at 21:07
  • Can you clarify the 3 following points : 1 - what's your active branch (`master` or `test` ) ? 2 - what git command you ran when you say "if I do a pull from the test branch" ? 3 - what you mean by "once I don't need the branch" (I genuinely do not understand that point : do you want to switch back to master ? that's simply `git checkout master`) ? – LeGEC Oct 28 '21 at 08:17
  • I think you're making a fundamental (and very common to Git beginners) Git mistake here: you are thinking that Git is about *files* and/or *branches*. But it's not: Git is about *commits*. Each commit acts like a permanent archive (like a zip or rar file) of *every file*. You never "remove a file" at all. Instead, you *add commits* to the repository. – torek Nov 01 '21 at 02:18
  • 1
    A commit that doesn't have a file, just doesn't have the file. The file is still in other commits that *do* have the file, just as if you made an archive today with six files, then removed two tomorrow and made another archive, the new archive would have four files. That doesn't stop the old archive from still having six files in it! Since the point of Git is to save every commit for all time, you literally don't ever *remove* anything. You just add new commits, and the new commits have whatever they have, and lack whatever they lack. – torek Nov 01 '21 at 02:19

2 Answers2

0

To remove a file both from the Git repository and the filesystem, you can use git rm without any parameters (except for the file's name, of course)

git rm file1 file2 

If you only want to remove the file from the repository, but keep it on the filesystem, you can add the --cached flag:

git rm file1 file2 --cached
Ran Turner
  • 14,906
  • 5
  • 47
  • 53
  • Hey, thanks for answer, but i want to know if i can avoid that just using the branch in the remove, e.g: 'git rm . branch test' . Greetings – Ernesto España Oct 27 '21 at 19:07
  • @ErnestoEspaña: No, you must clone, check out, modify, and commit. Git works with *commits*, not files, and you must make commits. (GitHub do provide some limited ability to make new commits directly *on* GitHub, but in general that's not a good idea as it's ... limited.) – torek Oct 27 '21 at 21:03
  • Note that there is a very big difference between making a *new commit* in which some files are *gone*, and *deleting a branch name*. Branches in Git are a little weird: a branch *name* merely *finds commits*. Deleting the branch name means "don't let me find the commits this way any more". The commits exist independently of the branch names that find them, and making a new commit where the file is gone still leaves the *old* commit where the file *still exists*. – torek Oct 27 '21 at 21:04
  • @torek hey, sorry for the delay im having some health issues, so are you saying I must delete the files one by one and I can't do with a simple command right? – Ernesto España Nov 01 '21 at 01:49
  • @ErnestoEspaña: You can remove multiple files from the proposed next commit using `git rm` or `git rm --cached`. First you *clone the repository*. Then you *check out the commit you want to build on*. This gives you a *working tree* (folder full of files and sub-folders) with the files from that particular commit. Now you modify the working tree as needed and run `git add` if/as needed to update the *proposed next commit*. Finally, you run `git commit` to turn the *proposed* commit into an *actual* commit. – torek Nov 01 '21 at 02:10
  • Hence, let's say you wanted to remove files A, B, and C from the commit that's currently the latest on branch `develop`, then make a new commit on `develop`. You'd clone the repository (if you haven't already) and then `git checkout develop`. This gets you the right starting commit. Now you can run `git rm A B C`. Those files are now removed from your working tree *and* scheduled to *not be in* the next commit. Now you run `git commit` to add the new commit in which the files are gone. The old commit still has the three files! – torek Nov 01 '21 at 02:12
  • Once you have the new commit made, the name `develop` in your clone refers to the *new* commit. The new commit refers back to what used to be the latest commit. So you can now *add this commit to other repositories*, such as the one you cloned, using `git push origin develop`. This *adds to their repository*. – torek Nov 01 '21 at 02:12
  • @torek wow I have a lot to learn, Mainly my doubt is because I want to use a server as tests, but my main idea was that this server be managed with branches according to the solution, example: I have my server with the 7777 problem, I finish testing that branch and now I want to go to branch 7778, but I don't want to have the files that belong to branch 7777 on that server, thats why i'm asking this – Ernesto España Nov 01 '21 at 02:24
  • Git isn't generally good as a deployment tool or a testing tool. Its function in life is that of any source-code version control system: to retain human-produced code or data that evolves in the way humans evolve it. There are other systems designed for doing testing, such as the various CI systems (GitLab, GitHub, Travis, CircleCI, Jenkins, etc.), that are more suited to running tests on software. – torek Nov 01 '21 at 02:35
0

From my knowledge, you have to select the files manually.

This will remove the file from your local repo.

git rm "file.ext"

IF you pushed to your remote repo and want to remove it from there too then this is the command you need to remove the files from your remote repo.

git rm "file.ext" --cached

IF you have a directory containing all the files you want to delete you can do the following command to remove the directory and all the files contained within it

rm -r "folder-name"

Delete the branch locally.

git branch -d "branch-name"

Delete the branch on the remote side.

git push origin --delete "branch-name"
David Buck
  • 3,752
  • 35
  • 31
  • 35