MS-Windows中的Git命令行

Git command line for MS-Windows

Inhalt

  • 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

Topic:.git.cmd.

.

1 Download and install, or copy the git command line suite for MS-Windows

Topic:.git.cmd.install.

You can download a git suite for command line processing from https://git-scm.com/download/win and install it. The installed files run on an other computer without installing. That may be important if you have experience with several git installations.

I have installed Git-2.9.3.2-32.bit.exe on a computer running under Windows-XP. I have answered all questions during the install process with 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 of 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), don‘t select Use Windows‘ default console window
  • Enable file system chaching
  • I have select D:\Programs\Gitcmd as destination for installation. That is my tool folder.

This produces the following files (raw):

D:\Programs\Gitcmd>dir
 Directory of D:\Programs\Gitcmd
29.08.2016  11:50    <DIR>          .
29.08.2016  11:50    <DIR>          ..
29.08.2016  11:38    <DIR>          bin
29.08.2016  11:38    <DIR>          cmd
29.08.2016  11:38    <DIR>          dev
29.08.2016  11:38    <DIR>          etc
25.08.2016  13:47           151.272 git-bash.exe
25.08.2016  13:47           150.760 git-cmd.exe
09.08.2016  19:02            18.765 LICENSE.txt
29.08.2016  11:38    <DIR>          mingw32
25.08.2016  15:06            77.551 ReleaseNotes.html
29.08.2016  11:38    <DIR>          tmp
29.08.2016  11:38           996.808 unins000.dat
29.08.2016  11:34         1.293.032 unins000.exe
29.08.2016  11:38            22.815 unins000.msg
29.08.2016  11:38    <DIR>          usr
               7 File(s)      2.711.003 bytes
               9 Dir(s)  83.736.236.032 bytes free

I have copied that 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_1608
03.05.2015  13:29    <DIR>          .
03.05.2015  13:29    <DIR>          ..
25.08.2016  23:33                20 .bash_history
31.07.2014  19:55    <DIR>          bin
31.07.2014  19:55    <DIR>          cmd
31.07.2014  19:55    <DIR>          doc
31.07.2014  19:55    <DIR>          etc
04.02.2014  17:27               793 Git Bash.lnk
02.06.2013  12:30               708 Git Bash.vbs
31.07.2014  19:55    <DIR>          lib
31.07.2014  19:55    <DIR>          libexec
02.06.2013  12:30            38.203 ReleaseNotes.rtf
31.07.2014  19:56    <DIR>          share
31.07.2014  19:56    <DIR>          ssl
04.02.2014  17:26           566.105 unins000.dat
04.02.2014  17:25         1.243.915 unins000.exe
               6 File(s)      1.849.744 bytes
              10 Dir(s)  83.732.533.248 bytes free

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

D:\Programs\Gitcmd_1608\bin>dir sh.exe
04.02.2014  17:27           540.672 sh.exe

D:\Programs\Gitcmd\bin>dir sh.exe
25.08.2016  13:47            43.240 sh.exe

There are some changes in the tool suite, but the behaviour for the usage is the same.

2 sh.exe

Topic:.git.cmd.sh.

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

2.1 pure run >sh.exe

Topic:.git.cmd.sh.pure.

Without any other action, only invoked from a Windows command line (double click in explorer), it opens a linux-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, here is an example content of the shell window after some commands:

Hartmut(at)lenovo-a6f13ea5 MINGW32 /bin

$ pwd
/bin

$ whoami
Hartmut

$ ls / -all
total 4080
drwxr-xr-x 1 hartmut 197121       0 Aug 29 11:50 .
drwxr-xr-x 1 hartmut 197121       0 Aug 29 11:50 ..
-rw-r--r-- 1 hartmut 197121   18765 Aug  9 19:02 LICENSE.txt
-rw-r--r-- 1 hartmut 197121   77551 Aug 25 15:06 ReleaseNotes.html
drwxr-xr-x 1 hartmut 197121       0 Aug 29 11:38 bin
drwxr-xr-x 1 hartmut 197121       0 Aug 29 11:38 cmd
drwxr-xr-x 1 hartmut 197121       0 Aug 29 11:38 dev
drwxr-xr-x 1 hartmut 197121       0 Aug 29 11:38 etc
-rwxr-xr-x 1 hartmut 197121  151272 Aug 25 13:47 git-bash.exe
-rwxr-xr-x 1 hartmut 197121  150760 Aug 25 13:47 git-cmd.exe
drwxr-xr-x 1 hartmut 197121       0 Aug 29 11:38 mingw32
dr-xr-xr-x 9 hartmut 197121       0 Aug 29 14:49 proc
drwxr-xr-x 1 hartmut 197121       0 Aug 29 14:49 tmp
-rw-r--r-- 1 hartmut 197121  996808 Aug 29 11:38 unins000.dat
-rwxr-xr-x 1 hartmut 197121 1293032 Aug 29 11:34 unins000.exe
-rw-r--r-- 1 hartmut 197121   22815 Aug 29 11:38 unins000.msg
drwxr-xr-x 1 hartmut 197121       0 Aug 29 11:38 usr

