Akash's Blog

_

Thursday, January 23, 2025

Auto push to Git on Mac

Introduction

For periodically syncing changes of your Github repository you can automate the process using a script and crontab on Mac OS. It's really quick and simple if you don't have more complex requirements. Note taking is one of the use cases where you want periodic sync to take place automatically to ensure notes are pushed to the cloud without any manual steps.

Setup

1. Open terminal and type crontab -e this will open an editor where you can specify which script to execute, here we want to push our changes so we will point it to that script. Here we have used cron expression "* */2 * * *" which will execute the script every two hours. You can use crontab.guru to generate the expression as per your requirement.

* */2 * * * /path/to/sync.sh >> /path/to/logs/run.logs 2>&1

Save the changes. (Type :wq! to save) 

File sync.sh should be accessible and hold required permissions. Use chmod command to update the permission to necessary state.















2. Define sync.sh in following way to push your changes. Prerequisite is that you have cloned and setup required github repository in respective folder.

#!/bin/bash

set -e

cd /path/to/repo

BRANCH="main"
COMMIT_MSG=${1:-"Update"}

 if [[ -n $(git status --porcelain) ]]; then
    echo "Changes detected."
    git add -A
    git commit -m "$COMMIT_MSG"
    git push origin "$BRANCH"
    echo "Changes pushed successfully!"
else
    echo "No changes to commit."
fi

Important Note
Note that this script syncs all the changes of given repository. Do not add sensitive information like credentials, tokens or anything which you do not want to get synced especially on public repository if that is the case.

Monday, January 20, 2025

Clone all GitHub Repositories of the User

Cloning all the repositories of the user is not something that you would need very frequently but for personal repositories it comes very handy as I don't need to clone individual repositories. Similarly while working with any organization this saves lot of time of individual cloning. Having said that you may need required access and permission to the account and organization as we will going to use the user token to clone the repositories.

Generate Access Token

The first step is to create a personal token which can allow us to access our repositories through API.  We can generate the token from Settings > Developer Settings > Personal Access Token. Here we will going to use classic token for better understanding and simplicity.















Run script to Clone

Following script can be used to clone all the repositories. It uses jq command to process the response of the github API which you may need to install if not done already. Basic idea is to call the GitHub API, fetch list of repositories, iterate over the repositories and clone each in given directory.  Update GitHub username and token in following script and execute it.

#!/bin/bash
GITHUB_USERNAME=""
GITHUB_TOKEN=""
# GITHUB_ORG=""

API_URL="https://api.github.com/user/repos"
# For organization use following
# API_URL="https://api.github.com/orgs/$GITHUB_ORG/repos" 
PAGE=1
PER_PAGE=100

mkdir -p allrepos
cd allrepos || exit

while :; do
  REPOS_TO_CLONE=$(curl -s -u "$GITHUB_USERNAME:$GITHUB_TOKEN" "$API_URL?per_page=$PER_PAGE&page=$PAGE" | jq -r '.[].clone_url')
  
  if [ -z "$REPOS_TO_CLONE" ]; then
break fi echo "$REPOS_TO_CLONE" | while read -r repo; do
git clone "$repo" done PAGE=$((PAGE + 1)) done echo "All repositories are cloned successfully!"

This script may take time depending on the number of repositories given user or oganization have. You will see success message once all repositories are cloned.

Monday, January 13, 2025

How Stackoverflow helped me?

Introduction

Stackoverflow has been part of every programmers day to day work. From finding solution to the errors, looking for best approach of the solution or just helping other fellow programmers on the internet, stackoverflow is an excellent website. 

In the era of two poles of internet search nowadays, ChatGPT and Google Search, it may seem that stackoverflow is loosing it's charm. I have been a user for a decade and I really enjoy reading questions and answers, not necessarily I am actively contributing the way I used to do earlier but still I find is truly valueable for learning and understanding the concepts. 

Having said that, the stackoverflow community has been a great company to be with so that you can ensure continuous learning.

How did it helped me?

Stackoverflow is the reason I managed to grab my first job, I knew nothing and I know nothing. When I signed up, I did all the mistakes that any new member would do, try to answer questions without clear explanations, impulsive voting and flagging, focusing on gaining reputation rather than helping the people etc.

However, I stick to it for longer and slowly kept on improing the way I answer the questions and primarily focused on helping instead of just answering the questions for the sake of gaining points. In a long term it helped me in following ways and most probably to anyone who actively contributed for a year and so,

  • I started paying much more attention to the problem rather than solution.
  • I understood the value of community and open source.
  • I learned to explain things better.
  • I gained confidence to build things on my own. etc.
Initially you may feel bullish when you focus on reputation and badges but gradually you realise that the quality answers and genuine help can take you far instead of reputations and badges. It doesn't mean reputations and badges are useless but it simply means that these should not be your driving force for contributing on Stackoverflow.

Where am I currently?

I no longer actively contribute on Stackoverflow now but I do regularly read interesting questions, drop in comments, help community in any possible way I can do and even answer question if I feel to. 

Following is my Stackoverflow flair,


profile for Akash Thakare at Stack Overflow, Q&A for professional and enthusiast programmers

Is it still worth?

For anyone who is in to programming should contribute to it. You can learn from the experts directly, get into discussion with them through chat, comments, questions and even answers. It surely enhances your knowledge. You don't need to invest whole day but couple of hours of contribution in a week can make a huge difference to your overall programming skill which I can surely guarantee. You just need to remain focused into area of your interest for longer rather than reading anything and everything.

Summary

Stackoverflow is an exceptional place to be if you are in programming and software developement in general to stay connected and updated about the tools and technologies which are making difference to the world. Do not rush to gain more reputations and badges but stay aligned on the path of help and support. ♥️

↑ Back to Top