Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to zip and ignore all files specified by .gitignore [duplicate]

I have a few dozen git repositories under one directory, all of them contain not only source code but also build artifact, developer artifact, blobs and other data normally ignored by .gitignore

I would like to quickly transfer these source code to another computer. Is there an easy way to package everything that is not ignored by git?

To add more context:

I have 50-ish git folders or so, each on different branches with some current changes

I would like to copy all files that are tracked by git, at the current state ( current branch plus current local change )

If by cloning them all, it means to go to each folder, commit the current changes locally, remember the branch name, git clone local branch to another folder, do that 50 times then I am strongly agaist it

like image 537
qkhanhpro Avatar asked Dec 14 '25 12:12

qkhanhpro


2 Answers

One-liner:

$ alias gitzip="git archive HEAD -o ${PWD##*/}.zip"
$ cd mydir
$ gitzip
like image 67
Daniel Viglione Avatar answered Dec 16 '25 05:12

Daniel Viglione


If you want to include history, use git bundle (or xeyownt/git-subundle if your repositories have submodules)

See this script as an example:

for repository in ${repositories[@]}
do
  echo "$repository..."
  declare file_name="../$repository-$(git rev-parse --short HEAD).bundle"
  cd $repository
  if [ -f $file_name ]; then
    echo 'Found bundle is current'
  else
    git bundle create $file_name --all
  fi
  cd -
done

Each repository gives one file (without anyprivate or ignored files), that you can easily copy and, once copied, cloned from.

like image 37
VonC Avatar answered Dec 16 '25 05:12

VonC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!