10 Jan, 2023
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.
git remote -v
-n
is a dry run
git add . -n
git commit -m "Message here!"
git add .
git push origin main
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!
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:
If there are replies, they will show below.