Execute a command from another directory in bash
This is often the best way:
( cd dir ; git init )
or
( cd dir && git init )
It's pretty short and easy to type. It does start a sub-shell, so you can't modify your environment from that, but that doesn't seem to be an issue here.
I was looking for a way to execute the git command from a path, and make changes to the repository in a different path. So I ended up in this question here.
But for my specific needs neither the accepted answer nor any of the other ones helped.
I needed to run git commands using sudo -u USER /usr/bin/git
(another user running it). And as you may know, sudo doesn't allow me to run the cd
command, so I can't be in the repository directory.
So, I went to git's man page. And among the options, I saw the --git-dir=<path>
:
--git-dir=
Set the path to the repository. This can also be controlled by setting the GIT_DIR environment variable. It can be an absolute path or relative path to current working directory.
So, if it help someone, you can still use git from a path and make changes to a repository "far from you". Just use:
git --git-dir=/path/to/repository GIT_COMMAND
or, to run it as another user, do something like:
echo USER_PASSWORD | sudo -u USER_LOGIN -S /usr/bin/git --git-dir=/path/to/repository GIT_COMMAND
Also from git-init's man page:
If the $GIT_DIR environment variable is set then it specifies a path to use instead of ./.git for the base of the repository.
So, if you want to init the repository under the usual .git folder, you will need to specify it together with the --git-dir
option. e.g.:
echo USER_PASSWORD | sudo -u USER_LOGIN -S /usr/bin/git --git-dir=/path/to/repository/.git init
After initializing the repository on /path/to/repo/.git
, all further commands should have the option --work-tree=<path>
, as described on git's man page:
--work-tree=
Set the path to the working tree. It can be an absolute path or a path relative to the current working directory. This can also be controlled by setting the GIT_WORK_TREE environment variable and the core.worktree configuration variable (see core.worktree in git-config(1) for a more detailed discussion).
So, the right command to run git as another user, and initialize a new repository is:
echo USER_PASSWORD | sudo -u USER_LOGIN -S /usr/bin/git --git-dir=/path/to/repository/.git init
echo USER_PASSWORD | sudo -u USER_LOGIN -S /usr/bin/git --git-dir='/path/to/repository/.git' --work-tree='/path/to/repository' add /path/to/repository/*
echo USER_PASSWORD | sudo -u USER_LOGIN -S /usr/bin/git --git-dir='/path/to/repository/.git' --work-tree='/path/to/repository' commit -m 'MESSAGE'
echo USER_PASSWORD | sudo -u USER_LOGIN -S /usr/bin/git --git-dir='/path/to/repository/.git' --work-tree='/path/to/repository' remote add origin [email protected]:path
echo USER_PASSWORD | sudo -u USER_LOGIN -S /usr/bin/git --git-dir='/path/to/repository/.git' --work-tree='/path/to/repository' push -u origin master
Not exactly what you're asking (you have real answers above with the subshell) but look at pushd
and popd