--git-dir changes directory in .bashrc in WSL - bash

I created a script that prints out all my git repo libraries with the current branch names. But for some reason, it changes the current directory at the end.
I have a ".bash_aliases" script that I call from .bashrc, so:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
The .bash_aliases file contains some aliases and variables, such as these:
repo1Dir=~/repo1/
repo2Dir=~/repo2/
repo3Dir=~/repo3/
alias repo1="cd $repo1Dir"
alias repo2="cd $repo2Dir"
alias repo3="cd $repo3Dir"
And I added this script at the end of the file:
echo -n "repo1 -> "
git --git-dir $repo1Dir".git" --work-tree=$repo1Dir rev-parse --abbrev-ref HEAD
echo -n "repo2 -> "
git --git-dir $repo2Dir".git" --work-tree=$repo2Dir rev-parse --abbrev-ref HEAD
echo -n "repo3 -> "
git --git-dir $repo3Dir".git" --work-tree=$repo3Dir rev-parse --abbrev-ref HEAD
When I start a WSL terminal, it will print this at initialization:
repo1 -> branch_abc
repo2 -> branch_cba
repo3 -> main
I expect that the current folder will end up being HOME because I didn't change that before and as far as I know the command --git-dir doesn't change the current dir. But in the end, the current folder will be this one:
myComputer#DESKTOP-PC:~/repo3$
So the folder of the last command. If I type one of these git commands in the terminal, the current folder will not change.
This becomes a problem if I want to work with repo2 for example. I type this:
repo2
and the current folder will be this of course: myComputer#DESKTOP-PC:~/repo2$. Then I start vscode with the code . command. The vscode opens repo2. The vscode's terminal will of course print the branches (which are in .bash_aliases) and then this will be the current directory: myComputer#DESKTOP-PC:~/repo3$
Why does it change the directory and how can I prevent it?
Thanks in advance for any answers!

Related

A alias remove local brach and create with same name again

I need to a alias remove local branch and regenerate with same name again. for example;
grb test-branch
git regenerate branch
will make this automatically
git branch -D test-branch && git checkout -b test-branch
how can I do this ?
is there any default shortcuts comes with vanilla git ?
You can add the following function to your .bashrc or .zshrc file;
# git branch regenerate
function gbrr () {
git branch -D "$#" && git checkout -b "$#"
}
And you can use it like below
gbrr feature/navbar

Is there a way to add multi steps to a git alias?

I'm going to create an alias that does this:
I've added it to my git-bash .bash_profile but I'd like to see if there is a way to add it as an alias so I don't have to use git bash
Git finish will push to current branch # Eg. gf "commit message"
gf() {
CURRENT_BRANCH="$(git rev-parse --abbrev-ref HEAD)"
git add . && git commit -m "$1" && git push origin "$CURRENT_BRANCH"
} # Git merge # Eg. gm branch-name
gm() {
git merge "$1"
} # Git checkout # Eg. gc branch-name
gc(){
git checkout "$1" && gp
}
You could create a "Git-Subcommand" the name of this executable must be in this Format: git-{{ Name }} eg: git-acommit. This File must be located in one the Directories listed in $PATH, you can now execute git acommit and git will search for it in you $PATH.
To use this than in an alias simply run git config --global alias.gf acommit and you are finished. Please note: the alias step is unnecessary since you can name your file how you want, so you could also name it git-gf instead of git-acommit.
But without creating a separated file it's impossible to stack commands in a git alias.
Or you could use GitAlias to create an alias that executes a function or multiple Commands.
For an example check out the Recovery examples Section.

Setting a sh file to git bash

I've created a Python "automation" task that will initialize a repository on github and then on my bash function I'll be creating a folder, initialize the repository as the code bellow shows:
#!/usr/bin/env sh
function repo() {
cd
python C:/Users/wsm/PycharmProjects/GitAutomation/create.py $1
cd C:/Users/wsm/$1
start .
git init
git remote add origin <mygithublink>/$1.git
touch README.md
git add .
git commit -m "Initial commit"
git push -u origin master
code .
}
The only problem is that I have to use source command everytime I close a git bash to enable the command repo (Name of the function) again. Any ideas on how to make that command permanent on Windows 10?

howto find out which git submodule current directory belongs to

