git from bash script on Windows using MinGW is not configured - windows

I am writing a bash script that needs to perform some git actions. I'm working on windows using MinGW.
running bash test.sh
with test.sh having the content:
echo "git config --global user.name -> $(git config --global user.name)"
This prints:
git config --global user.name ->
Whereas going on powershell and typing git config --global user.name prints my_actual_user_name.
I'd like in this case to user my user settings on git, which would include having access to my ssh keys, which it is not doing. How can I achieve this?

Try to use, both in your script and PowerShell:
git config --show-scope --show-origin user.name
You will see exactly which file is used to query that setting.
If they differ, that would explain your difference in output.
The OP Mefitico confirms in the comments HOME was not set.
Setting a user environment variable HOME to %USERPROFILE% is enough for a git bash session to access the same global .gitconfig setting file as a Git PowerShell session.

Related

Git alias to open the git installation folder

I want to create a git alias git dir, which when used should open the git installation folder via Windows Explorer, how to implement such an alias?
If using Git Bash, try:
git config --global alias.dir '!start "" "$(git --exec-path)"'
Reference: Can I use the "start" command with spaces in the path?
Starting an alias with ! treats it as a command.
I don't have a windows machine at hand, but for Linux:
git config --global alias.open '!git --exec-path | xargs xdg-open'
Works as described.
So the command you're looking for will probably look something like:
git config --global alias.open "!git --exec-path | 'sed s~/~\\~g' | xargs explorer"

How to alias "git push" into itself + running a script?

I need to run a script which notifies my CI server after I push. Therefore I need to alias "git push" into "git push; powershell script.ps1". Here is what I am trying:
$ alias git push='git push; powershell script.ps1'
bash: alias: git: not found
Alternatively, denoting whitespace characters returns this:
$ alias git\ push='git push; powershell script.ps1'
bash: alias: `git push': invalid alias name
$ alias "git push"='git push; powershell script.ps1'
bash: alias: `git push': invalid alias name
How can I alias my script to run every time I push?
This worked:
git config --global alias.push "push; powershell ./script.ps1"
The command runs, but using "git push" still doesn't run my script.
Yes, I know that I can use webhooks for this, but it is imperative that the CI server remains fully local. Yes, I know that I can use NGROK for that, but my company does not allow it.
Git doesn't allow alias names to override proper command names. Use another label for your alias.
And if the alias content mixes a git command and shell commands, you'll have to prepend a ! to tell git to pass the command for the shell to interpret.
Try
git config --global alias.p '!git push; powershell ./script.ps1'
# then to invoke it, just
git p
Another slight improvement would be to use && instead of ; to chain your commands, in case the push fails. It would then stop from executing the unnecessary following.
git config --global alias.p '!git push && powershell ./script.ps1'

Git doesn't create .gitconfig when it's inside a function

I'm a fairly new user in Bash and Git in general and I'm scratching my head about what the problem could be. I'm creating a code that checks if .gitconfig exists and if it doesn't it allows you to configure it almost automatically using a read command to get your email and username and apply them in a line of code.
Code example:
#!/bin/bash
# colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NO_COLOR='\033[0m'
# function git
git () {
printf "${RED}Set your Git email\n${NO_COLOR}"
read GIT_AUTHOR_EMAIL
git config --global user.email "$GIT_AUTHOR_EMAIL"
printf "${RED}Set your Git username\n${NO_COLOR}"
read GIT_AUTHOR_USERNAME
git config --global user.name "$GIT_AUTHOR_USERNAME"
git config --list | grep user.email && git config --list | grep user.name
}
# git check & configuration
if [ -f ".gitconfig" ]; then
printf "${YELLOW}Git was previously configured\n${NO_COLOR}"
exit
else
git
printf "${YELLOW}Done\n${NO_COLOR}"
exit
fi
If it doesn't exist, it calls a function to configure it but after some quick debugging using the set -x command I figured out the file .gitconfig is not created at all but it does when I do it myself outside a function. All it does is go back to read GIT_AUTHOR_EMAIL, apply the code git config --global user.email "$GIT_AUTHOR_EMAIL" and go back to the first read command. I want the code to check for .gitconfig and if it exists it'll also ask if the user wants to re-configure their Git details. I'm super close on doing so.
Is there any way I can fix it or do it another way?
Once you've defined the function git, any invocation of the name git in that shell process will refer to that shell function. If you'd like to invoke the program git, then you need to prefix it with the built-in command:
command git config --global user.name "$GIT_AUTHOR_USERNAME"
If it wasn't your intention to override the git command, then you probably want to name your shell function differently, which avoids the problem altogether.

Pre commit hook solution

I'm looking for a solution where before I push, it determines what host it's pushing to and then changes to the appropriate git user. For example if I'm pushing to a bitbucket then I should use
example#bitbucket.com
if pushing to git then I should use
example#github.com.
Links to similar solutions or just high level of the approach is fine.
currently I have a switch_user bash script that I use to toggle between usernames, but I could forget this.
Following github's help page, you can set the email that you commit as in the git repository with git config user.email "email#example.com". The lack of the --global flag makes it so that it only applies to this particular repository.
If you want to do this automatically for new projects you add, I recommend adding a git alias script to figure it out for you. Something like git smart-clone and git smart-remote-add.
So I found out I could actually do a global hook
On your global gitconfig which can be found here
$ vi ~/.gitconfig
Edit the core.hookspath
hookspath=/path/to/git-hooks
Then I did something like this
#!/bin/sh
File=$PWD/.git/config
if grep -q github.com "$File"; then
git config --global user.name "Foo Bar"
git config --global user.email example#github.com
else
git config --global user.name "Bar Foo"
git config --global user.email example#bitbucket.com
fi
git commit nom_rep
git push nom_rep or fichier // pour ajouter les fichier
git status

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.

Resources