git archive with unstaged changes
Asked Answered
P

7

10

I'm building my own rpm's. Usually I use git archive to get a tarball from the commit or tag I'm interested in (suppose I have put a tag 1.0):

git archive --format=tgz --prefix=product-1.0 1.0 > product-1.0.tgz

suppose now I am doing some local developments that I haven't committed yet and I want to get an archive; is there any way to obtain this without having to commit?

edit I could just use something like this:

tar cf product-1.0.tgz --exclude=.git

but that will include all binaries and other untracked files which I prefer not...

Processional answered 16/4, 2014 at 17:3 Comment(2)
You could probably put the work you're doing in a local branch that isn't pushed to the repo / doesn't track a branch on the repo.Bhakti
If using npm is fine then check out gitzip This will let you zip files of folder based on .gitignore simply run gitzip in your folder.Amathiste
A
11

I'm not 100% sure this is exactly what you want, but you can stash the changes you've made with git stash create, which will give you a hash for the current state of the repo - and then create an archive for it with git archive <options> $HASH.

Androecium answered 16/4, 2014 at 17:9 Comment(3)
then my archive will not include my unstaged changes, but I want them!Processional
It should give you the repository including unstaged changes, but not uncommitted files. If you need those too, you'll have to find another way, alasAndroecium
A small FYI: a new hash is created every time the command is run.Hippocrene
T
11
git ls-files | tar Tzcf - archive.tgz
Teepee answered 16/4, 2014 at 17:50 Comment(0)
D
5

When you have unstaged changes you can try git diff and tar or zip commands.

tar command:

git diff --name-only -a | tar Tzcf - myUnstagedChanges.tgz

zip command:

git diff --name-only -a | xargs zip myUnstagedChanges.zip

Furthermore if you need to do this about a commit, try git show [idCommit] command.

git show --pretty="" 9471ae --name-only -a | tar Tzcf - myCommitedChanges.tgz

or

git show --pretty="" 9471ae --name-only -a | xargs zip myCommitedChanges.zip

The compress archive will be created into git root directory.

Hope I've helped.

Durham answered 18/4, 2019 at 17:0 Comment(1)
Thanks for trying but this is no answer to the question: The question is how to compress the whole repository with the unstaged changes included; not only the unstaged changes.Processional
A
1

Sorry, that will not be actual answer.

There may be some way, but actually I don't see much point to it. Committing is rather light. There's nothign wrong in committing a completely temporary or broken files. Until you push it to public, it's all your local storage. Just create a new branch, commit to it, then at some point of time you can delete that branch, even immediatelly. The only thing you must be careful is to not push that branch to public repo. The biggest plus in it is that, during commit, you will be able to selectively pick what files you want to include. It will allow you to filter out any unwanted binaries, archives, etc - and at very fine details. It's really much more usable than trying to filter them by bash and similar.

So, for me, if you are building packages, then you really want to commit. Just because at some point of time in future you will need to check what were the original files. Or revert to it. That's exactly what branches and whole repository is: marking and remembering some files in some state. Repository is not just for keeping "release code". It is for keeping everything relevant that you needed at any point of time, in the same exact state as it was back then.

Therefore, I'd even recommend true branching for those builds/packagings. Not stash. Stash is something temporary, meant to evaporate quickly. You get a hash for that, but you can very easily delete that whole branch/stash/revision. That would exactly annihilate the essence of versioning. You will put it into git, just to delete it.

With use of normal branches and normal commits, at any future time you will be able to review or recreate any RPM you have built at any time in the history. IMHO, that's huge plus.

Asseveration answered 16/4, 2014 at 17:3 Comment(1)
I use SourceTree which gives a very nice view of all the differences you have in unstaged changes. I often refer to this view to see the exact differences I've made since the last commit, if I commit I can no longer see these changes as soon as they are made. I can compare two commits, but this wont show unstaged changes combined with the difference between two commits. So the only way to get live updates on exactly what I've changed seems to be to leave the changes unstaged. That is why I personally am looking for a way to backup unstaged changes with git bash.Portie
I
1

Create a branch called archive, add, and commit the files I want to track in addition to what is already tracked in git. Whether its your currently untracked development files, generated documentation, binaries you're not tracking in Git but want archive, etc.

With those committed locally, you can now use

git archive archive --format=tgz --prefix=product-1.0 1.0 > product-1.0.tgz

Note, the first archive is the command, the second is the branch name.

Now revert that last commit so those files become untracked again

git reset --soft HEAD~; git reset

Switch back to master and delete the archive branch.

Imco answered 4/4, 2017 at 18:42 Comment(1)
thanks for the answer. That will work but requires WAY more work than the two top answers...Processional
S
0

The following command with help

 git archive --output=modified-files.zip HEAD $(git ls-files --modified)
Surveying answered 5/4, 2017 at 8:9 Comment(2)
I'm sorry that does not answer my question. I am interested in ALL the files; not only the modified ones.Processional
This command zips all modified files including untracked and ignored (excluding committed files) git status --porcelain --untracked-files=all --ignored | cut -c4- | sort -u | zip modified.zipNinette
T
0

For unstaged changes:

tar czvf product-1.0.tar.gz `git diff --name-only`

For staged changes:

tar czvf product-1.0.tar.gz `git diff --cached --name-only`

But these only work when there are no staged part and unstaged part in the same file.

Take answered 25/5, 2021 at 12:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.