6

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?

retrohacker
  • 3,194
  • 4
  • 21
  • 31

2 Answers2

3

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
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks m8! So with --git-dir and --work-tree being two different commands, I could theoretically keep .git in /foo and my repo in /bar? – retrohacker Feb 09 '14 at 19:12
  • 1
    @Crackers yes, though I am not "m8" ;) – VonC Feb 09 '14 at 19:12
  • @VonC how can I give a remote path to `--work-tree`? – horse Mar 11 '17 at 13:57
  • @horse you don't: work-tree is to reference a local (or shared) path you can access from your computer. What are you trying to do? – VonC Mar 11 '17 at 13:58
  • @VonC I have a remote deployment server. I want to be able to checkout previous commits if required. – horse Mar 11 '17 at 14:06
  • @horse As long as you can access those files from a shared path, you can ise work-tree. If not, it is best to push to a bare repo and let a post-receive hook checkout those files for you. (http://stackoverflow.com/a/12722204/6309) – VonC Mar 11 '17 at 14:09
  • @VonC via this command (from my dev local computer): `git --work-tree=... checkout -f -q` – horse Mar 11 '17 at 14:09
  • @horse As I just said, that kind of command is best executed from a post-receive hook on the server side (where work-tree is accessible locally) – VonC Mar 11 '17 at 14:10
  • @VonC Isn't it possible without remote bare repo? I am the only developer, and my local repo is the only repo. I don't want to add complexity. – horse Mar 11 '17 at 14:12
  • @horse with Git 2.4+, you can simply push directly to your (non-bare normal) repo on the remote server: http://stackoverflow.com/a/30030312/6309 – VonC Mar 11 '17 at 14:15
  • @VonC Ok. But, how can I push previous commits to remote server via post-receive hook? – horse Mar 11 '17 at 16:22
  • @horse a simple push will push *all* commits. Only the latest one will be checked out. No need for post-receive hook if you are pushing to a non-bare repo. – VonC Mar 11 '17 at 16:24
  • @horse read https://github.com/blog/1994-git-2-4-atomic-pushes-push-to-deploy-and-more: no post-receive hook involved. – VonC Mar 11 '17 at 16:25
  • @horse So, If I want to return a previous commit, I will connect to remote server and I will run `git checkout ; git --work-tree=/path/to/site/public checkout -f -q`. Right? – horse Mar 11 '17 at 16:44
  • @horse No: re-read https://github.com/blog/1994-git-2-4-atomic-pushes-push-to-deploy-and-more: the re is post-receive hook or checkout involve. If you want to go back a past commit, your reset your local branch and force push it. The last comit of your branch is the one checked out automatically by the remote non-bare repo. – VonC Mar 11 '17 at 16:49
  • @VonC But reset will delete last commit? – horse Mar 11 '17 at 18:13
  • @horse you don't have to reset your current branch: you can create another branch from an old commit, and push that new branch. – VonC Mar 11 '17 at 18:37
  • @VonC In local computer, I run: `git checkout ; git checkout -b branch_from_previous; git push remote_deployment branch_from_previous;` Nothing changes. Even I modified a file and `git add .; git commit -m "a commit from branch_from_previous"; git push remote_deployment branch_from_previous;` Nothing :( What is the problem? – horse Mar 12 '17 at 08:00
  • @horse https://github.com/blog/1994-git-2-4-atomic-pushes-push-to-deploy-and-more describes what you need to setup on the server side in order for the branch to be checked out. Make sure, on the remote side, you have Git 2.4 or more. – VonC Mar 12 '17 at 08:04
1

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

git --git-dir /where/ever remote add ...
Peter Westlake
  • 4,894
  • 1
  • 26
  • 35