How open the current directory on Git Bash in windows using keyboard shortcut? - windows

In my Windows laptop i want to set a shortcut key such that whenever I click on the shortcut keys from my keyboard it will open the Git Bash in the current directory and the path would also be the current directory in Git Bash.
Example:
Let i have choose a shortcut Ctrl + G. So, i want that whenever i click on Ctrl+G it will open a git bash and the path on Git bash should be the current directory from where i have entered the shortcut keys.
Inplace of opening Git Bash from right-context, i want to do the same from keyboard only by clicking some key combination to open the Git Bash.
Unable to find any solution to this query. In place of it there are solution to setup on right-click context and i want it to do with the help of keyboard.

You can setup an AutoHotkey script as in here to open a CMD in the current path.
Except instead of opening a new CMD, you can run bash --login, assuming C:\Program Files\Git\bin is in your %PATH%.
Run, cmd /K (start /d "C:\your\path" bash --login)

Related

How can I open command prompt in the current directory as a shortcut

I'm just wondering how I can make a shortcut that will open command prompt in the folder it is currently in.
Why I need this.
I save my c program file in the directory Users\PC\Documents\Other\My_Programs\WIP\Project_1\Test\
So I would have to type cd Users\PC\Documents\Other\My_Programs\WIP\Project_1\Test\ every time I want to open the command prompt in that directory.
Is there a shortcut I can make where I can move that shortcut around anywhere (don't need to change the shortcut) and it will open command prompt in the directory it is current in, so I don't need to keep typing cd .....
you can go to the directory and just type start . in the same directory
or you can make a batch file like test.bat and add below line in the same:
cd Users\PC\Documents\Other\My_Programs\WIP\Project_1\Test\
Save it anywhere and whenever you will run that batchfile, it will automatically move PWD to the required directory
Right-click in the open space of your desktop and click New > Shortcut
For the location, type or copy and paste one of the following lines.
%windir%\system32\cmd.exe /k cd Users\PC\Documents\Other\My_Programs\WIP\Project_1\Test\
As commented by aschipfl, you can already do it, (may be OS dependent)!
In Windows Explorer, or I suppose the desktop, just hold down the shift key, right click in a blank area of it and choose Open command window here, (or whatever your locale states).
I think in Windows 10 at least, the option is even available in the Explorer toolbar menu too.

"git bash here" of subcontext menu opens in windows root path

Right clicking a folder in windows opens the submenu of a windows right click. Where you have the basic OS operations like "Open", "Edit", "rename" etc.
If git is installed you also have the option to "git bash here". This command should open the terminal/cmd and directly point to the right-clicked folder. But it's always openening in the root system folder.
tl;dr; clicking "git bash here" opens the system root folder instead of the right-clicked one
The way I solve my own problem is to uninstall Git, and delete .bashrc manually (cause uninstall Git doesn't remove that file), and install Git again. And the terminal open correctly in my working folder rather than root system folder. Thanks to Todd.
As one of the comments suggest, the .bashrc file is the one to "blame".
Deleting it would solve the issue.
However, it contains valuable declarations. So "don't throw the baby out with the bathwater": a minor edit would do the trick:
Open Git Bash window. While the bash is active, enter the following:
vim ~/.bashrc
In my case, the bottom line (scroll using arrows) was:
cd /c
Just delete it (press twice on d), and then quit with saving, by pressing :, and then wqEnter.
That's it! The next time you open Git Bash Here - you should see the shell active on the relevant folder.

enable get bash here/get gui here on Windows [duplicate]

How to add a context (aka right click) menu to the windows explorer that, when clicked, opens the git-bash console in the current explorer folder?
Step 1. On your desktop right click "New"->"Text Document" with name OpenGitBash.reg
Step 2. Right click the file and choose "Edit"
Step 3. Copy-paste the code below, save and close the file
Step 4. Execute the file by double clicking it
Note: You need administrator permission to write to the registry.
Windows Registry Editor Version 5.00
; Open files
; Default Git-Bash Location C:\Program Files\Git\git-bash.exe
[HKEY_CLASSES_ROOT\*\shell\Open Git Bash]
#="Open Git Bash"
"Icon"="C:\\Program Files\\Git\\git-bash.exe"
[HKEY_CLASSES_ROOT\*\shell\Open Git Bash\command]
#="\"C:\\Program Files\\Git\\git-bash.exe\" \"--cd=%1\""
; This will make it appear when you right click ON a folder
; The "Icon" line can be removed if you don't want the icon to appear
[HKEY_CLASSES_ROOT\Directory\shell\bash]
#="Open Git Bash"
"Icon"="C:\\Program Files\\Git\\git-bash.exe"
[HKEY_CLASSES_ROOT\Directory\shell\bash\command]
#="\"C:\\Program Files\\Git\\git-bash.exe\" \"--cd=%1\""
; This will make it appear when you right click INSIDE a folder
; The "Icon" line can be removed if you don't want the icon to appear
[HKEY_CLASSES_ROOT\Directory\Background\shell\bash]
#="Open Git Bash"
"Icon"="C:\\Program Files\\Git\\git-bash.exe"
[HKEY_CLASSES_ROOT\Directory\Background\shell\bash\command]
#="\"C:\\Program Files\\Git\\git-bash.exe\" \"--cd=%v.\""
And here is your result :
I had a similar issue and I did this.
Step 1 : Type "regedit" in start menu
Step 2 : Run the registry editor
Step 3 : Navigate to HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell. If you don't have the shell key, create one.
Step 4 : Right-click on "shell" and choose New > Key. name the Key "Bash"
Step 5 : Modify the value and set it to "open in Bash" This is the text that appears in the right click.
Step 6 : Create a new key under Bash and name it "command". Set the value of this key to your git-bash.exe path.
Close the registry editor.
You should now be able to see the option in right click menu in explorer
PS Git Bash by default picks up the current directory.
EDIT : If you want a one click approach, check Ozesh's solution below
The easiest way is to install the latest Git from here. And while installing, make sure you are enabling the option Windows Explorer Integration.
Once you are done, you will get those options in whenever you right click on any folder.
Hope it helps.
Here are the Registry exports (*.reg files) for Git GUI and Git Bash directly from the Windows installer —Git GUI:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\background\shell\git_gui]
#="Git &GUI Here"
"Icon"="C:\\Program Files\\Git\\cmd\\git-gui.exe"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\background\shell\git_gui\command]
#="\"C:\\Program Files\\Git\\cmd\\git-gui.exe\" \"--working-dir\" \"%v.\""
Git bash:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\background\shell\git_shell]
#="Git Ba&sh Here"
"Icon"="C:\\Program Files\\Git\\git-bash.exe"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\background\shell\git_shell\command]
#="\"C:\\Program Files\\Git\\git-bash.exe\" \"--cd=%v.\""
For detail about *.reg files, see “How to add, modify, or delete registry subkeys and values by using a .reg file” from Microsoft.
You can install git for windows or Github for windows , both give you the choice while installing to add this feature to your windows explorer. You can find it here:
Github for Windows
https://windows.github.com/
Git for Windows
http://git-scm.com/
As, #Shaswat Rungta said: "I think the question is more about how to add it after the installation is over."
On my PC(Windows 7) I think that the command "Git Bash here" disappeard after I installed Visual Studio 2017.
I fixt this by downloading and installing Git again.
NOTE: "When installing Git for Windows the context menu options are not 'on' by default. You will have to select them during the install." – #nbushnell
(I did this)
What worked for me was almost this, but with the following REGEDIT path:
HKEY_LOCAL_MACHINE/SOFTWARE/Classes/Directory/background/shell and here I created the key Bash, with the value of what I want the display name to be, and then created another key under this named command with the value as the path to git-bash.exe
I'm on Windows 10 and have a fresh git install that didn't add this automatically for some reason (git version 2.12.0 64bit)
Had a similar issue in adding "Start Command Prompt with Ruby" to context menu as it involves passing parameters along with the patch of cmd. Followed a similar procedure as the solution above
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell\Cmd With Ruby]
#="Cmd With Ruby"
"Icon"="C:\\Windows\\System32\\cmd.exe"
[HKEY_CLASSES_ROOT\*\shell\Cmd With Ruby\command]
#="\"C:\\Windows\\System32\\cmd.exe\" \"/E:ON /K
\"C:\\Ruby25-x64\\bin\\setrbvars.cmd\"\" \"--cd=%1\"\""
[HKEY_CLASSES_ROOT\Directory\shell\bash]
#="Cmd With Ruby"
"Icon"="C:\\Windows\\System32\\cmd.exe"
[HKEY_CLASSES_ROOT\Directory\shell\bash\command]
#="\"C:\\Windows\\System32\\cmd.exe\" \"/E:ON /K
\"C:\\Ruby25-x64\\bin\\setrbvars.cmd\"\" \"--cd=%1\"\""
[HKEY_CLASSES_ROOT\Directory\Background\shell\bash]
#="Cmd With Ruby"
"Icon"="C:\\Windows\\System32\\cmd.exe"
[HKEY_CLASSES_ROOT\Directory\Background\shell\bash\command]
#="\"C:\\Windows\\System32\\cmd.exe\" \"/E:ON /K
\"C:\\Ruby25-x64\\bin\\setrbvars.cmd\"\" \"--cd=%v.\"\""
When you install git-scm found in "https://git-scm.com/downloads" uncheck the "Only show new options" located at the very bottom of the installation window
Make sure you check
Windows Explorer integration
Git Bash Here
Git GUI Here
Click Next and you're good to go!
Usually git bash here can be run only on directories so you have to go up a directory and right click on the previous directory then select git bash here (of course on Windows OS).
Note: context menu inside a directory does not have a git bash here option.
Another option but using PowerShell script.
This is basically a PowerShell script which does almost the same as this answer (it also adds icon).
Create file (addOpenGitBashHereContextMenu.ps1), paste in the code below and run it from PowerShell.
You probably need to change exePath to point to your git executable.
Also change contextMenuText if you want to change the text in context menu.
$exePath = $env:USERPROFILE + '\scoop\apps\git\current\git-bash.exe'
$contextMenuText = 'Open Git Bash Here'
$regEntryName = 'git_shell'
$regPathShell = 'HKCU:\SOFTWARE\Classes\Directory\Background\shell'
$regPathContextMenu = $regPathShell + '\' + $regEntryName
$regPathCommand = $regPathContextMenu + '\command'
$user = $env:UserName
echo "Setting '$contextMenuText' context menu..."
echo " User name: '$user'"
echo " Register path: '$regPathCommand'"
echo " Executable path: '$exePath'"
# Key: shell
If (-NOT (Test-Path $regPathShell)) {
New-Item -Path $regPathShell
}
# Key: regEntryName
If (-NOT (Test-Path $regPathContextMenu)) {
New-Item -Path $regPathContextMenu -Value $contextMenuText
# Add Icon to context menu
Set-ItemProperty -Path $regPathContextMenu -Name 'Icon' -Value $exePath
}
# Key: command
If (-NOT (Test-Path $regPathCommand)) {
New-Item -Path $regPathCommand -Value $exePath
}
Try updating your git. For window user you can update your git via cmd by simply writing the command- "git update-git-for-windows".
When done, you can simply right click inside a directory where you want to open the gitbash and check if its showing "Gitbash here" or not. If it's not showing anything, try to look for "Show more options" there and click on it. Now, you may able to see it.
I ran into this problem installing Git-2.38.1-64-bit on a new Windows 10 Pro corporate laptop.
I tried several things ... and considered one of the regedit scripts (the moral equivalent of "Nurse - chainsaw!")... but resolved the problem by simply uninstalling and reinstalling Git (as "Admin").
Specifically:
Control Panel > Programs/Features > Uninstall Git
Reboot Windows
Reinstall Git-2.38.1-64-bit as "Admin"
Carefull check all the options, especially:
"Windows Explorer Integration: Git Bash Here, Git GUI Here
<= These are checked by default ... and I'm sure they were both "Y" on the initial install
I copied my .gitconfig ("C:\Users\USERNAME.gitconfig") from another PC
<= I don't believe this was "essential" ...but I thought I'd mention it...
Browse to a local Git repo and verify the Git context menus work
<= Success after reinstall!
I updated my git and I marked the option of "Git Bash Here"
I have already too many programs that want a spot on my PATH list. Rather than pollute my PATH list, I choose instead to add a symbolic link, from an elevated command prompt, like so.
mklink C:\bin\git-bash.exe "C:\Program Files\Git\git-bash.exe"
While there are exceptions, this works in the majority of cases, including, among others, the 7-zip CLI, selected SysInternals command line tools, the NuGet CLI, and many others.
As a bonus, having the Git Bash command line interface so readily accessible makes activating it from an open command prompt window trivial. With this improvement, I can probably live without yet another item on my context menu for directories.
In addition to the answers with the theory and the minimal examples see the example of the .reg file for Git (and not only, with the instruction) with user-friendly names, shortcut keys (& in a user-friendly name) and icons in the "win-registry-snippets" repository
Do Nothing.
Just open your git bash terminal and type in:
git update-git-for-windows
Once update is completed, this will automatically add "Git Bash here" into your context menu.
Refer this: https://stackoverflow.com/a/65467670
You can install TortoiseGit for Windows and include integration in context menu.
I consider it the best tool to work with Git on Windows.
Add the gitpath to the Environment-path variable (e.g. C:\Program Files\Git\cmd) by which you can access git from any folder using command line.
Use Shift + F10 to open the menu of options which appear when right clicked, and then click s to git bash here.

