Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I deploy multiple branches to different directories via git push?

In production, I maintain two sites - beta and release. Each points to a different directory via a soft link (e.g.)

beta_public_html -> /home/scott/myapp/trunk/public
public_html      -> /home/scott/myapp/branches/1.2.3/public

I'm a longtime svn user, moving to git. I'm used to deploying via svn update and changing the soft link on a new branch, things are pretty simple.

Now I'm moving to git. I still need to have the two soft links (it's a Rails app using Passenger), though now I want them to point to two different git branches ("beta" and "release", say). And I want to be able to update them via git push (or git pull).

Question Part 1: I'm not sure the best way to do this.

The way I had started to do it was to just deploy to two different remotes, e.g.

git push ssh://[email protected]/home/scott/myapp-beta beta
git push ssh://[email protected]/home/scott/myapp-release release

But this doesn't work because push doesn't update the working tree by default.

So I go into the remote directories and run git reset --hard the first time, and it pulls the working tree. But I push again and I can't get the new push to show up - it just stays at the initial one.

(BTW, note that I can't seem to push to "myapp-beta.git" - that fails, I have to push to the directory name. I am worried that this is part of the problem, but I don't know what I did wrong here.)

So, if the answer to Question 1 is that my method is fine, Question Part 2: what's wrong with what I'm actually doing? If there are hooks I should be using, can someone point me to them?

(An answer to Question 1 that says "run these seven manual steps" will not be a terribly useful answer, seeing as svn checkout + ln -s are two steps.)

Thanks. I want to get back to writing code.

like image 627
scottru Avatar asked Sep 23 '09 06:09

scottru


People also ask

How do you push a commit to multiple branches?

Short answer. You can apply already existing commit to another branch using cherry-pick command, and then push both branches using git push origin branchA branchB .

How do I push all remote branches?

The steps to follow in order to push new Git branches to remote repos such as GitHub, GitLab or Bitbucket are as follows: Clone the remote Git repo locally. Create a new branch with the branch, switch or checkout commands. Perform a git push with the –set-upstream option to set the remote repo for the new branch.

Does git push push to all branches?

No, git push only pushes commits from current local branch to remote branch that you specified in command.

How do I push a git branch to another repository?

To push the commit from the local repo to your remote repositories, run git push -u remote-name branch-name where remote-name is the nickname the local repo uses for the remote repositories and branch-name is the name of the branch to push to the repository. You only have to use the -u option the first time you push.


1 Answers

The article Git push is worse than worsless has an interesting discussion about a similar issue.

One of its solution and conclusion involves:

  • a bare repository on the production server
  • a cloned repository with a hook to pull what has been pushed into the bare one

So in your case,

  • one bare repo on which you can push beta and release branches
  • two cloned repo 'beta' and 'release' with a hook to pull their respective branches from the bare repo.

In short: one step: git push. No more link to manage (since the directory no longer represent a branch in Git, unlike SVN)


Regarding the hook part, a post-receive hook in the bare repo could be all what you need

See Git Tip: Auto update working tree via post-receive hook

$ cd bare
$ chmod +x .git/hooks/post-receive

with a post-receive hook like

#!/bin/sh
cd ../../beta
env -i git reset --hard
cd ../../release
env -i git reset --hard

Note:

the post-receive hook starts out with the GIT_DIR environment variable set to the repo/.git folder, so no matter what path you 'cd' into it will always try to run any following git commands there.
Fixing this is simply a matter of unsetting the GIT_DIR.

'env -i' does just that: it ignores the inherited environment completely and uses only the supplied variables and values.

like image 171
VonC Avatar answered Nov 02 '22 15:11

VonC