git commit and push via batch file on Windows - windows

I do same task often of committing and pushing changes to remote branch. Being lazy sometimes, I needed to put set of git commands to automatically perform these steps:
cd D:\wamp\www\projectName
git checkout dev
git add .
git commit -am "made changes"
git push
pause
I also tried:
cd D:\wamp\www\projectName
call git checkout dev
call git add .
call git commit -am "made changes"
call git push
pause
and
cd D:\wamp\www\projectName
git.exe checkout dev
git.exe add .
git.exe commit -am "made changes"
git.exe push
pause
Everything works excpet for the final push command. Here is output:
D:\wamp\www\givingcircle>git checkout dev
Already on 'dev'
Your branch is ahead of 'origin/dev' by 1 commit.
D:\wamp\www\givingcircle>git add .
D:\wamp\www\givingcircle>git commit -am "made changes"
# On branch dev
# Your branch is ahead of 'origin/dev' by 1 commit.
#
nothing to commit, working directory clean
D:\wamp\www\givingcircle>git push
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
D:\wamp\www\givingcircle>pause
Press any key to continue . . .
As you can see, for push, I am getting:
D:\wamp\www\givingcircle>git push
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
When I run above commands via git shell itself, everything works fine. I have also added git to Windows Path env variables.
Does anyone have an idea of why it works on git shell and not on batch command ? (even though other commands work but not push)

For me, by default, Windows executes .sh files correctly using Git Bash. So I would write your script as a regular bash shell script:
#!/bin/sh
cd /d/wamp/www/projectName
git checkout dev
git add .
git commit -am "made changes"
git push
echo Press Enter...
read

I had a similar need, to be able to move code from BBCloud to our development test servers, for stage 1 testing.
To do this, I created a Windows scheduled task:
Under "Actions", I added "C:\Program Files\Git\bin\bash.exe" in Program/script field (the quotes were required).
In the "Add arguments" field, I entered c:\path\to\bash script\pull.sh.
I then completed the Task Scheduler wizard (run frequency, time, etc.).
I then created a bash script, using Nano in Git Bash for Windows containing:
#!/bin/bash
cd /c/path/to/bash script
git pull
I would prefer a push to the repository automatically pushing down to the test server, but Pipes, Webhooks, and DeployHQ don't seem to be a solution for our environment.

Try this one !!
cd c://TESTS/path
set HOME=%USERPROFILE%
GIT COMMAND GOES HERE
pause

Related

Why all the script in my git hooks (pre-commit, post-commit, pre-receive, pre-push etc) do not run?

Why all the script in my git hooks (pre-commit, post-commit, pre-receive, pre-push etc) do not run?
Note:
this question is not a duplicate;
I have try the answer to each of the other questions but none of them work.
I did chmod +x, added the path to hook. rename script, neither of them solve my issue.
Inside my git
branches config description HEAD hooks info objects refs
Inside hooks:
applypatch-msg.sample fsmonitor-watchman.sample post-update.sample pre-commit prepare-commit-msg.sample pre-rebase.sample update.sample
commit-msg post-merge.sh pre-applypatch.sample pre-commit.sample pre-push pre-receive
I run them manually and they are all working fine.:
$ bash pre-commit
You are about to commit to master
Do you really want to do this? [y/n] y
pre-commit script
#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".
echo "You are about to commit" $(git diff --cached --name-only --diff-filter=ACM)
echo "to" $(git branch --show-current)
while : ; do
read -p "Do you really want to do this? [y/n] " RESPONSE < /dev/tty
case "${RESPONSE}" in
[Yy]* ) exit 0; break;;
[Nn]* ) exit 1;;
esac
done
But when i git commit and git push to the repository none of the scripts work.
$git commit -m "Test hooks"
[master] Test hooks 1 file
changed, 1 insertion(+)
My git version is 2.39.1
I created the repository on a VM with Ubuntu 18.04.6 LTS installed
Here was the procedure fro creating the repo.
mkdir project1.git
cd project1.git
git init --bare
After the creation i clone the repo to my local computer (windows).
Clone the git repository
git clone git#{ip}:/home/git/git_repositories/project1.git/
But I want use the scripts in project1.git/hooks to make this work.
A pre-commit hook for instance would not work in a bare repository (which has no working tree).
It would work only in your cloned repository, on Windows, where you can create a myClonedRepo/.git/hook/pre-commit script (no extension, no .sh), which will be run before each commit.
From the comments:
all users could create/clone their repositories from a shared Git template repository. The article "Creating a Custom Git Template" gives an illustration of that approach, but means that:
every user must access the same shared folder
they need to activate the init.templateDir config setting in their global Git configuration
any check which must be enforced for all the team members, especially for a distributed team, is best managed by server-side hooks instead.

