Windows - start git in given directory - windows

I'm using git portable on windows. It's rather user friendly, but there is one thing that bothers me. Every time I run it I have to type the entire path to the project directory, which is quite long sometimes. Maybe it's not a serious problem, but it would be very nice to shorten it. I tried the following:
bash script.sh // cd in this file // nothing happens
create symbolic link it - it just copies the directory
create windows shortcut - can't open it within git console
Anybody managed to solve this?

You can right click on the Windows shortcut that launches Git Bash, edit its properties and modify the "Start in" path to your project path. Every time you launch this shortcut, it will cd into that project path.
Or you can add an alias to your ~/.bashrc like below:
alias proj="cd /path/to/project/"
This will allow you to cd into the project dir on demand by typing the alias name at the prompt.

If you run git-bash.bat from Portable git, it should work mostly as normal git installation. So, to work with a specific repository, just cd into it:
cd /c/code/MyRepo/
git whatever

I've added
cd /c/dev
to
~/.bashrc
with
echo cd /c/dev >> ~/.bashrc
But I'm running MSysGit the non-portable version. Hope this helps in some way anyway.

Related

Change directory where git bash launches without shortcut or modifying $HOME

The company I work for has a code repository deep within a folder structure, the path of which I have saved in a text file. As I was copying that path from notepad++ and pasting it into a cd command for the hundredth time after launching git bash I began to get the feeling that I was wasting time. Being the good programmer that I am, spending an hour researching how to do something programmatically is obviously preferable to doing something that takes four seconds over and over again. However I have been disappointed to find that all of the readily-available solutions offer one of two recommendations which both feel more like workarounds than an actual config change:
Create a shortcut and modify the "start in" field. This is not acceptable for me because I launch git from the start menu (windows key + type "git"). I don't want to add the extra steps of navigating to the folder where I created the shortcut. Even if I give the shortcut a unique name such as "stupidshortcutname", I am not able to access this shortcut by searching in the windows start menu.
Modify the Windows environment variable HOME. This is not acceptable because I still want cmd to launch in its default location of C:\Users\MyUserNameHere.
With some further research I also found the option of creating a file called .bashrc in my C:\Users\MyUserNameHere directory, and having this run cd <folder I want to go to>, but this still feels pretty hacky for something that I feel should be a configuration somewhere.
Git has its own config files, I would be shocked if none of them allowed for setting the location git will default to launching in. Am I out of luck and/or being too picky?
Create a batch file with name like LaunchGit.bat & write the following command:
start F:\Program" "Files\Git\git-bash.exe --cd=D:\your\deep\workspace\path
Now you can launch it from desktop or place it in your Git home directory (and launch from there)
This may not be the most perfect solution, but it will solve your problem.
And ofCouse this is just an idea and you can change/improve it.
Use git-bash configuration to change the directory, if this is what you want.
In ~/.bash_profile (create it if it does not exist), add
cd /path/to/git/repository
The reason to use ~/.bash_profile instead of ~/.bashrc is because the latter is read and executed when an interactive shell that is not a login shell is started (if exists).

problems with git bash on windows. i can't access folders on git bash for windows

i bought a course from codecademy and at beginner was a lesson about git bash. I install it , but if i want to change directories with "cd" i cant't.
enter image description here
Also , when i double-click on shortcut the program doesn't run.
Try changing to you home directory first
$ cd
then
$ pwd
/c/Users/yourname
Try going to you root directory with : cd / (make sure you add a space between cd and /)
Then do cd yourfilename/yourfilename....you can add multiple directories
Make sure your spelling is correct
Few other tips :
To navigate to your home directory use "cd ~".
To navigate to the previous directory use "cd -"

Why are files created in git bash not visible in Windows explorer?

