retrohacker
retrohacker

Reputation: 3234

Adding Git Remote to Different Directory

So I am writing a script that initializes a git repository in ~/.cfi, adds a remote, and pulls it down from the server.

Creating the directory, and initializing the repository work fine. The issue is adding the remote and doing a pull.

From the documentation, it doesn't look like git remote add has a directory parameter. Same goes for git pull. Is there a simple way to execute these two commands on the above directory without cding to it?

Upvotes: 6

Views: 5117

Answers (2)

Peter Westlake
Peter Westlake

Reputation: 5036

git itself has parameters --git-dir for the .git directory and --work-tree for your working copy:

git --git-dir /where/ever remote add ...

Upvotes: 1

VonC
VonC

Reputation: 1324576

Yes, with the --git-dir and --work-tree options.

And since git 1.8.5, you even can use -C (shorter option).
See "Use git log command in another folder"

git --git-dir=/path/to/repo/.git --work-tree=/path/to/repo remote add xxx
git --git-dir=/path/to/repo/.git --work-tree=/path/to/repo pull

Or:

git -C /path/to/repo remote add xxx
git -C /path/to/repo pull

Upvotes: 3

Related Questions