乐闻世界logo
搜索文章和话题

How do i create a remote git branch?

3个答案

1
2
3

First, create a new local branch and verify it:

bash
git checkout -b <branch-name>

When you push it to the remote server, it automatically creates a remote branch:

bash
git push <remote-name> <branch-name>

The <remote-name> is typically origin, which is the name provided by Git for the remote server you cloned from. Then, your colleagues can simply pull the branch.

However, note that the format is:

bash
git push <remote-name> <local-branch-name>:<remote-branch-name>

When you omit one parameter, Git assumes both branch names are identical. Nevertheless, be cautious not to make the critical mistake of specifying only :<remote-branch-name> (using a colon), as this would delete the remote branch!


To inform subsequent users how to git pull, you might want to use:

bash
git push --set-upstream <remote-name> <local-branch-name>

As described below, the --set-upstream option configures the upstream branch:

For each latest or successfully pushed branch, add an upstream (tracking) reference used by git pull without parameters and other commands.

2024年6月29日 12:07 回复

Simple solution for Git 2.0+:


Starting from Git 2.0, the behavior becomes simpler:

You can configure git push.default = current to simplify the process:

I added this configuration, so now I can push a new branch upstream:

shell
$ git push -u

-u tracks the remote branch with the same name. With this configuration, you will automatically determine the remote reference for git push. From the git.config documentation:

Default Push

Defines the action git push should take when no refspec is explicitly given.

push.default = current - Pushes the current branch to update the remote branch with the same name. Suitable for both centralized and non-centralized workflows.

For me, this greatly simplifies my daily Git workflow. The configuration handles the common use case where you add a branch locally and want it created remotely. git checkout remote_branch_name Additionally, I can easily create a local branch from a remote branch by using the checkout command (instead of the --set-upstream-to flag).

I know this question and the accepted answer are quite old, but the behavior has changed, so now there are configuration options to simplify your workflow.

To add to the global Git configuration, run the following command on the command line:

shell
$ git config --global push.default current
2024年6月29日 12:07 回复

First, create a branch locally.

bash
git checkout -b your_branch

After that, you can work on the local branch. When you're ready to share it, push it. The next command pushes the branch to the remote origin and tracks it.

bash
git push -u origin your_branch

Teammates can reach your branch using the following commands:

bash
git fetch git checkout origin/your_branch

You can continue working on the branch and push when needed without specifying parameters for git push (a git push without parameters will push the local master to the remote master, the local your_branch to the remote your_branch, etc.).

bash
git push

Teammates can push to your branch by committing changes and then explicitly pushing.

bash
# ... work ... git commit # ... work ... git commit git push origin HEAD:refs/heads/your_branch

Alternatively, track the branch to avoid specifying parameters for git push.

bash
git checkout --track -b your_branch origin/your_branch # ... work ... git commit # ... work ... git commit git push
2024年6月29日 12:07 回复

你的答案