git command line in Ms-windows

Source: Internet
Author: User

Git command line for Ms-windows


    • 1 Download and install, or copy the git command line suite for Ms-windows
    • 2 Sh.exe
      • 2.1 Pure Run >sh.exe
      • 2.2 Run Sh.exe in a batch file with some environment variables
      • 2.3 Run Sh.exe in a batch file to execute one Linux cmd, especially git with options
      • 2.4 Run Sh.exe in a batch file to execute a shell script
      • 2.5 The Sh.exe executes ms-windows executables
      • 2.6 Run Sh.exe from a Java application
    • 3 git log



1 Download and install, or copy the git command line suite for Ms-windows


You can download a git suite for command line processing from Https:// and install it. The installed files run on a other computer without installing. That is important if you had experience with several git installations.

I have installed on Git- a computer running under WINDOWS-XP. I have answered all questions during the install process and the minimal influence to the operation system:

    • Select Components:deselect all! No icons, no Windows Explorer integration, no change of file extension-associations.

    • No change in the system ' s PATH variable: Use git from git Bash only

    • Use OpenSSH

    • Configure The line ending conversions: Checkout as-is, Commit-as-is: see TODO

    • Use mintty (the default terminal of MSYS2), and don ' t select Use Windows ' default Console window

    • Enable File System chaching

    • I have a select as D:\Programs\Gitcmd destination for installation. That's my tool folder.

This produces the following files (raw):

 d:\programs\gitcmd>dir Directory of D:\Programs\Gitcmd29.08.2016 11:50 <DIR>. 29.08.2016 11:50 <DIR>..           29.08.2016 11:38 <DIR> bin29.08.2016 11:38 <DIR> cmd29.08.2016 11:38 <DIR> dev29.08.2016 11:38 <DIR> etc25.08.2016 13:47 151.272 git-bash.exe25.08.2016 13:4 7 150.760 git-cmd.exe09.08.2016 19:02 18.765 license.txt29.08.2016 11:38 <DIR> mi           ngw3225.08.2016 15:06 77.551 releasenotes.html29.08.2016 11:38 <DIR> tmp29.08.2016 11:38 996.808 unins000.dat29.08.2016 11:34 1.293.032 unins000.exe29.08.2016 11:38 22.815 unins000. msg29.08.2016 11:38 <DIR> usr 7 File (s) 2.711.003 bytes 9 DIR (s) 83.7 36.236.032 bytes Free 

I have copied the file tree to a windows-7-computer, it runs perfect, without installation.

An older version of Gitcmd gives the following files:

 d:\programs\gitcmd_1608>dir Directory of D:\Programs\Gitcmd_160803.05.2015 13:29 <DIR>. 03.05.2 015 13:29 <DIR>. 25.08.2016 23:33 bash_history31.07.2014 19:55 <DIR> bin31.07.2014 19:55 <dir > cmd31.07.2014 19:55 <DIR> doc31.07.2014 19:55 <DIR> etc04.02.2014 17:          793 git bash.lnk02.06.2013 708 git bash.vbs31.07.2014 19:55 <DIR>     lib31.07.2014 19:55 <DIR> libexec02.06.2013 38.203 releasenotes.rtf31.07.2014 19:56 <DIR> share31.07.2014 19:56 <DIR> ssl04.02.2014 17:26 566.105 Unins000.da t04.02.2014 17:25 1.243.915 Unins000.exe 6 File (s) 1.849.744 bytes ten Dir (s) 83. 732.533.248 bytes Free 

There is some and directories, is mingw3w missing. Nevertheless it produces the adequate results. Both contains

D:\programs\gitcmd_1608\bin>dir sh.exe04.02.2014  17:27           540.672 sh.exed:\programs\gitcmd\bin>dir sh.exe25.08.2016  13:47            43.240 Sh.exe

There is some changes in the tool suite and the behaviour for the usage is the same.

2 Sh.exe

The D:\Programs\Gitcmd . is \bin\sh.exe a Windows program to run Linux shell scripts or Linux commands.

2.1 Pure Run >sh.exe

