views:

213

answers:

3

I tried looking for a an answer to this, but couldn't find any which address this specific need. Which is weird.

I want to be able to do the following:

  1. create a local branch based on some other (remote or local) branch (via git branch or git checkout -b)
  2. push the local branch to remote repo (publish), but make it trackable so git pull and git push will work immediately.

How do I do that?

EDIT: I know about --set-upstream in git 1.7, but that is a post-creation action. i want to find a way to make a similar change when pushing the branch to the remote repo.

+2  A: 

There is no git push option to obtain what you desire. You have to add new configuration statements.

If you create a new branch using:

$ git checkout -b branchB
$ git push origin branchB:branchB

You can use the git config command to avoid editing directly the .git/config file.

$ git config branch.branchB.remote origin
$ git config branch.branchB.merge refs/heads/branchB

Or you can edit manually the .git/config file to had tracking information to this branch.

[branch "branchB"]
    remote = origin
    merge = refs/heads/branchB
Lohrun
you can, see VP's and my answer
Tobias Kienzler
sorry, I disagree with your comment : you have to edit the .git/config file. This is what git publish-branch do automatically but it's not part of the core git command.
Lohrun
you're right, it's an extra script, but the safeguards are better than manually editing .git/config
Tobias Kienzler
your edits make it clearer. I edited my answer and added a pure bash script
Tobias Kienzler
+1  A: 

Use git publish-branch from William's miscellaneous git tools

edit: ok, no ruby, so - ignoring the safeguards! - take the last three lines of the script and create a bash script git-publish-branch

#!/bash/bin
REMOTE=$1 # rewrite this to make it optional...
BRANCH=$2
# uncomment the following line to create BRANCH locally first
#git checkout -b ${BRANCH}
git push ${ORIGIN} ${BRANCH}:refs/heads/${BRANCH} &&
git config branch.${BRANCH}.remote ${REMOTE} &&
git config branch.${BRANCH}.merge refs/heads/${BRANCH}

then run git-publish-branch REMOTENAME BRANCHNAME, where REMOTENAME is usually origin (you may modify the script to take origin as default etc...)

Tobias Kienzler
this assumes I have ruby installed. no such luck. any other ideas?
Roni Yaniv
the ruby script calls `git push` and `git config` command. I used the code of the script to edit my answer. You might used this information to create a small shell script that does the puslishing for you.
Lohrun
@Roni: ok, I added a bash script
Tobias Kienzler
A: 

I suppose that you have already cloned a project like:

git clone http://github.com/myproject.git

1.

then in your local copy you create a new copy and checkout it

git branch <newbranch>
git checkout <newbranch>

2.

Supposing that you made a "git bare --init" in your server and created the myapp.git you should:
git remote add origin ssh://myserver.com/var/git/myapp.git
git push origin master

3.

After that users should be able to
git clone http://myserver.com/var/git/myapp.git

NOTE i'm assuming that you have your server up and running. if you don't have it, it wont work. a good how to is here

ADDED

to add a remote branch:

git push origin origin:refs/heads/new_feature_name

check if everything is good

git fetch origin

list remove branches:

git branch -r

lets track the new branch:

git checkout --track -b new_feature_name origin/new_feature_name

update everything:

git pull
VP
William's script I linked to does about the same with the additional option to delete remote branches and some safeguards, too
Tobias Kienzler
>to push the local branch to remote repo (publish), but make it >trackable so git pull and git push will work immediately.its what github does automatically when you push your code to their repository :-)
VP
This does not respond to the question, the <newbranch> of the original repo is not trackable (and is renamed as <master> is the new repo you clone in step 3).
Lohrun
seems kind of overkill. does the `git remote add origin` make the local branch trackable? is that the key command here?
Roni Yaniv
@Roni Yaniv: no `git remote add origin` only register a new remote repository. It is just a step needed before pushing your branch to that remote repository (if you don't want to type the whole address each time)
Lohrun
well - we already have a remote repo (origin). i need a way to add a new local branch to that repo while making it trackable+pushable+pullable in the process.
Roni Yaniv
i added new instructions. It is to add new remote branch, track it and push and pull.
VP