Got Git?

Git is a great way to keep track of programming changes. However, some of the commands can get overwhelming and just need a simple way to get changes synced with a remote repository.

For repetition of tasks you can create a file you always run or add to your profile to name a few. Working in both Windows and Linux, I wrote a simple solution that works for me. For this solution I wanted a way to check the repository I was going to push too and see the changed files without adding git just yet. Then if all looks right, then we enter our commit message, and push.

Overview of whats needed

  1. Step 1: Check remote repository
    • git remote -v
  2. Step 2: Show changes. -n is a dry run
    • I usually add all files and not specific changes, also depends on the project. For a new post, adding all files is best.
    • git add . -n
  3. Step 3: Prompt for message to use for the commit
    • git commit -m "Message here!"
  4. Step 4: Add all files and push to the branch
    • git add .
    • git push origin main

Windows solution

For the Windows solution, this is a batch file that does the trick. With PowerShell we could add this to the profile as a function as well.

@echo off
cls

REM show the remote location branch
echo Does this look correct?
echo.
git remote -v

echo.
REM show the changed fils
echo Here are the changed files
git add . -n

echo.
REM set a commit message if all is good
set /p message="Enter commit Message: "
echo Here is your message: %message%

REM add files and commit and push
git add .
git commit -m "%message%"
git push origin master
echo Update complete!

Linux solution

I have this added to my bash_aliases as a function and accomplishes the same thing.

function gitpush(){
git remote -v
read -p "Do you want to proceed? (yes/no) " -t 10 yn

if [ -z "$yn" ]
then
echo -e "\nerror: no response detected"
exit 1
fi

case $yn in
y ) echo ok, we will proceed;;
n ) echo exiting...;
exit;;
* ) echo invalid response;
exit 1;;
esac

echo Here are the changed files
git add . -n

read -p "Enter the commit message: " message
git add .
git commit -S -m "$message"
git push origin $(git branch --show-current)
read -p "Push Completed!"
}

This is just a simple solution that works for me and small changes might occur over time.

Update: I was using my function across some various repositories and found a small issue. This happened when the name of the main branch could be main or master. To work around this for Bash we can run a small bash command to get the branch in place. By running git push origin $(git branch --show-current) the $(git branch --show-current) will run and pick up the branch we are on.

Check out the snippets as well:


I’m publishing this as part of 100 Days To Offload. You can join in yourself by visiting 100DaysToOffload.com.

Tags: git, shell, powershell, 100DaysToOffload

Found an issue? Edit on Github

← Back home