Fancy Notes

Git Tips

Start a new git repository

Initialize and push a new Git repository:

git init                        # Initialize a new repo
git add .                       # Stage all files
git commit -m "first commit"    # Initial commit
git branch -M main              # Rename branch to main
git remote add origin https://github.com/OWNER/REPOSITORY
git push -u origin main         # Push to remote main

Change the remote repository URL

Update an existing remote URL:

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

Cleanup & optimize a git repository

Free up space and optimize Git internals:

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

Clone all GitHub repositories (user or org)

Works on Windows, macOS, or Linux using Git Bash:

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 your personal repositories, or CNTX=orgs for your organization’s repositories.

GitHub API limits results to 100 per page. Increase the PAGE variable to fetch all repositories across multiple pages.

Delete a branch in git

Delete a local branch

git branch -d branch_name      # Safe delete
git branch -D branch_name      # Force delete (use with caution)

Delete a remote branch

git push origin --delete branch_name

This command tells Git to delete the branch from the remote repository (like GitHub or GitLab).

Resources

On this page