212

Say that I'm doing this:

cd subdir
git init
cd ../

Is there a way to do this with a single command, or perhaps two, rather than having to move in and out of a directory in order to run a command there?

(Not looking for a git-specific solution; that's just an example.)

8 Answers8

348

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.

Flimzy
  • 4,465
Mat
  • 8,353
27

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 user@domain.com: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
vicho
  • 105
dmmd
  • 371
15

Not exactly what you're asking (you have real answers above with the subshell) but look at pushd and popd

Rich Homolka
  • 32,350
10

You have a few options. You can either group the commands with && or ;. Like this:

cd subdir && git init && cd ..

or

cd subdir; git init; cd ..

The difference between these is that in the first example, if one of the commands fails, it will not execute the rest of them. In the second example, all of the commands will run no matter what.

Another option would be to define a function and use it, for instance:

function cdinit() {
    cd $1
    git init
    cd ..
}

Then you can run the command:

cdinit subdir

And it will automatically git init in that directory and move out of it.

You could also do a more complex solution using a function if you have a bunch of directories and want to git init them with one command.

function cdinit() {
    for arg in $@
    do
        cd $arg
        git init
        cd ..
    done
}

You can then run this with:

cdinit subdir1 subdir2 subdir3

And it will do git init in subdir1, subdir2, and subdir3.

Wuffers
  • 19,619
6

In case of git (at least in version 2.7.0), you can leverage the -C option which makes git behave as if it was started in the given directory. So your solution may look like:

> git -C subdir init
Initialized empty Git repository in /some/path/subdir/.git/

Quoting the documentation:

Run as if git was started in <path> instead of the current working directory. When multiple -C options are given, each subsequent non-absolute -C
<path> is interpreted relative to the preceding -C <path>.

This option affects options that expect path name like --git-dir and --work-tree in that their interpretations of the path names would be made
relative to the working directory caused by the -C option. 
Mifeet
  • 190
2

You can group the commands with &&, i.e.

cd subdir && git init && cd ../

If you don't want any dependency on the exit code of each command, you can use ; instead, i.e.:

cd subdir ; git init ; cd ../
Gareth
  • 19,080
2

You have to hop into your target-directory if the command doesn't have a filename or directory name parameter.

But you can write a bash script that takes the target directory and the command as parameters. For this you could take a look at pushd and popd: http://ss64.com/bash/pushd.html

I would write that little script for you, but I haven't a Linux box here :)

Gareth
  • 19,080
wullxz
  • 2,876
2

Programs have different ways of handling arguments, so a few will have some equivalent of -folder=name option. Beyond that exception, the standard, even on MS DOS, is simply

$ program subdir

Sometimes you need

$ program subdir/

The program will open the folder, work with it the same way you work with a file, and once finished, return control to your shell, which is pointed at your original standard directory. Programs handled this way DO have the issue that error outputs (like core dumps) go to a file in your shell's current directory (rather than subdir.)

There is no workaround unless the program has command switches available to specify a different place. Some programmers take artistic license between "directory program was called from" and "directory program was told to work in ."

Vlueboy
  • 723