without any other action, only invoked from a Windows command line (double click in Explorer), it opens a l Inux-like Command Window. That shell window gets the environment variables from ms-windows and sets the path variable (slightly) properly. One can type in some Linux commands, where is a example content of the shell window after some commands:

Hartmut (at) lenovo-a6f13ea5 mingw32/bin$ pwd/bin$ whoamihartmut$ ls/-alltotal 4080drwxr-xr-x 1 Hartmut 197121 0 Au G 11:50. Drwxr-xr-x 1 Hartmut 197121 0 29 11:50.. -rw-r--r--1 Hartmut 197121 18765 9 19:02 license.txt-rw-r--r--1 Hartmut 197121 77551 in 15:06 releasenotes. Htmldrwxr-xr-x 1 Hartmut 197121 0 11:38 bindrwxr-xr-x 1 hartmut 197121 0 in 11:38 cmddrwxr-xr-x 1 h Artmut 197121 0 11:38 devdrwxr-xr-x 1 hartmut 197121 0 in 11:38 etc-rwxr-xr-x 1 Hartmut 197121 151       272 13:47 git-bash.exe-rwxr-xr-x 1 Hartmut 197121 150760 1 13:47 git-cmd.exedrwxr-xr-x 197121 0 11:38 mingw32dr-xr-xr-x 9 hartmut 197121 0 14:49 procdrwxr-xr-x 1 Hartmut 197121 0 29 14: 1 Hartmut 197121 996808 tmp-rw-r--r--11:38 unins000.dat-rwxr-xr-x 1 hartmut 197121 1293032 11:34 unins0 00.exe-rw-r--r--1 Hartmut 197121 22815 11:38 unins000.msgdrwxr-xr-x 1 harTmut 197121 0 11:38 usr$ Echo $PATH/mingw32/bin:/usr/bin:/c/dokumente und Einstellungen/hartmut/bin:/d/progra Ms/python27:/d/programs/python27/scripts:/d/batch:/d/progs/bat_exe:/d/progs/java/jre7/bin:/c/windows/system32 :/c/windows:/c/windows/system32/wbem:/c/programme/microsoft SQL server/90/tools/binn:/d/progs/bazaar:/c/ programme/skype/phone:/d/programs/bazaar$ echo $HOMEDRIVEC: $ echo $HOMEPATH \dokumente und einstellungen\hartmut$ Echo $HOME/c/dokumente und einstellungen/hartmut$ git--versiongit version$

the environment variables  $HOMEDRIVE  and  $HOMEPATH   is set in the Windows environment. the  $HOME  is set newly for this shell with the input of  $HOMEDRIVE  and& nbsp $HOMEPATH . the  $PATH is set from the given PATH but enhanced with  /mingw32/bin:/usr/bin   and change  ; &NBSP;TO&NBSP :   (Windows separator  ;  versus Unix separator  : ).

The root of the Linux directory is built internally. The root is the parent directory from the bin\sh.exe invocation. But the bin itself, where is stored are not sh.exe member of. Instead is mounted as which are in the usr\bin\ bin/ path. Furthermore and is /c/ /d/ available which is the whole hard disk ' s content. /c/ /d/ c9/> command, but they is available though.

2.2 Run Sh.exe in a batch file with some environment variables

The and the environment variable may is other HOMEDRIVE HOMEPATH than for Windows usage. Therefore They is set before, and then is sh.exe called:

Content of unix_window.bat :

@echo offset Path=d:\programs\gitcmd\bin;%path%set homepath=\vishia\homeset homedrive=d:::sh.exe--login-ish.exe

The PATH extension and the environment variables May is set in the Windows system environment in this kind already, but if They is sets only in the this batch and then its changed content is valid only for that session.

--login -ithe variant with came from a older git cmd tool suite (from $ I think), it has no effect for the newer one.

That batch creates a Unix/linux the shell window to the some experiences with Linux command line invocations.

2.3 Run Sh.exe in a batch file to execute one Linux cmd, especially git with options

Content of unix_cmd.bat :