setup
i have a git repo located in /home/v/git_repo, in which i have a submodule localted in subdirectory ./a/b/c.
$ cat /home/v/git_repo/.gitmodules
[submodule "foo/bar"]
path = a/b/c
url = git#github.com:username/repo.git
having the full path or only the in-repository subpath (that i have implemented in helper script git-where-in-repo-am-i-currently)
$ pwd
/home/v/git_repo/a/b/c/something
$ git where-in-repo-am-i-currently
a/b/c/something
question
i want to find out (preferably in fish) which submodule this path belongs to: e.g
$ git which-submodule (pwd)
foo/bar
to later use it to query that submodules status like
$ git -C (git rev-parse --git-dir)/modules/(git which-submodule) status
on branch master
Your branch is up to date with 'origin/master'
and ultimately display this information in my prompt (that part is already implemented)
what i tried
parsing the output of
$ git -C (git rev-parse --show-toplevel) config --file=.gitmodules --get-regexp "path"`
submodule.foo/bar.path a/b/c
and comparing my sub-directory path to that of a submodule, but it was rather a mess, with splitting pathes into arrays and all kinds of hacks
For the usual setup you've described here, with the worktree nesting matching the submodule nesting, you can
mytoplevel=`git rev-parse --show-toplevel`
abovethat=`git -C "$mytoplevel"/.. rev-parse --show-toplevel`
Then,
echo ${mytoplevel#$abovethat/}
will get you the submodule path in the superproject, or you can
echo ${PWD#$abovethat/}
to get your current directory's path relative to the superproject.
So:
me=`git rev-parse --show-toplevel`
up=`git -C "$me"/.. rev-parse --show-toplevel`
subpath=${me#$up/}
git -C "$up" config -f .gitmodules --get-regexp '^submodule\..*\.path$' ^$subpath$
gets you the current repo's submodule name and path from the config entry in its superproject.
Git can be useful in any imaginable build system, though; it doesn't impose restrictions on how things outside its remit are set up. So short of an exhaustive search of the filesystem namespace you can't be sure you've found everybody using any worktree as a submodule checkout, there's just no reason for Git to care how a repository is used.
For instance, if multiple projects all need to run off the same submodule rev, you can have a single repo and worktree serve as a shared submodule for them all: rather than have to go through every single one of them and do synchronized checkouts, and then trusting that you haven't missed one, just use one repo, with one worktree, and point everybody using it at that.
For workflows with that need, this can be compellingly better than the usual setup, all users by definition see a synchronized, current submodule revision and any client who needs to know "what's new" with an update can e.g. git -C utils diff `git rev-parse :utils` HEAD, every submodule user effectively has their own tracking branch and can use all of Git's tools to help stay current or resolve conflicts.
So, to recreate your setup, I do:
git init git_repo; cd $_
mkdir a/b; git init a/b/c; cd $_
mkdir something; touch something/somefile;
git add .; git commit -m-
cd `git -C .. rev-parse --show-toplevel`
git submodule add --name foo/bar ./a/b/c -- a/b/c
git add .; git commit -m-
Then I get this when I try it:
$ find -print -name .git -prune
.
./a
./a/b
./a/b/c
./a/b/c/something
./a/b/c/something/somefile
./a/b/c/.git
./.gitmodules
./.git
$ git grl
core.repositoryformatversion 0
core.filemode true
core.bare false
core.logallrefupdates true
submodule.foo/bar.url /home/jthill/src/snips/git_repo/a/b/c
submodule.foo/bar.active true
$ cd a/b/c/something
$ me=`git rev-parse --show-toplevel`
$ up=`git -C "$me"/.. rev-parse --show-toplevel`
$ subpath=${me#$up/}
$ git -C "$up" config -f .gitmodules --get-regexp '^submodule\..*\.path$' ^$subpath$
submodule.foo/bar.path a/b/c
$ echo $me $up $subpath
/home/jthill/src/snips/git_repo/a/b/c /home/jthill/src/snips/git_repo a/b/c
If there's a difference between this setup and what you've described, I'm missing it, I've got the directory structure, the submodule name, the start directory... if you'll step through that and find where the setup or results diverge from yours I think that'd help.

Bash Shell Script Process Each Directory in Home

I am using Git Bash, and I would like to write a script that processes the same set of commands for each directory (local repo) in my home directory. This would be easy enough in DOS, which most consider as handicapped at best, so I'm sure there's a way to do it in bash.
For example, some pseudo-code:
ls --directories-in-this-folder -> $repo_list
for each $folder in $repo_list do {
...my commmand set for each repo...
}
Does anyone know how to do this in Bash?
You can do that in bash (even on Windows, if you name your script git-xxx anywhere in your %PATH%)
#! /bin/bash
cd /your/git/repos/dir
for folder in $(ls -1); do
cd /your/git/repos/dir/$folder
# your git command
done
As mentioned in "Git Status Across Multiple Repositories on a Mac", you don't even have to cd into the git repo folder in order to execute a git command:
#! /bin/bash
cd /your/git/repos/dir
for folder in $(ls -1); do
worktree=/your/git/repos/dir/$folder
gitdir=$worktree/.git # for non-bare repos
# your git command
git --git-dir=$gitdir --work-tree=$worktree ...
done

Resources