if we have a remote branch A, and we would like to rename it to B, we could do
$ git checkout -b A origin/A
$ git branch -M A B
$ git push origin B
$ git push origin :A
---------------------------------------------------------
First, clone a remote git repository and cd into it:
$ git clone git://example.com/myproject $ cd myproject
Next, look at the local branches in your repository:
$ git branch
* master
But there are other branches hiding in your repository! You can see these using the -a
flag:
$ git branch -a
* master
origin/HEAD
origin/master
origin/v1.0-stable
origin/experimental
If you just want to take a quick peek at an upstream branch, you can check it out directly:
$ git checkout origin/experimental
But if you want to work on that branch, you'll need to create a local tracking branch:
$ git checkout -b experimental origin/experimental
Now, if you look at your local branches, this is what you'll see:
$ git branch
master
* experimental
You can actually track more than one remote repository using git remote
.
$ git remote add win32 git://example.com/users/joe/myproject-win32-port
$ git branch -a
* master
origin/HEAD
origin/master
origin/v1.0-stable
origin/experimental
win32/master
win32/new-widgets
------------------------------------------
push and delete remote branches
This is an action that many Git users need to do frequently, but many (including the author) have forgotten how to do so or simply don’t know how. Here’s the definitive guide if you’ve forgotten.
So let’s say you have checked out a new branch, committed some awesome changes, but now you need to share this branch though with another developer. You can push the branch up to a remote very simply:
git push origin newfeature
Where origin
is your remote name and newfeature
is the name of the branch you want to push up. This is by far the easiest way, but there’s another way if you want a different option. Geoff Lane has created a great tutorial which goes over how to push a ref to a remote repository, fetch any updates, and then start tracking the branch.
Deleting is also a pretty simple task (despite it feeling a bit kludgy):
git push origin :newfeature
That will delete the newfeature
branch on the origin
remote, but you’ll still need to delete the branch locally with git branch -d newfeature
.
There’s a script called git-publish-branch created by William Morgan that can easily automate this process if you find yourself performing these actions frequently. It also makes deleting remote branches feel a bit more natural. Know of better or easier ways to do the above tasks? Let us know in the comments or submit your own tip!
没有评论:
发表评论