Setting a sh file to git bash - windows

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?

Related

Is there a way to add multi steps to a git alias?

I'm going to create an alias that does this:
I've added it to my git-bash .bash_profile but I'd like to see if there is a way to add it as an alias so I don't have to use git bash
Git finish will push to current branch # Eg. gf "commit message"
gf() {
CURRENT_BRANCH="$(git rev-parse --abbrev-ref HEAD)"
git add . && git commit -m "$1" && git push origin "$CURRENT_BRANCH"
} # Git merge # Eg. gm branch-name
gm() {
git merge "$1"
} # Git checkout # Eg. gc branch-name
gc(){
git checkout "$1" && gp
}
You could create a "Git-Subcommand" the name of this executable must be in this Format: git-{{ Name }} eg: git-acommit. This File must be located in one the Directories listed in $PATH, you can now execute git acommit and git will search for it in you $PATH.
To use this than in an alias simply run git config --global alias.gf acommit and you are finished. Please note: the alias step is unnecessary since you can name your file how you want, so you could also name it git-gf instead of git-acommit.
But without creating a separated file it's impossible to stack commands in a git alias.
Or you could use GitAlias to create an alias that executes a function or multiple Commands.
For an example check out the Recovery examples Section.

mac homebrew git command not found

When I run the following on a mac from the command line it works fine:
git submodule foreach "git checkout develop; git pull"
... but when I run it from within a .sh script it gives a git: command not found error:
/usr/local/Cellar/git/2.14.2/libexec/git-core/git-submodule: line 355: “git: command not found
The full script is as follows. The checkout and pull work, but submodule foreach throws the error:
#!/bin/bash
set -e
set -u
git checkout develop && git pull
git submodule foreach “git checkout develop; git pull”
exit 0
Advice I've found says to make sure usr/bin is in my path. But here's what I see when I do echo $PATH (seems to already be there):
/Users/myusername/.nvm/versions/node/v8.0.0/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Applications/VMware Fusion.app/Contents/Public
Your quotes are off.
It should be "git checkout develop; git pull" instead of “git checkout develop; git pull”.

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.

git commit and push via batch file on 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

Git should I create repo file in project folder?

I'm little bit confused when I setup Git. I have local Repository at my C:\Repository and root my project is located at C:\Projects. I want to ask should I create repo file into work project folder or not to be applied to commit and push to local repo?
If I get it right, you have a project in C:/Projects and a repository in c:/Repository and you want to push changes from the project repository into the c:/Repository.
Here how you can setup all this (I assume, you use Git for the Windows and not Cygwin git):
$ mkdir -p /c/Repository/MyProject.git
$ cd /c/Repository/MyProject.git
$ git init --bar # Create "server" (bar) repository
$ mkdir -p /c/Projects/MyProject
$ git init # Create project repository
$ git config user.name "My Name"
$ git config user.email "My#e-mail"
$ # set more properties...
$ # create some files
$ # ...
$ git add -A . # Add new files to the index
$ git commit -m "Initial commit"
$ git remote add origin /c/Projects/MyProject
$ git push origin master:master

Resources