Skip to content

Instantly share code, notes, and snippets.

@r-a-y
Last active May 29, 2024 12:58
Show Gist options
  • Save r-a-y/da6c3b1b99aafcb3e97e311280aa9434 to your computer and use it in GitHub Desktop.
Save r-a-y/da6c3b1b99aafcb3e97e311280aa9434 to your computer and use it in GitHub Desktop.
Using Git for Windows (PortableGit) with a portable HOME directory

Prologue

I develop on Windows (yeah, I hear your jeers, linux users!), so to use Git, I use Git for Windows

However, I use Git For Windows (portable version) so I can keep my dev environment centrally located. This is so I can reuse this environment simply by copying my msysgit directory to a USB drive.

Installation

(1) Download the latest version of PortableGit:

https://github.com/git-for-windows/git/releases/

You'll want the latest 7z file prefixed with PortableGit in the file name.

Extract the 7z file to a directory of your choosing. For this article, I'll be using c:\PortableGit\ as a reference point.

(2) Setting up your bash environment to use a custom HOME directory

The last step is loading bash and telling it to use a custom HOME directory.

  1. Create a new directory called home\portable\ in the folder that you extracted PortableGit to. eg. c:\PortableGit\home\portable

  2. Next, create a .bat in the main PortableGit folder to launch bash to use our custom HOME directory.

In the following code block, we'll be using Mintty as our terminal to launch bash since it comes with PortableGit.

The most important part in the .bat file is setting our HOME environment variable to use our custom HOME directory.

Copy the accompanying mintty.bat file to c:\PortableGit\mintty.bat.

  1. Launch Mintty by double-clicking on mintty.bat.

The above example uses Mintty, but you could easily modify the .bat file to use some other terminal like ConEmu.

set HOME=%~dsp0home/portable
set "MSYSTEM=MINGW64"
start %~dsp0usr/bin/mintty /bin/bash --login -i
@r-a-y
Copy link
Author

r-a-y commented Jan 16, 2020

@roro4ever - I finally duplicated your bug as I was trying out Mintty for the first time in awhile. Should be fixed when you update mintty.bat to the newest revision.

@rakesh1988
Copy link

@r-a-y I am now having an issue after updating mintty.bat
image

@r-a-y
Copy link
Author

r-a-y commented Jan 16, 2020

@rakesh1988 - Your Mintty.bat should exist in your PortableGit root. In the example, I stated c:\PortableGit.

For you, you would place Mintty.bat here - c:\users\XXX\DOWNLO~1\git\

@BIMartingo
Copy link

Worked fine. Thanks!

@BIMartingo
Copy link

to use the Git-Bash.exe Git-cmd.exe and the Git-Gui is still an option?

@r-a-y
Copy link
Author

r-a-y commented Aug 7, 2020

@BIMartingo - You can use the native git-bash.exe and git-cmd.exe as well, but some prefer better terminal emulators such as Mintty or ConEmu like myself.

@ZaroyRedrum
Copy link

I'm using PortableApps on an USB drive, I've download the portable.sh (moved it to i:\portablegit\etc\profile.d), but I don't natively use mintty. I'd like to use ConEmu instead. How do I modify mintty.bat ?

@ronaldjonnes
Copy link

ronaldjonnes commented Mar 6, 2021

(2) Ensure PortableGit uses our custom .bashrc

Where can I find your custom file?

Thanks.

@r-a-y
Copy link
Author

r-a-y commented Mar 6, 2021

@ronaldjonnes - See https://gist.github.com/r-a-y/da6c3b1b99aafcb3e97e311280aa9434#gistcomment-3130225. I've just updated the readme to hopefully make things a little clearer.

@ronaldjonnes
Copy link

Thank you.

Before submitting my comment, I read that comment and created the empty file; however, you refer to your custom .bashrc file.

@r-a-y
Copy link
Author

r-a-y commented Mar 6, 2021

It's custom in the sense that it's not the proper .bashrc file Git is expecting, but I get how that can cause confusion. I've updated the readme once more.

@Amirhmohammady
Copy link

1- open D:\PortableApps\Git-2.31.1PortableByAmir\etc\profile bye notepad

2- add this to profile file:

HOME="/PortableHome"

3- create PortableHome folder in D:\PortableApps\Git-2.31.1PortableByAmir\

4- copy

C:\Users\Amir.gitconfig
C:\Users\Amir.git-credentials

to

D:\PortableApps\Git-2.31.1PortableByAmir\PortableHome

Note: D:\PortableApps\Git-2.31.1PortableByAmir\bin\bash.exe is not a portable git only open D:\PortableApps\Git-2.31.1PortableByAmir\git-bash.exe

@r-a-y
Copy link
Author

r-a-y commented Apr 10, 2021

1- open D:\PortableApps\Git-2.31.1PortableByAmir\etc\profile bye notepad

2- add this to profile file:

HOME="/PortableHome"

@Amirhmohammady - The problem with this approach is it requires you to modify /etc/profile every time you upgrade PortableGit. My goal was not to modify anything from PortableGit.

@RickBankers
Copy link

RickBankers commented May 25, 2021

I also keep my development environment portable as well. I'd suggest taking a look at CMDER which is a console emulator. Simply download the zip package and extract it and launch cmder.exe. GIT, OpenSSL, SSH, etc. are all included and are easily updated in a single portable environment as well as all you configs, etc. I've been using CMDER for years and it's fantastic! https://github.com/cmderdev/cmder

@pinuke
Copy link

pinuke commented Aug 15, 2022

So the current portable git packages are 7z SFX packages (.7z.exe) and not 7z archives (.7z).

However, you can run the install silently from commandline. 7z SFX executables support command line switches

According to the docs, -o PATH\PATH\PATH\PATH sets the path, -y runs the extractor without prompt, -gm2 completely hides the extraction.

Personally, I prefer to use portablegit.7z.exe -o "out\folder" -y so I can see the extraction progress bar

EDIT: so 7z SFX has some weird jargon about command line parameters. Apparently, most SFX configuration file parameters can be passed as command line switches. Exceptions:

AutoInstall
AutoInstallX
Delete
DeleteX
RunProgram
Shortcut
ShortcutX

@prince-wayne
Copy link

will the version control work without doing all of this? on my home computer, I only needed the config to be correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment