I want to be able to do the following:
Create a local branch based on some other (remote or local) branch (via
git branch
orgit checkout -b
)Push the local branch to the remote repository (publish), but make it trackable so
git pull
andgit push
will work immediately.
How do I do that?
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 repository.
A slight variation of the solutions already given here:
Create a local branch based on some other (remote or local) branch:
Push the local branch to the remote repository (publish), but make it trackable so
git pull
andgit push
will work immediatelyUsing
HEAD
is a "handy way to push the current branch to the same name on the remote". Source: https://git-scm.com/docs/git-push In Git terms, HEAD (in uppercase) is a reference to the top of the current branch (tree).The
-u
option is just short for--set-setupstream
. This will add an upstream tracking reference for the current branch. you can verify this by looking in your .git/config file:Building slightly upon the answers here, I've wrapped this process up as a simple Bash script, which could of course be used as a Git alias as well.
The important addition to me is that this prompts me to run unit tests before committing and passes in the current branch name by default.
git_push_new_branch.sh
I suppose that you have already cloned a project like:
Then in your local copy, create a new branch and check it out:
Supposing that you made a "git bare --init" on your server and created the myapp.git, you should:
After that, users should be able to
NOTE: I'm assuming that you have your server up and running. If it isn't, it won't work. A good how-to is here.
ADDED
Add a remote branch:
Check if everything is good (fetch origin and list remote branches):
Create a local branch and track the remote branch:
Update everything:
In Git 1.7.0 and later, you can checkout a new branch:
Edit files, add and commit. Then push with the
-u
(short for--set-upstream
) option:Git will set up the tracking information during the push.
edit Outdated, just use
git push -u origin $BRANCHNAME
Use
git publish-branch
from William's miscellaneous Git tools (gitorious repo and clone).OK, no Ruby, so - ignoring the safeguards! - take the last three lines of the script and create a bash script,
git-publish-branch
:Then run
git-publish-branch REMOTENAME BRANCHNAME
, where REMOTENAME is usually origin (you may modify the script to take origin as default, etc...)For GitLab version prior to 1.7, use:
git checkout -b name_branch
(name_branch, ex :master)
To push it to the remote repository, do:
git push -u origin name_new_branch
(name_new_branch, example: feature)