So I just moved from Ubuntu to Windows (read forced to move, thanks to compatibility issues) and am using Git Bash to pull in my files. I've noticed something extremely strange.
Git Bash is installed in C:\Program Files (x86)\Git\, and when I first run the Git Bash program, the default location is this. Normal right? Right.
But here's the weird part..
When I run the command mkdir srv in the Git Bash command line, I can see it in the Bash window - but I can't see it in Windows Explorer?! What the heck. Same thing happens with files created using vi in the CLI. Invisible in Windows Explorer. (Yes, I have enabled the Show hidden files option in Explorer, so it's not that).
Here are some snapshots (no, I'm not blind - nor am I insane). Help?
You might not be able to see the folder because of missing privileges. Try running your explorer as Administrator and look again.
The proposed solutions above didn't help me. However, I copied the hidden directories to another place via git bash. Then I copied them once again via windows explorer to the original place. Now they've become visible. I created those invisible folders via git bash with git clone command before.
I had similar issue, but with file attributes, not with missing privileges.
In cmd under C:\Program Files (x86)\Git\ run command attrib
attrib *
You will know if srv folder has System or Hidden file attribute.
If you would like to view it in cmd then use dir /a:s

How to default to other directory instead of home directory

I am developing on a windows machine. The only place I need for linux command line is Git Bash. The problem is: When I open it, I am in the home directory. I have to change the directory to my workspace, like:
cd ../../../d/work_space_for_my_company/project/code_source
Can I wrap this in a .sh file so I don't have to hand-type it anymore? This should be simple but I have zero knowledge about Linux command line. I am really appreciated If you can walk me
through how to create that .sh file.
Here's a more Windows-ish solution:
Right click on the Windows shortcut that you use to launch git bash, and click Properties. Change the value of "Start In" to your desired workspace path.
Edit: Also check that the Target value does not include the --cd-to-home option as noted in the comments below.
Just write that line to a file "cd.sh", then do this from your shell prompt:
. ./cd.sh
Or you can create an alias or function in your $HOME/.bashrc file:
foo() { cd /d/work_space_for_my_company/project/code_source ; }
If the directory name includes spaces or other shell metacharacters, you'll need quotation marks; it won't hurt to add them even if they're not necessary:
foo() { cd "/d/Work Space/project/code_source" ; }
(Note that I've omitted the ../../..; you don't need it.)
EDIT: If you add a line
foo
to your .bashrc after the function definition, your shell will start in that directory. Or you can just use the cd command directly in your .bashrc if you aren't going to need to use the function later.
(The name foo is just an example; you should pick a more meaningful name.)
Add the line to the .bashrc file in the home directory (create the file if it doesn't exist):
cd ~
touch .bashrc
echo "cd ~/Desktop/repos/" >> .bashrc
I use ConEmu (strongly recommended on Windows) where I have a task for starting Git Bash like
Note the button "Startup dir..." in the bottom. It adds a -new_console:d:<path> to the startup command of the Git Bash. Make it point to wherever you like
This may help you.
Right click on git bash -> properties
In Shorcut tab -> Start in field -> enter your user defined path
Make sure the Target field does not include --go-to-home or it will continue to start in the directory specified in your HOME variable
Thats it.
I also just changed the "Start in" setting of the shortcut icon to: %HOMEDRIVE%/xampp/htdocs/
(Please read warning below)
Really simple way to do this in Windows (works with git bash, possibly others) is to create an environmental variable called HOME that points to your desired home directory.
Right click on my computer, and choose properties
Choose advanced system settings (location varies by Windows version)
Within system properties, choose the advanced tab
On the advanced tab, choose Environmental Variables (bottom button)
Under "system variable" check to see if you already have a variable called HOME. If so, edit that variable by highlighting the variable name and clicking edit. Make the new variable name the desired path.
If HOME does not already exist, click "new" under system variables and create a new variable called HOME whose value is desired path.
NOTE: This may change the way other things work. For example, for me it changes where my .ssh config files live. In my case, I wanted my home to be U:\, because that's my main place that I put project work and application settings (i.e. it really is my "home" directory).
EDIT June 23, 2017: This answer continues to get occasional upvotes, and I want to warn people that although this may "work", I agree with #AnthonyRaymond that it's not recommended. This is more of a temporary fix or a fix if you don't care if other things break. Changing your home won't cause active damage (like deleting your hard drive) but it's likely to cause insidious annoyances later. When you do start to have annoying problems down the road, you probably won't remember this change... so you're likely to be scratching your head later on!
This will do it assuming you want this to happen each time you open the command line:
echo cd ../../../d/work_space_for_my_company/project/code_source >> ~/.bashrc
Now when you open the shell it will move up three directories from home and change to code_source.
This code simply appends the line "cd ../../../d/work_space_for_my_company/project/code_source" to a file named ".bashrc". The ">>" creates a file if it does not exist and then appends. The .bashrc file is useful for running commands at start-up/log-in time (i.e. loading modules etc.)
Right-click the Git Bash application link go to Properties and modify the Start in location to be the location you want it to start from.
From a Pinned Start Menu Item in Windows 10
Open the file location of the pinned shortcut
Open the shortcut properties
Remove --cd-to-home arg
Update Start in path
Re-pin to start menu via recently added
Thanks to all the other answers for how to do this! Wanted to provide Win 10 instructions...
For windows: Follow these steps-
Go to windows home> Right click on "Git Bash" application.
Properties> Shortcut
Change these two settings:
(a) Delete --cd-to-home from target
(b) Type folder path you want to start with git in "Start in".
This worked for me:)
If you want to have projects choice list when u open GIT bash:
edit ppath in code header to your git projects path, put this code into .bashrc file and copy it into your $HOME dir (in Win Vista / 7 it is usually c:\Users\$YOU)
#!/bin/bash
ppath="/d/-projects/-github"
cd $ppath
unset PROJECTS
PROJECTS+=(".")
i=0
echo
echo -e "projects:\n-------------"
for f in *
do
if [ -d "$f" ]
then
PROJECTS+=("$f")
echo -e $((++i)) "- \e[1m$f\e[0m"
fi
done
if [ ${#PROJECTS[#]} -gt 1 ]
then
echo -ne "\nchoose project: "
read proj
case "$proj" in
[0-`expr ${#PROJECTS[#]} - 1`]) cd "${PROJECTS[proj]}" ;;
*) echo " wrong choice" ;;
esac
else
echo "there is no projects"
fi
unset PROJECTS
you may want set this file as executable inside GIT bash chmod +x .bashrc (but its probably redundant, since this file is stored on ntfs filesystem)
My Git Bash shortcut on Windows complained when I put the cd to my work directory into ~/.bashrc
WARNING: Found ~/.bashrc but no ~/.bash_profile, ~/.bash_login or ~/.profile.
This looks like an incorrect setup.
A ~/.bash_profile that loads ~/.bashrc will be created for you.
So git created this .bash_profile:
$ cat ~/.bash_profile
# generated by Git for Windows
test -f ~/.profile && . ~/.profile
test -f ~/.bashrc && . ~/.bashrc
Which does the job.
Alternatively, you can just remove the .bashrc again and put the cd command into .bash_profile:
$ rm ~/.bashrc
$ echo "cd Source/Repos" >~/.bash_profile
$ cat ~/.bash_profile
cd Source/Repos
Once this is done you can close the Window and re-open it using your desktop shortcut and the prompt will tell you that your location is now where you wanted it - looks like this is my case:
Administrator#raptor1 MINGW64 ~/Source/Repos
$
If you type this command:
echo cd d:/some/path >> ~/.bashrc
Appends the line cd d:/some/path to .bashrc. The >> creates a file if it doesn’t exist and then appends.
it must be cd d:/work_space_for_....
without the : it doesn't work for me
Another solution for Windows users will be to copy the Git Bash.lnk file to the directory you need to start from and launch it from there.

Bookmark Directories In Terminal

Looking for a solution to quickly navigate to long paths in a shell (particularly Max OS X Terminal.app).
Say my path is ~/This/Is/A/Really/Long/Path/That/I/Would/Rather/Not/Type/Frequently
Instead of cd ~/This/Is/A/....
I would like to be able to store favorites/bookmark directories so I could do "cd myPath"
Are there any binaries or tools available to do something like this?
I've found the packages 'Apparix' and 'Goto' which together make the stuff dreams are made of for us terminal junkies.
Naturally, I had trouble installing Apparix, but I figured it out in the end.
How To Install Apparix on Mac OS X:
Download the tarball from Apparix's homepage.
Unpack the tarball, cd to the unpacked folder.
Run this command ./configure --prefix=$HOME/local && make && make install.
Run man apparix, scroll down to the heading BASH-style functions, copy everything within that section (delimited with ---) and paste it into ~/.bash_profile.
That's it. You should now have Apparix up and running on OS X (further install info and usage is on Apparix's homepage).
Another solution is to use Bashmarks, which allows you to this
$ cd ~/This/Is/A/Really/Long/Path/That/I/Would/Rather/Not/Type/Frequently
$ s shortname # save current path as `shortname`
$ cd /
$ g shortname # cd to ~/This/Is/A/Really/Long/Path/That/I/Would/Rather/Not/Type/Frequently
You can use aliases (stick them in your ~/.bash_profile if you want them to always load)
alias cd_bmark1='cd ~/This/Is/A/Really/Long/Path/That/I/Would/Rather/Not/Type/Frequently'
Then use by just typing
cd_bmark1
into the console
I know you already found an answer that worked for you, but a couple of more lightweight suggestions that might help others looking for similar things
If your directories are relatively fixed, just long and far away from each other, you can use the CDPATH environment variable to add directories to the search path when typing the "cd" command. If the directory name you try to cd to isn't in the current directory, the other entries in your CD path will also be looked at (and it's also tab complete aware, at least in bash and zsh).
Switching to zsh rather than bash and using the excellent directory stacks abilities. With it, you can maintain a history of directories that you've visited, view the history with the "dh" alias, and easily switch to a directory by using quick shortcuts (ex: cd -3 to switch to the 3rd directory in your history stack).
Why not having a symlink ?
ln -s ~/This/Is/A/Really/Long/Path/That/I/Would/Rather/Not/Type/Frequently bmark
cd bmark
I use to.sh daily to create and navigate bookmarked paths in bash. It supports tag autocompletion and the ability to easily add/remove bookmarks.
https://github.com/Grafluxe/to.sh
Full disclosure, I wrote this tool :)

Resources