$ echo $PATH
/mingw32/bin:/usr/bin:/c/Dokumente und Einstellungen/Hartmut/bin:/d/Programs/Pyt
hon27:/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/Microso
ft SQL Server/90/Tools/binn:/d/Progs/Bazaar:/c/Programme/Skype/Phone:/d/Programs
/Bazaar

$ echo $HOMEDRIVE
C:

$ echo $HOMEPATH
\Dokumente und Einstellungen\Hartmut

$ echo $HOME
/c/Dokumente und Einstellungen/Hartmut

$ git --version
git version 2.9.3.windows.2

$

The environment variables $HOMEDRIVE and $HOMEPATH are set in the windows environment. The $HOME is set newly for this shell with the input of $HOMEDRIVE and $HOMEPATH. The $PATHis set from the given path but enhanced with /mingw32/bin:/usr/bin and change ; to : (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 sh.exe is stored is not member of. Instead usr\bin\ is mounted as bin/ which is in the path. Furthermore /c/ and /d/ are available which is the whole hard disk‘s content. /c/ and /d/ are not shown with the ls command, but they are available though.

2.2 run sh.exe in a batch file with some environment variables

Topic:.git.cmd.sh.bat.

The HOMEDRIVE and the HOMEPATH environment variable may be other than for windows usage. Therefore they are set before, and then sh.exe is called:

Content of unix_window.bat:

@echo off
set PATH=D:\Programs\Gitcmd\bin;%PATH%
set HOMEPATH=\vishia\HOME
set HOMEDRIVE=D:
::sh.exe --login -i
sh.exe

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

The variant with --login -i came from an older git cmd tool suite (from 2010 I think), it has no effect for the newer one.

That batch creates a Unix/Linux shell window to have 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

Topic:.git.cmd.sh.batcmd.

Content of unix_cmd.bat:

@echo off
set PATH=D:\Programs\Gitcmd\bin;%PATH%
set HOMEPATH=\vishia\HOME
set HOMEDRIVE=D:
REM The only one argument of this batch should be given with "" usual,
REM because the argument of the batch is the command with arguments for sh.exe-execution.
REM -x to output the command as they are executed.
REM -c to run the following argument in "" as command, then exit.
REM Write unix_cmd.bat "cmd arg1 ‘arg2 with spaces‘ arg3" for arguments with spaces.
echo on
sh.exe -x -c %1

This executes the given command by argument.

If that batch is invoked from another batch for example with a complex git command, one should regard some special rules for 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/CalculatorExpr.java" >test.txt

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

Note that the character % should be 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 sh.exe was invoked immediately in a batch.

2.4 run sh.exe in a batch file to execute a shell script

Topic:.git.cmd.sh.batscript.

Content of unix_script.bat:

@echo off
set PATH=D:\Programs\Gitcmd\bin;%PATH%
set HOMEPATH=\vishia\HOME
set HOMEDRIVE=D:
REM -x to output the command as they are executed.
echo on
sh.exe -x %1

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

2.5 The sh.exe executes MS-Windows executables

Topic:.git.cmd.sh.exeWin.

In a shell script all windows executables can be invoked. Only the internal shell commands are linux-typical. If executables are equal for unix and windows you can use the same linux shell scripts for both platforms, maybe it is lesser effort for maintenance. Nevertheless some options are slightly different.

2.6 Run sh.exe from a Java application

Topic:.git.cmd.sh.java.

Java provides with the system‘s class javadoc-java/lang/ProcessBuilder the capability to run a opertion system process with 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 deal with input, output, abort, and several additional possibilities. That class is used to run sh.exe respectively git from 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 are the adequate operations like in the MS-Windows-batches above. Only with Java syntax. The PATH enhancement is valid only for the running cmd, not for the start. Therefore the sh.exe should be invoked with the full path. The special arguments should be taken from a config file, it is TODO yet.

3 git log

Topic:.git.cmd.log.

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

--pretty=format:%H %an %ad %+s

It seems to be a format in one line-thinking, proper for simply parsing algorithm for example with pearl text processing. Therefore the format flag ,%s, writes the commit message in one line. All line-feed characters are replaces by spaces. That has the disadvantage that the originally commit message are 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 is in progress .... TODO

原:

时间: 2024-07-30 13:04:49

MS-Windows中的Git命令行的相关文章

vs中集成git命令行插件Gitsoler

用vs操作git看不到任何信息,而且觉得git命令行用着也挺方便,但在开发时用命令行每次都要切到文件操作觉得麻烦,找了一圈找了个方法是直接用NuGet的命令行窗口,这个方法也是可以的,不过就是用着老是出现乱码... 有天又再找其他方法,方法没找到,但是发现了vs有插件市场(以前都没用过- -!),搜了下git,发现还真有个集成gitbash到vs窗口的插件(Gitsoler),安装了下,真香,哈哈 ! 可能有其他插件,不过没用过,大神勿喷 ^_^ 原文地址:https://www.cnblogs

Python - Git for windows 下使用 交互式命令行

1. 概述 尝试在 windows 环境的 git 命令行使用 python3 的交互模式 2. 环境 OS win10 1905 git 2.20.1 mintty 2.9.4 python 3.7.0 2. 问题 直接使用 结果 无法开启, 会卡住 3. 解决 命令 > winpty python 4. 其他 ref Git for windows的MINGW64终端无法使用Python的解决方法 疑问 winpty 是干嘛的 原文地址:https://www.cnblogs.com/xy1

Windows下的cmd命令行中设置环境编码

我们都知道,Windows下的cmd命令行默认编码是Windows系统的编码,就是ANSI编码或者说是GBK编码的,这样我们编写的很多应用比如php编写utf-8编码的应用在命令行下面运行时都会出现乱码的情况,原因就是因为运行环境的编码使用自己的编码去解析程序运行结果,而程序本身返回编码和解析编码不一致,最后导致乱码情况的发生. 遇到这种情况,我们可以通过设置cmd命令行环境的编码使编码保持统一来解决,方法如下: 在命令行中,有个命令是chcp,这个命令是用来显示当前活动代码页编号的,也可以理解

git命令行使用

1. git命令行配置 1)安装完git客户端之后,在任意目录右键鼠标,选择Git GUI Here 2)在打开的窗口中依次选择Help->> Show SSH Key 2)如果弹出的窗口中中间部分没有key,可以点击 Generate Key 按钮,生成key,然后将生成的key拷贝一下,添加到gitlab中即可. 2. 命令使用 1)下载代码 在存放代码的目录中,右键鼠标,选择 Git Bash Here,然后会打开终端窗口 输入: git init   创建git配置目录 输入下面命令,

