Git - Tips & Tricks

Git - Tips & Tricks

Start a new git repository

To create a new repository on the command line:

git init
git add .
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/OWNER/REPOSITORY
git push -u origin main

Changing a remote repository's URL

The command changes an existing remote repository URL:

git remote set-url origin https://github.com/OWNER/REPOSITORY

Cleanup unnecessary files and optimize the local repository

The following commands can be used to reduce the size of a git repository:

git reflog expire --all --expire=now
git gc --prune=now --aggressive

Clone all repos at once from GitHub

On Windows and all UNIX/LINUX systems, using Git Bash with this script:

CNTX={users|orgs}; PAGE=1
curl "https://api.github.com/$CNTX/OWNER/repos?page=$PAGE&per_page=100" |
  grep -e 'clone_url*' |
  cut -d \" -f 4 |
  xargs -L1 git clone
💡

Set CNTX=users, to download all your repositories or set CNTX=orgs, to download all repositories of your organization.

💡

The maximum page-size is 100, so you have to call this several times with the right page number to get all your repositories (set PAGE to the desired page number you want to download).

Resources

  1. https://docs.github.com/en (opens in a new tab)
  2. https://education.github.com/git-cheat-sheet-education.pdf (opens in a new tab)
  3. https://stackoverflow.com/questions/19576742/how-to-clone-all-repos-at-once-from-github (opens in a new tab)