CMD Command for "Git Bash Here"

In Windows I can run "Git Bash Here" in the current directory by opening right click menu.
I want to place gitbashhere.bat in default path for commands to have got easy access from standard cmd window.
The sh.exe --login -i command only opens git bash in sh.exe's directory instead of the current directory.
How do I check what command is bound to "Git Bash Here"?
How do I check what command is bound to "Git Bash Here"?
Open the Registry Editor using regedt32 or regedit.
Menu "Edit" > "Find" > "Find what" and enter "git_shell"
Examine the returned Registry entries.
One will be:
HKEY_CLASSES_ROOT\Directory\shell\git_shell\command
With the value:
"C:\Windows\SysWOW64\cmd.exe" /c "pushd "%1" && "C:\git_install_dir\bin\sh.exe" --login -i"
where git_install_dir is the directory where you have installed git.
You can see it runs something similar to the following command:
pushd "%1" && "C:\git_install_dir\bin\sh.exe" --login -i
Another way to see how a specific program was started is to launch Process Explorer, look for the process in the list of processes (for git this would be "git-bash.exe") and double-click on the process to open the properties.
On the 'Image' tab there's a text field that yields the command line of the process:
That is a windows shell thing. It depends on the git GUI toolkit that you've installed. For tortoiseGit, there's a setting hidden somewhere in the configuration menu. If that doesn't suit you: It's free and open software, so fix that :)

