git update submodule via windows batch file - windows

I wrote a small batch file which updates the git submodules. My only problem is I want to review the changes after the git command but the command window closes - even if I add the "pause" command to it. See:
#echo off
git submodule foreach git pull origin master
pause
When I replace the whole git command with a simple cd .. it'll work as expected (waits for the user to hit enter) so my guess is that the git command does something I don't understand right now.
Thanks in advance!
krnl

if the git command is a Batch file (git.bat) you must run it via a CALL command:
call git submodule foreach ....
Otherwise the original Batch file ends when the git.bat Batch file ends. The CALL command cause that when git.bat ends, the execution continue at the line after the CALL

Related

Bundle a Set of Commands Into a Single Custom Command (Windows Command Prompt)

I have a clone of a git repository nested in many subfolders, and it takes a while to cd through all of them in order to call git pull. Is there a way to create a custom command that just bundles all of the cd statements + git pull together for Windows CMD?
A batch file would solve your problem, just call the commands one after another.
cd_pull.bat
#ECHO OFF
cd D:/Projects/my_folder/my_other_folder/yet_another_folder/my_git_repo
git pull

Watch for file changes and add changes to github?

On OSX I want to create a script that 'when a file changes, automatically runs
git add [FILES CHANGED];
git commit -m "Changing ${file names}";
git push -u origin master"
I understand there is fswatch, however, this keeps a process running.
Maybe I can try running "git status" and if != "On branch master
nothing to commit, working directory clean" then automatically push to Github?
I know this is an old thread, but I was also looking for an alternative solution and found rerun2. Perhaps it will also be useful to somebody else.
https://github.com/tartley/rerun2
From Tartley's readme:
It runs COMMAND every time there's a filesystem modify event within
your current directory (recursive). For example, it can re-run tests
every time you hit 'save' in your editor. Or it can re-generate
rendered output (html, SVG, etc) every time you save the source (e.g.
Markdown or GraphViz dot).
COMMAND needs to be a single parameter to rerun, hence if it contains
spaces, either quote the whole thing:
rerun "ls ~"
or escape them:
rerun ls\ ~
How to implement:
If you take your code, and put it inside an update.sh script:
#!/usr/bin/env bash
git add -u
git commit -m "Automatic update of modified files" && git push
Make it executable:
chmod u+x gitpush.sh
Create an alias to make things easier:
alias gitpush=/path/to/gitpush.sh:
Open the terminal, got to the directory you want to watch and run the command:
rerun gitpush
Now it will execute gitpush everytime files changes in that directory.
If you're willing to give up automatically responding to file modifications, you could just periodically run (e.g., via a crontab entry or something):
git add -u
git commit -m "Automatic update of modified files" && git push
The git add -u will stage any modified files, and "git commit" will only be successful if there are modifications to commit.
If you also want to pick up new files, you could use git add -A instead.

Git Hook under Windows

I have the following code to checkout in a working directory in the hook post-receive:
#!/bin/sh
git --work-tree=d:/websites/__gitweb --git-dir=d:/_gitrepo.git/ checkout -f
Unfortunately it doesn't seem to work. However, the command does work when I enter just this in the windows command line (cms):
git --work-tree=d:/websites/__gitweb --git-dir=d:/_gitrepo.git/ checkout -f
I have checked the permissions and the executing attributes but nothing.
UPDATE:
I think I'm getting closer. Now I know what the problem is but I don't know why is this happening. The hook is actually being triggered but I receive this message:
remote: Starting copy from repository to work tree...
remote: fatal: Not a git repository: 'd:/_gitrepo.git/'
remote: Finished.
I have tried to change the path of d: to the whole network path but it still doesn't work. If I go to the remote repository and I do a git log, the changes are there and if I run the hook with sh, it works.
Why is it saying that it is not a git repository when clearly it is?
I finally got it working!
This is really weird. I had to type a pwd to see where actually is the batch being located and it showed the hook location on the server. However, in the next line I added a hostname and it showed me my local hostname.
Then I add the whole path just like:
#!/bin/sh
echo "Starting copy from repository to work tree..."
pwd
hostname
git --work-tree=//remotehost/d$/Webseiten/__gitweb --git-dir=//remotehost/d$/_gitr
epo.git checkout -f
echo "Finished."
I hope this solution works for someone
For a shell script (and those hook scripts will be executed as shell, even in Windows, through the msys layer of msysgit), you could use a different sort of path:
#!/bin/sh
git --work-tree=/d/websites/__gitweb --git-dir=/d/_gitrepo.git/ checkout -f
See also other possibilities with "Posix path conversion in MinGW"

Git pre-commit hook is not running on Windows

I'm just starting to look into Git hooks, but I can't seem to get them to run.
I set up a local repository, so there is now a '.git' directory in my project folder. I have added a '.cmd' file into the C:/path/to/my/project/.git/hooks directory named 'pre-commit.cmd'. Here is the contents of this file:
echo "HOOK RUNNING"
echo. 2>C:/path/to/my/project/.git/hooks/EmptyFile.txt
This should echo the text "HOOK RUNNING" and create an empty text file in that directory. However, if I commit changes through my IDE (NetBeans) or use Git Bash to commit, neither of them seem to run my pre-commit hook, as no file is created.
My understanding is that all you have to do to get a hook to run is add an executable with the name of the hook (as I have done). Am I doing something wrong?
Note: This is on a Windows 7 PC.
Name your hook pre-commit (without any file extension).
And add #!/bin/sh on the first line or #!/bin/bash.
You probably don't have the permissions to run the pre-commit file
Run in your terminal:
chmod +x .git/hooks/pre-commit
Thanks to #vaughan for giving the idea
TL;DR
Git hooks work on Git for Windows by default assuming the Git hook script is simple.
Background of Git and Windows
Please Note: Git was made for shell interpretation; thus, using Git hooks on a Windows command prompt or Windows-made PowerShell will inherently have its flaws, and complete interoperability is not to be expected.
Using Git hooks on Windows is possible, but it has many drawbacks.
Git for Windows uses a shell emulator that makes Bash and shell commands possible. This means that when a Git hook is activated by Git, the Windows version will run the command using the shell emulator. Which in turn, will convert those shell commands to commands that the Windows operating system can understand. Meaning, simple shell scripts will work right off the bat. For example, the Git hook pre-commit that ships with an initialization of a Git repository can be run without any modification.
Example of default behavior
Initialize a Git repository with the command git init
Navigate to the Git hooks directory with the command cd .git\hooks
This directory holds all the Git hook scripts. Create a file named pre-commit. Note:
The name of the file is important
Replace the contents with the following shell script
#!/bin/sh
echo "Hello, World!"
Navigate back to your root directory of the project and create a file named test.txt using the command echo "something" > text.txt
Stage the file to commit using the command git add test.txt
Commit the change and watch the pre-commit hook activate using the command git commit -m "test commit"
Verify the output to look like the following
git commit -m "test commit"
Hello, World!
[master f00ccea] test commit
Example of bad behavior
When using a very advanced shell script to do things in Git hooks, Windows shell interpretation doesn't always stack up. For example, when using the Husky Git hook plugin for NPM, along with the Prettier formatter, the commands do not map 1-1. Meaning that your pre-commit Git hook will fail on Windows.
Answering user1578653's question
A Git hook is an executable script; however, you are using a command prompt script (.cmd) and not a shell script (.sh). If you would like this behavior you described on a Windows operating system then create the file named pre-commit and place it in the .git\hooks directory (relative to the project you are working on). Then place the following content in that file.
.git\hooks\pre-commit
#!/bin/sh
echo "HOOK RUNNING"
thisCausesError 2> .git/hooks/EmptyFile.txt
You will notice that the Git hook works and outputs the words HOOK RUNNING to the console, and the thisCauseError will print an error message to standard error that will be printed to the file EmptyFile.txt.
In my case, I had set core.hooksPath to the wrong directory.
Resetting it with git config --global core.hooksPath '~/.githooks' solved the issue :)
You can verify your hooks path using git config --get core.hooksPath
For me, I tried to run a .bat file.
I discovered that backslashes need to be escaped:
For example:
#!/bin/bash
C:\\somefolder\\somefile.bat
For me, none of the previous solutions worked. I moved the pre-commit file from hooks to some other location, effectively deleting the file from hooks.
That worked for me :D
In my case, where I did npm install and accidentally deleted the .git folder, npm install pre-commit --save worked.
If it helps anyone:
I was getting the following error:
error: cannot spawn .git/hooks/pre-commit: No error
It turned out that in my pre-commit file I did not have 'newline' character after last exit command:
#!/bin/sh
# From gist at https://gist.github.com/chadmaughan/5889802
# Stash any unstaged changes
git stash -q --keep-index
# Run the tests with the Gradle wrapper
./gradlew test --daemon
# Store the last exit code in a variable
RESULT=$?
# Unstash the unstashed changes
git stash pop -q
# Return the './gradlew test' exit code
exit $RESULT
# << must have a newline after the above command >>
I was running a Gradle project on Windows and Gradle commands in the Cmder shell and cmd.
I tried solutions suggested in other answers and it didn't help to fix this problem:
cannot spawn .git/hooks/pre-commit: No such file or directory
The solution, which worked for me, was to rename the file .git/pre-commit.sample to .git/pre-commit and insert the script for formatting changed files with Prettier. The file with the name 'pre-commit' which I have created manually must have had some problems (encoding or end-line symbols remains unclear).

How do I execute several git commands in a batch file without terminating after the first command?

I tried to put a series of GIT commands that I always use continuously togeter as batch files so that I don't repeat myself too much. For example, I have this batch file called update_repo_branch.bat to update a local repo and synch a branch with the remote branch:
#echo off
if(%1) == () goto end
if(%2) == () goto end
cd %1
git checkout %2
git fetch origin
git merge oring/%2
:end
Good to be lazy, but what I found is that when a GIT command is finished, it seems to send an exit flag back to terminate whatever is running. Therefore, using a batch file to exectute them all in one go simply doesn't work. Any idea how to work around it?
I'm not sure if this is true for all Windows git packages, but at least some use a git.cmd script as a wrapper around the actual git executables (for example git.exe). So when you're batch file uses a git command, Windows is actually running another batch file.
Unfortunately, when one batch file invokes another, by default it 'jumps' to the invoked batch file, never to return (this is for compatibility with ancient MS-DOS command processors or something).
You can solve this problem in a couple ways:
invoke git in your batch files using the call command to run the git.cmd batch file and return back to yours:
call git checkout %2
call git fetch origin
rem etc...
invoke git in your batch file using the .exe extension explicitly to avoid the git.cmd batch file altogether. For this to work, you might need to make sure that you have your path and other environment variables set the way git.exe expects (that seems to be what git.cmd does in msysgit):
git.exe checkout %2
rem etc...
Assuming you are using msysGit as your Git client you might actually want to use Bash scripts for this. You could place a bash function in your ~/.bashrc (~ is usually your C:\Users\- see here) as follows
update_repo_branch() {
if [ $# != "2" ]; then
echo "Usage: update_repo_branch REPO BRANCH" 1>&2
return 1
fi
cd $1
git checkout $2
git fetch origin
git merge origin/$2
}
You can then run update_repo_branch myrepo cool-branch from the mysysGit shell.
Of course, this won't be accessible from cmd.exe. You will only be able to use it within the msysGit cygwin shell.
As i see from your example you're actually trying to sync your local branch 'branchname' with origin/branchname
For this you don't need any additional scripting, you just have to use git pull instead of sequence git checkout branchname; git fetch origin; git merge origin/branchname
take a look at the docs about tracking branches in git and their benefits.
generally speaking if you have a repo layout like this:
git branch -a
...
master
dev1
dev2
remotes/origin/master
remotes/origin/dev1
remotes/origin/dev2
And your dev1 and dev2 branches are tracking branches for origin/dev1 and origin/dev2 correspondingly then you just need to execute in repository:
git pull
This command will effectively sync up all you local tracking branches with remote ones.
for more see here:
Git pull docs
Git remote branches and tracking branches (Progit book)
Create a notepad file and paste the below content and save with .bat extension.
This script can be use for setting up git first time in a project.
!echo off
git init
git add .
git commit -m "first commit"
git branch -M main
git remote add origin <YOUR_REPO_URL>
git push -u origin main
pause

Resources