0

In a fish function, can I run a command from a certain directory without changing into it?

or

Can I run a git command (push) from outside the repository by specifying its path somehow?


I'd like to do something like this:

function backup
    set BKDIR /flash/drive
    rsync -av $HOME/important/ $BKDIR/important/
    cd $HOME/git_repo
    git push flash master
end

But I don't want to change into some other directory. It'd be great if I could sync the git repo like when I use rsync: by specifying the source directory but not changing the working directory.

I suppose a last resort would be to record the current directory at the beginning and change back to it at the end.

1

git --git-dir /foo/bar/ rest-of-git-command

ref:

--git-dir=<path> 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.

| improve this answer | |
  • I've actually found that using git -C /foo/bar rest-of-git-command is closer to what I wanted: "-C <path>: Run as if git was started in <path> instead of the current working directory." I think it works better in more cases, as it sets git-dir and work-tree. See -C in the git man-page. – askewchan Sep 30 '15 at 15:42
1

@RaGe has the correct answer.

A fish-specific answer is to use pushd and then popd

function backup
    # ...
    pushd $HOME/git_repo
    git push flash master
    popd
end
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.