Windows系统下用命令行编译C/C++程序过程总结

转自:http://www.cnblogs.com/caikehe/archive/2013/01/12/2858017.html (1)先用记事本编写如下所示的代码,并另存为hello.cpp,假设其保存路径为C:\Users\Administrator\Desktop. #include using namespace std; int main() { cout<<"hello world!"<<endl;< span="">

git命令行(2)

一.父提交的表示方法 1.HEAD引用 在.git/HEAD目录下存在一个HEAD文件,其记录着当前工作区对应的SHA1.如果当前工作区从某个分支检出(checkout),那么这个HEAD文件中的引用最终执行分支对应的SHA1,如果处于分离头状态(不对应分支,从某个commit检查),那么这个HEAD文件中保存的就是检出的commit SHA1. 如果我们直接使用git checkout master分支,那么HEAD的值为ref: refs/heads/master.指向.git/refs/h

【重要】使用Git命令行上传到GitHub上

[本人GitHub账号:] 用户名:chenhongshuang 密码:shuangshuang6300 邮箱:[email protected] 进入GitHub账号后 1·新建项目文件名称例demo,并且点击[Branch:master 输入 gh-pages 后回车键]::: [建好以后必须在自己电脑的任意位置建"相同项目文件名称demo"] 2·cd进入本地新建的项目文件中执行:git init 3·把文件添加到版本库中,使用命令 git add .添加到暂存区里面去,不要忘记

3.Git 命令行操作

1.Git 命令行操作(本地库操作): 1.1. 创建本地库(本地库初始化): 第一步:首先在D盘建了个名为git空文件夹,命令行中cd到这个文件夹: 第二步:通过git init命令把这个目录变成Git可以管理的仓库: 文件夹会生成一个.git目录,不用去改它,这个目录默认是隐藏的,用ls -lah命令就可以看见 注意:.git 目录中存放的是本地库相关的子目录和文件,不要删除,也不要进行修改: 参考:https://blog.csdn.net/songkuiyuansky/article/d

eclipse使用git命令行

eclipse使用git的命令行很简单,无需额外配置,类似idea的方式 在eclipse的preferences中搜索terminal,可以看见git的配置(之前计算机上装过git),就类似于idea的terminal配置一样 之后,去打开eclipse的terminal,windows->show view中选择terminal,显示terminal(或 Ctrl + Shift + Alt + T) 之前百度搜的一堆配置console方式打开git命令行,,感觉没什么意义,这边放一个配置c