@echo Offset Path=d:\programs\gitcmd\bin;%path%set Homepath=\vishia\homeset Homedrive=d:rem The only one argument of This batch should was given with "" Usual,rem because the argument of the "the" and "the" command with arguments for Sh.exe-ex Ecution. Rem-x to output the command as they is executed. Rem-c to run the following argument in "As command and then exit. REM Write unix_cmd.bat "cmd arg1 ' arg2 with spaces ' arg3" for arguments with Spaces.echo Onsh.exe-x-C%1

This executes the given command by argument.

If that batch was invoked from another batch for example with a complex git command, one should regard some special rules F or batch processing in Windows.

Example content of a test-gitlog.bat:

Unix_cmd.bat "git log--date=iso-z-b--ignore-space-change--no-textconv '--format=tformat:commit::%%h%%H%%an%%ad%%+ S ' Org/vishia/util/ ">test.txt

The Linux cmd should is written as one argument in "...." for the batch. The same rule is valid ifsh.exe -c "command" will be invoked.

Note that the character % should is written twice: %% because the batch processing in Windows detect that % as Expansion switch for environment variables. %%expands to a single % . The same rule is valid if be sh.exe invoked immediately in a batch.

2.4 Run Sh.exe in a batch file to execute a shell script

Content of unix_script.bat :

@echo Offset Path=d:\programs\gitcmd\bin;%path%set homepath=\vishia\homeset homedrive=d:rem-x to output the command as T Hey is executed.echo onsh.exe-x%1

The Sh.exe runs the given as path/to/file a Unix shell script and terminates then. The option is proper to see -x which commands was executed in the script. It's adequate in echo on windows. Adequate some echo ... lines should is written in the script.

2.5 The Sh.exe executes ms-windows executables

In a shell script all Windows executables can invoked. Only the internal shell commands is linux-typical. If executables is equal for UNIX and Windows can use the same Linux shell scripts for both platforms, maybe it's Les Ser effort for maintenance. Nevertheless some options are slightly different.

2.6 Run Sh.exe from a Java application

Java provides with the system ' s class javadoc-java/lang/processbuilder the capability to run a opertion system process wit H a given executable. That works for Ms-windows, for Linux and all other platforms.

The class Srcjava_vishiabase/org/vishia/cmd/cmdexecuter builds a wrapper surround the java/lang/processbuilder to help de Al with input, output, abort, and several additional possibilities. That's used to run sh.exe respectively from git a Java Application:srcjava_vishiagui/org/vishia/gitgui/gitgui.

 cmdexecuter cmd = new Cmdexecuter (); void Initializecmd () {map<string, string> env =   Cmd.environment ();   Env.put ("HomePath", "\\vishia\\HOME");   Env.put ("Homedrive", "D:");   String spath = Env.get ("PATH");   spath = "D:\\programs\\gitcmd\\bin;" + spath; Env.put ("PATH", spath);   } ... public void Revisionscommit (string sgitdir, String sFile) {Cmd.setcurrentdir (new File (Sgitdir));   Out.buffer (). SetLength (0);   Out.assign (Out.buffer ()); To reset positions to the changed Out.buffer () string[] args ={"D:/programs/gitcmd/bin/sh.exe", "-X", "-C", "Git log-   -date=iso '--pretty=raw ' "+ sFile +" ' "};   int error = Cmd.execute (args, null, out, NULL); Out.firstlinemaxpart (); 

There is the adequate operations like in the ms-windows-batches above. Only with Java syntax. The PATH enhancement is valid only for the running CMD and not for the start. Therefore the should is invoked with the full sh.exe path. The special arguments should was taken from a config file, it was TODO yet.

3 git log


It offers a history of all commits, maybe for the whole repository, for some branches or especially for one file.


It seems to bes a format in one line-thinking, proper for simply parsing algorithm for example with pearl Text Pro Cessing. Therefore the format flag ,%s , writes the commit message in one line. All line-feed characters is replaces by spaces. that have the disadvantage that the originally commit message is garbled.

In conclusion the printf-like format is not able to use fortunately. I uses the --pretty=raw format and parse it with a well Java parser Srcjava_vishiabase/org/vishia/util/stringpartscan.

It's in progress .... Todo


git command line in Ms-windows

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.