Markus
Markus

Reputation: 165

Git: how do I delete remotely available files and keep only locally changed files

Some time ago, I cloned a remote repository of someone else onto my local machine.

Over the term of the project I changed some files locally and partly had them in my local git version control.

Now, since the project has ended and the files take a lot of space on my machine, I want to delete all those files, which can be drawn from the remote repository once I need them again.

I want to keep, however, all the files (and versions) I changed locally.

Is there an (elegant) way to do this with git without finding all the finds manually by going through my entire git history? So far, I couldn't find a post, which could give me an answer on this question.

Upvotes: 2

Views: 56

Answers (1)

ElpieKay
ElpieKay

Reputation: 30878

Find all the commits whose author name is you, in the reverse order:

git log --pretty=%H --author=Markus --reverse

List the modified/added/renamed files of a commit $commit, excluding deleted files:

git log -1 ${commit} --diff-filter=d --name-only --pretty=

Archive these files of $commit:

git archive -o ${commit}.zip ${commit} ${files}

Combine them:

n=0
for commit in $(git log --pretty=%H --author=Markus --reverse);do
   n=$((n+1))
   git archive -o ${n}-${commit}.zip  ${commit} $(git log -1 ${commit} --diff-filter=d --name-only --pretty=)
done

The output is 1-xxx.zip, 2-xxx.zip, etc. 1-xxx.zip is the oldest commit and its changed files.

This method takes almost the smallest space.

Update:

Unfortunately, this method keeps only the archived files. The commits and related git metadata are gone if you remove the local repository without backing them up. To reuse the metadata at the lowest cost of space, I recommend git bundle. For example, you can bundle the branches foo and bar:

git bundle create my.bundle foo bar

If you clone the repository again and want to reuse the commits you have made, you can run in the repository:

git fetch <path_to_my.bundle> foo:backup_foo
git fetch <path_to_my.bundle> bar:backup_bar

The commits are now available from the branches backup_foo and backup_bar in the newly-cloned repository. You can then manipulate the branches and commits. my.bundle works as a read-only remote repository. You can also create a remote for path_to_my.bundle, for example with git remote add backup <path_to_my.bundle> so that you can run git fetch backup foo:backup_foo.

With my.bundle, you can remove the previously-cloned repository to save space.

I tried to bundle a shallow branch, and then a range of commits, but both bundles threw errors when I tried to clone or fetch them. So I thinks this way it costs the lowest space. But if you can push them to a remote repository, like one on Github or Gitlab, or upload the repository or the bundle to a service like Dropbox, you can even save the local space.

Upvotes: 3

Related Questions