How do I push a new local branch to a remote Git r

2018-12-31 19:06发布

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 the remote repository (publish), but make it trackable so git pull and git 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.

13条回答
后来的你喜欢了谁
2楼-- · 2018-12-31 19:45

Prior to the introduction of git push -u, there was no git push option to obtain what you desire. You had 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
查看更多
刘海飞了
3楼-- · 2018-12-31 19:48

If you are not sharing your repo with others, this is useful to push all your branches to the remote, and --set-upstream tracking correctly for you:

git push --all -u

(Not exactly what the OP was asking for, but this one-liner is pretty popular)

If you are sharing your repo with others this isn't really good form as you will clog up the repo with all your dodgy experimental branches.

查看更多
呛了眼睛熬了心
4楼-- · 2018-12-31 19:52

To create a new branch by branching off from existing branch

git checkout -b <new_branch>

and then push this new branch to repository using

git push -u origin <new_branch>

This creates and pushes all local commits to a newly created remote branch origin/<new_branch>

查看更多
后来的你喜欢了谁
5楼-- · 2018-12-31 19:53

I simply do

git push -u origin localBranch:remoteBranchToBeCreated

over an already cloned project.

Git creates a new branch named remoteBranchToBeCreated under my commits I did in localBranch.

查看更多
还给你的自由
6楼-- · 2018-12-31 19:55

I made an alias so that whenever I create a new branch, it will push and track the remote branch accordingly. I put following chunk into the .bash_profile file:

# Create a new branch, push to origin and track that remote branch
publishBranch() {
  git checkout -b $1
  git push -u origin $1
}
alias gcb=publishBranch

Usage: just type gcb thuy/do-sth-kool with thuy/do-sth-kool is my new branch name.

查看更多
余生无你
7楼-- · 2018-12-31 19:57

To upload your local branch of a public repository, you need to cd to the public repository and then use the following code:

git push -u origin branchname
查看更多
登录 后发表回答