简体   繁体   中英

How to zip and ignore all files specified by .gitignore

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

You can try this :

for dir in repo1 repo2 repo3; do
    git -C "$dir" ls-files | perl -pe "s[^][$dir/]"
done | zip /tmp/test.zip -@

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.

One-liner:

$ alias gitzip="git archive HEAD -o ${PWD##*/}.zip"
$ cd mydir
$ gitzip

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM