How to call Git Bash from Windows and preserve autocrlf - windows

I have automated python scripts for git versioning system. It works on linux, but I have some problems on windows. I will go directly to the core of the problem...
There are sources ended by LF (not CRLF) and Windows Git Bash can handle it perfectly (checkout as Windows style and commit as unix style).
When I open Git Bash and type "git status" I get
your branch is up-to-date.....nothing to commit, working directory clean
When I open cmd.exe and I write "git status"
....
modified: example.h
modified: example.cpp
....
the modification is just because cmd.exe does not handle autocrlf. the monification is just in line endings.
I tried a workaround:
c:\myrepo>echo git status|"C:\Program Files (x86)\Git\bin\sh.exe" --login -i
output:
Welcome to Git (version 1.9.2-preview20140411)
Run 'git help git' to display the help index.
Run 'git help <command>' to display help for specific commands.
xxx#yy /c/myrepo (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
xxx#yy /c/myrepo (master)
$
xxx#yy4 /c/myrepo (master)
$ logout
This almost solves my issue, but there is an annoying welcome message which makes my script not working.
Another workaround could be without --login -i:
c:\myrepo>echo git status|"C:\Program Files (x86)\Git\bin\sh.exe"
output:
....
modified: example.h
modified: example.cpp
....
The command without --login -i solves the welcome issue, but it does not autoclrf.
I could delete welcome mesage file , but I want my project to be usable after checkout without deleting anything in system.
Is there some painless solution?

Related

difference between git --git-dir checkout and git checkout

git checkout acts differently for using option --git-dir.
Commands :
inside_gitdir$ git checkout remote/branch
outside_gitdir$ git --git-dir=/path/.git checkout remote/branch
Here command 1 works well and checking out. but the command 2 shows to move files like :
error: The following untracked working tree files would be overwritten by checkout:
someFiles
Please move or remove them before you can switch branches.
Aborting
I thought both the commands are running in same way. But,
Why am i getting this error in second command not in first ?
What is the difference of using --git-dir option ?
I ran into the same issue.
It seems to work if you do: git --git-dir=/path/.git --work-tree=/path/ checkout remote/branch

Git for Windows "No tags file" Response from "git diff" Command

Git version: 2.14.2.2
Whenever I run git diff on a repository I am greeted with the response No tags file. I have tried running the command on multiple repositories, multiple consoles (Cmd, PowerShell, MINGW64, Visual Studio Command Prompt) and all have the same response.
Strangely, the git log command also fails. Many other commands work, however, such as git status, git pull, etc. It seems to be only log and diff.
I have uninstalled Git entirely and reinstalled. Restarted my system. Tried referencing the git.exe directly (which yields the exact same response). Nothing is working and I have not seen this error anywhere else. I compared my user configs with those of a colleague and they are identical.
Some portion of the command executes properly, because if I supply two commit hashes, and I intentionally break one, the response I receive is:
It seems like another program may be hijacking the git diff command. I believe this because I'm not sure "No tags file" is even a possible Git response. Not sure what else it would be.
To make things even more confusing- my ultimate goal is to run the git diff within the context of an msbuild and it DOES EXECUTE CORRECTLY. Now, I could be satisfied with this, but I need to modify the diff command slightly, and running a full build each time is not productive, nor easy to troubleshoot. There is a task within the build script that runs an Exec command and it has no issues performing the diff. I'm also able to execute a Diff Against Current within SourceTree, which to the best of my knowledge, runs a git diff behind the scenes.
Any help would be very much appreciated.
:: Edits ::
Various commands:
git diff HEAD~1 HEAD
git diff master~1 master
git diff <commit-hash-1> <commit-hash-2>
git log HEAD~1..HEAD
git log master~1..master
git log <commit-hash-1>..<commit-hash-2>
Output:
Every one of the commands above returns the same No tags file response, in all of my repos.
Cat Head:
cd .git
cat HEAD
ls -R refs
Output:
New Repo:
mkdir testrepo
cd testrepo
git init
echo "file1" > file1.txt
git add .
git commit -m "initial commit of file1.txt"
echo "Hi there!" > file2.txt
git add .
git commit -m "added file2.txt"
git log
git diff HEAD~1 HEAD
Output:
git config -e:
git config --global -e:
::Edits 2::
I uninstalled all of my diffing/source control tools (SourceTree, Git, SVN, WinMerge, KDiff). Installed the portable version of Git. Opened CMD to a repo, put in full path to the git.exe portable and it still returned the No tags file response.
I also reviewed all of my path variables for: git, vim, ming, mintty and anything else that seemed suspect, but didn't find any.
I have restarted after performing all steps, and yet the problem persists.
::Edits 3::
I have a different user on my laptop, switched to that user and the git diff works properly, so clearly there is something with my main user that is conflicting. Will continue to look into my User directory for issues.
Here are the steps I'd take in this situation:
Try the following and check the response:
git diff HEAD~1 HEAD
git diff master~1 master
git diff <commit-hash-1> <commit-hash-2>
Try the same with log:
git log HEAD~1..HEAD
git log master~1..master
git log <commit-hash-1>..<commit-hash-2>
I'm actually guessing that your refs are messed up, which means that the direct hashes might work, but the HEAD and/or master one may not.
Look into the .git/refs folder
From the main repo folder:
cd .git
cat HEAD
ls -R refs
Hopefully, HEAD is pointing to a branch, and if master is checked out, cat HEAD output should look like:
ref: refs/heads/master
Then, the ls -R refs, should show a heads folder, with files for each of your local branches (i.e. master and possibly others). You also likely have refs/remotes and refs/tags directories.
If any of these things are radically different or missing, that could be your issue...
Since you have reinstalled git, create a brand new repo and try the same commands:
mkdir testrepo
cd testrepo
git init
echo "file1" > file1.txt
git add .
git commit -m "initial commit of file1.txt"
echo "Hi there!" > file2.txt
git add .
git commit -m "added file2.txt"
git log
git diff HEAD~1 HEAD
If this last one works, then git is likely working okay, but some tool you have is messing things up.
Post your config from git config -e and git config --global -e - maybe we can see something?
When googling for the "No tags file" message, the first results I get all talk about vi.
I do not understand why git would try to execute vi when running git diff or git log, could it be that your system is configured to use vi as a pager ?
# some possible places which could influence that :
echo $PAGER
echo $GIT_PAGER
git config --get core.pager
When digging in the documentation for less, I found that less can use a ctags file, to spot "the file that contains this tag".
So you can also look at the list of variables that influence the behavior of less :
# from bash :
# env will list all the defined environment variables
env
# the ones that impact 'less' should contain "LESS" in their names :
env | grep LESS

Windows custom git commands

Say I want a new git command, git new, that makes a new branch that is up to date with origin/master.
Is there a way I can make this script and have it available in all repositories on Windows from powershell?
edit: To clarify I want a git script not a powershell function. The only reason I mentioned powershell is because I don't use git bash.
Create a batch file that contains the following commands:
git branch %1 origin/master
git checkout %1
Save it, let's say, as C:\Scripts\new-branch.cmd. (I never worked with PowerShell, I don't know its rules. However, it should work as well using the old Windows Command Prompt).
Test the batch file works as expected by running:
C:\Scripts\new-branch.cmd test1
It should output something along these lines:
Branch test1 set up to track remote branch master from origin by rebasing.
Switched to branch 'test1'
Your branch is up-to-date with 'origin/master'.
If you don't need the new branch to track the remote branch then you just add --no-track to the git branch command.
If everything goes well then run:
git config --global alias.new "!C:/Scripts/new-branch.cmd"
This makes the Git alias new available to your Windows profile in all repositories. If you need it only in one repository then remove --global and run the command when the current directory is in the repository where you need it.
Use it as:
git new test2
You can use a git alias which uses git checkout:
git config --global alias.new 'checkout origin/master -b'
This would then be used as git new new_branch.
(Which is equivolent to git checkout origin/master -b new_branch
See the git docs for checkout. I tried the command and it worked, but when I looked at the docs, I didn't find a syntax that exactly matched my form. (Closest is git checkout [-q] [-f] [-m] [[-b|-B|--orphan] <new_branch>] [<start_point>])
Note: #axiac, I may have used the !git because it doesn't hurt, and I may have needed to do multiple commands to solve the problem, and didn't remove it when I was done.

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).

svn commit from bash script without opening the editor

I try to write a bash script which should commit to a svn repo. Everything works fine until the point where I try to commit. The commit command opens the editor and the script ends with an error that the commit message was left in svn-commit.tmp
I try a couple of things but none will work
commit_msg="$1"
svn commit -m "$commit_msg"
and
commit_msg="$1"
svn commit -m '$commit_msg'
and
commit_msg=$1
svn commit -m '$commit_msg'
and all with the -q and --non-interactiveoperators. Even svn commit -m "woohoo" opens the editor and the script ends with the error.
Any ideas why it is impossible to commit within a bash script without opening the editor?
You should use the --non-interactive option on the svn command:
svn commit --non-interactive -m '$commit_msg'
After I tried svn ci instead of svn commit, everything was fine. My first thought was a buggy version of svn. Asking the Great Dump (aka Google) I found the solution:: In my .bash_profile was a code snippt that forced the svn commit always to open the editor while svn ci worked as expected.
I don't know exactly where the code snippet came from, but windows users have to fight a lot of mysterious behaviours.
Thanks all for your help.

Resources