Skip to Content
Git – Tips & Tricks

Git – Tips & Tricks

🆕 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

Last updated on