PROJECTS NOTES HOME

Fetch and pull changes automatically

This is mainly important when you are working ont he same repo on two different machines. You make changes on one, then login to another one - you must always rememebr to pull those changes, otherwise if you make your own - you will get into merge conflicst. And as of [2023-12-14 Thu] I am not so good at solving those. So I try to avoid them as much as possible.

This is the reason I am creating this script that will automatically run git fetch && git pull that command for me whenever I turn on the WSL.

The script example:

#!/bin/bash
REPO_DIR="$HOME/GIT/notes/"

function auto-fetch-pull() {
    if [ -d "$1" ]; then
        cd "$1" || exit
        echo "Fetching latest changes..."
        git fetch
        echo "Pulling changes into your branch..."
        git pull
    else
        echo "Directory '$1' not found."
    fi
}

auto-fetch-pull "$REPO_DIR"

Ensure the script has execute permissions:

chmod +x auto_fetch_pull.sh

Then, try running the script:

./auto_fetch_pull.sh

Add the script execution to your .bashrc

echo '~/.emacs.d/MISC/auto_fetch_pull.sh' >> ~/.bashrc

Each time WSL (or any other linux distribution launches) launches, one of the thing it does that concern us in this case, is it runs source .bashrc. Basically evaluates and applies everything that it sees in .bashrc file. And since we have conveniently placed our script into .bashrc with the last command - it will run the script each time WSL is launched.