How do I launch a Git Bash window with particular working directory using a script?

How can I launch a new Git Bash window with a specified working directory using a script (either Bash or Windows batch)?
My goal is to launch multiple Git Bash windows from a single script, each set to a different working directory. This way I can quickly get to work after booting the computer instead of having to open Git Bash windows and navigating each one to the correct working directory.
I am not asking how to change the default working directory, like this question does, but to launch one or more terminal windows with different working directories from a script.
Another option is to create a shortcut with the following properties:
Target should be:
"%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login
Start in is the folder you wish your Git Bash prompt to launch into.
Try the --cd= option. Assuming your GIT Bash resides in C:\Program Files\Git it would be:
"C:\Program Files\Git\git-bash.exe" --cd="e:\SomeFolder"
If used inside registry key, folder parameter can be provided with %1:
"C:\Program Files\Git\git-bash.exe" --cd="%1"
Git Bash uses cmd.exe for its terminal plus extentions from MSYS/MinGW which are provided by sh.exe, a sort of cmd.exe wrapper. In Windows you launch a new terminal using the start command.
Thus a shell script which launches a new Git Bash terminal with a specific working directory is:
(cd C:/path/to/dir1 && start sh --login) &
(cd D:/path/to/dir2 && start sh --login) &
An equivalent Windows batch script is:
C:
cd \path\to\dir1
start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login
D:
cd \path\to\dir2
start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login
To get the same font and window size as the Git Bash launched from the start menu, it is easiest to copy the start menu shortcut settings to the command console defaults (to change defaults, open cmd.exe, left-click the upper left icon, and select Defaults).
Let yet add up to the answer from #Drew Noakes:
Target:
"C:\Program Files\Git\git-bash.exe" --cd=C:\GitRepo
The cd param should be one of the options how to specify the working directory.
Also notice, that I have not any --login param there: Instead, I use another extra app, dedicated just for SSH keys: Pageant (PuTTY authentication agent).
Start in:
C:\GitRepo
The same possible way, as #Drew Noakes mentioned/shown here sooner, I use it too.
Shortcut key:
Ctrl + Alt + B
Such shortcuts are another less known feature in Windows. But there is a restriction: To let the shortcut take effect, it must be placed somewhere on the User's subdirectory: The Desktop is fine.
If you do not want it visible, yet still activatable, place this .lnk file i.e. to the quick launch folder, as that dir is purposed for such shortcuts. (no matter whether displayed on the desktop) #76080 #3619355
"\Application Data\Microsoft\Internet Explorer\Quick Launch\"
In addition, Win10 gives you an option to open git bash from your working directory by right-clicking on your folder and selecting GitBash here.
Windows 10
This is basically #lengxuehx's answer, but updated for Win 10, and it assumes your bash installation is from Git Bash for Windows from git's official downloads.
cmd /c (start /b "%cd%" "C:\Program Files\GitW\git-bash.exe") && exit
I ended up using this after I lost my context-menu items for Git Bash as my command to run from the registry settings. In case you're curious about that, I did this:
Create a new key called Bash in the shell key at HKEY_CLASSES_ROOT\Directory\Background\shell
Add a string value to Icon (not a new key!) that is the full path to your git-bash.exe, including the git-bash.exe part. You might need to wrap this in quotes.
Edit the default value of Bash to the text you want to use in the context menu
Add a sub-key to Bash called command
Modify command's default value to cmd /c (start /b "%cd%" "C:\Program Files\GitW\git-bash.exe") && exit
Then you should be able to close the registry and start using Git Bash from anywhere that's a real directory. For example, This PC is not a real directory.
This is the command which can be executed directly in Run dialog box (shortcut is win+R) and also works well saved as a .bat script:
cmd /c (start /d "/path/to/dir" bash --login) && exit
I'm not familiar with Git Bash but assuming that it is a git shell (such as git-sh) residing in /path/to/my/gitshell and your favorite terminal program is called `myterm' you can script the following:
(cd dir1; myterm -e /path/to/my/gitshell) &
(cd dir2; myterm -e /path/to/my/gitshell) &
...
Note that the parameter -e for execution may be named differently with your favorite terminal program.
Using Windows Explorer, navigate to any directory you want, type "cmd" in the address bar it will open Windows command prompt in that directory.
Along the same lines, if you have the git directory in your path, you can type "git-bash" in the address bar and a Git Shell will open in that directory.
If using Windows OS :
Right click on git terminal > Properties
Properties>Under shortcut tab>Start in:
add your folder target path like below image

Resources