Backup using GIT - add, commit and push everything including other GIT repositories

I want to build a backup system. I have a server (an old pc) and I boot it up via magic packets. For this purpose, I've written a batch Script doing this. On the server is a git server running with a couple of repositories. I can easily push to it.
But here's the problem:
My project folder contains itself a couple of git repositories. If I now add all files with git add . an Error is thrown, that I should add the submodules. This is the post talking about it Automatically Add All Submodules to a Repo. I've modified the shell script, to work with spaces, and to automatically commit and push everything while executing.
cd "D:\Projekts"
find . -type d | while read x ; do
if [ -d "${x}/.git" ] ; then
cd "${x}"
origin="$(git config --get remote.origin.url)"
cd - 1>/dev/null
echo ""
echo git submodule add "${origin}" "${x}"
git submodule add "${origin}" "${x}"
fi
done
echo "done adding all submodules"
git add .
git commit -am "new backup"
git push
echo "done pushing"
And this doesn't throw an error so far. But still the folders containing all those repositories are empty, if I clone the repository.
sidenote: I add the remote to the backup repository like this: $ git remote add --mirror=fetch origin git#<ip>:backup.git
Thanks in advance for your time,
Hellow2 :)

Setting a sh file to git bash

I've created a Python "automation" task that will initialize a repository on github and then on my bash function I'll be creating a folder, initialize the repository as the code bellow shows:
#!/usr/bin/env sh
function repo() {
cd
python C:/Users/wsm/PycharmProjects/GitAutomation/create.py $1
cd C:/Users/wsm/$1
start .
git init
git remote add origin <mygithublink>/$1.git
touch README.md
git add .
git commit -m "Initial commit"
git push -u origin master
code .
}
The only problem is that I have to use source command everytime I close a git bash to enable the command repo (Name of the function) again. Any ideas on how to make that command permanent on Windows 10?

Multiple git commands in single command executed in order they are encountered by compiler

I have following list of commands that I run in respective order so that a source project can be committed and pushed to the repository on Bitbucket:
git init
git remote add origin https://[BitBucket Username]#bitbucket.org/[BitBucket Username]/[BitBucket Repository Name].git
git config user.name "[BitBucket Username]"
git config user.email "[BitBucket Email ID]"
## if email doesn't work then use below ##
git config --global user.email \<\>
git add *
git commit -m "[Comment]"
git push -u origin master
Now instead of putting each and every line at their respective time and order, I want to know, if there is a possibility that I can chain all these into single git command and maintain the same order, something like below ?
git init remote add origin https://[BitBucket Username]#bitbucket.org/[BitBucket Username]/[BitBucket Repository Name].git config user.name "[Username]" ....
Or atleast combine multiple same category params like below ?
git config user.name "[BitBucket Username]" user.email "[BitBucket Email ID]"
I need to know possibility of both scenarios with examples.
We can use list off command in single command for example:
git add . && git commit -m "updated pom file" && git push
or:
git stash pop && git add . && git commit -m "updated pom file" && git push
&& -> if 1st command successfully executes then execute next command else it won't execute next command.
& - it executes all the command
|| - execute next command if 1st one failed
If you are in a Windows Powershell:
git add . ; git commit -m "Testing one line command" ; git push
I have gitbash on Windows system and I am not as good with Win batch as with Linux shell.
You still can write a bash script (interpreted by the msys2 bash embedded with Git for Windows).
As mentioned in the comments by Lasse V. Karlsen, and as I mentioned before in "Which shell used for git '!' aliases?", you can write a shell script in a file (in your %PATH%) named git-xxx, and call it with git xxx.
That script would begin with:
#!/bin/bash
I created a file called reset.txt and in that file I have the commands
git reset --hard
git clean -d -f
[this is a newline - very important to have it]
I just copy and paste this into my terminal and it executes the commands in order.

.bashrc strange warning "lias command not found"

I have a following .bashrc file:
function lazygit() {
git add --all :/
git commit -a -m "$1"
git push
}
function bak() {
git pull
git add --all :/
git commit -a -m "backup: $1"
git push
}
But whenever I call lazygit or bak command, I get:
/home/bok/.bashrc: line 1: lias: command not found
Already up-to-date.
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
/home/bok/.bashrc: line 1: lias: command not found
Everything up-to-date
Why do I get "lias: command not found"?
Open /home/bok/.bashrc and goto line #1. You will see lias and change to alias (the a was probably accidentally deleted).
Close your terminal completely and re-open it. It should work fine now.

Resources