I created a local branch which I want to \'push\' upstream. There is a similar question here on Stack Overflow on how to track a newly created remote branch.
Howeve
I've solved this by adding this into my bash ~/.profile
:
function gitb() { git checkout -b $1 && git push --set-upstream origin $1; }
Then to start up a new local + remote branch, I write:
gitb feature/mynewbranch
This creates the branch and does the first push, not just to setup tracking (so that later git pull
and git push
work without extra arguments), but actually confirming that the target repo doesn't already have such branch in it.
If you have used --single-branch
to clone the current branch, use this to create a new branch from the current:
git checkout -b <new-branch-name>
git push -u origin <new-branch-name>
git remote set-branches origin --add <new-branch-name>
git fetch
First, you must create your branch locally
git checkout -b your_branch
After that, you can work locally in your branch, when you are ready to share the branch, push it. The next command push the branch to the remote repository origin and tracks it
git push -u origin your_branch
Teammates can reach your branch, by doing:
git fetch
git checkout origin/your_branch
You can continue working in the branch and pushing whenever you want without passing arguments to git push (argumentless git push will push the master to remote master, your_branch local to remote your_branch, etc...)
git push
Teammates can push to your branch by doing commits and then push explicitly
... work ...
git commit
... work ...
git commit
git push origin HEAD:refs/heads/your_branch
Or tracking the branch to avoid the arguments to git push
git checkout --track -b your_branch origin/your_branch
... work ...
git commit
... work ...
git commit
git push
First you create the branch locally:
git checkout -b your_branch
And then to create the branch remotely:
git push --set-upstream origin your_branch
Note: This works on the latests versions of git:
$ git --version
git version 2.3.0
Cheers!
Creating a local branch from an existing branch (can be master/ develop/ any-other-branch).
git checkout -b branch_name
Push this to remote
git push -u remote_name local_branch_name:remote_branch_name
Here,
If we remove the local and remote branch names, it will have the format
git push -u remote_name branch_name
This will push the local branch to remote and with the same name as the local branch branch_name. The local branch will be tracking the remote branch as well.