首页 > 技术文章 > git branch

chucklu 2019-10-14 10:30 原文

Git push branch from one remote to another?

A quick test making some temporary repositories shows you can construct a refspec that can do this:

$ git push rorg origin/one:refs/heads/one
Counting objects: 5, done.
Writing objects: 100% (3/3), 240 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /tmp/rorg
 * [new branch]      origin/one -> one

So origin/BRANCHNAME:refs/heads/BRANCHNAME

Checking in my rorg remote:

  • Clone一个本地干净的库 或者 使用你本地的一个库
  • 命令行新增一个remote 叫azure
    • git remote add azure url
  • 将remote/origin中的所有分支全部推到azure中
    • git push azure refs/remotes/origin/*:refs/heads/*   //make sure the previous remote name is origin
  • 使用同样的方法将tags都推到azure中
    • git push azure refs/tags/*:refs/tags/*

 

过滤分支

git branch -a --list 'o*/chuck/*'

在过滤远程分支的时候,remote name也在过滤里面,pattern可以限制remote name。

remotes/origin/chuck/configuration
remotes/origin/chuck/configuration-uk6.3
remotes/origin/chuck/master

 

What is the format of <pattern> in git-branch --list

Quoting from that same manual page you linked:

If --list is given, or if there are no non-option arguments, existing branches are listed; the current branch will be highlighted with an asterisk. Option -r causes the remote-tracking branches to be listed, and option -a shows both local and remote branches. If a <pattern> is given, it is used as a shell wildcard to restrict the output to matching branches. If multiple patterns are given, a branch is shown if it matches any of the patterns. Note that when providing a <pattern>, you must use --list; otherwise the command is interpreted as branch creation.

So the answer, at least according to the documentation, is that "it is used as a shell wildcard". This assumes, of course, that you know what the phrase "shell wildcard" means—and more importantly, it's wrong, since a straight shell wildcard would not match across the /.

The documentation should say something like: "The pattern acts much like a shell wildcard / glob pattern, except that slashes are not treated specially, so that a*b matches both accb and ac/cb, and a[bc/]* matches all of a/d, abcd, ac/cb, and accb."

Examples:

$ git branch -a
  a/d
  abcd
  ac/cb
  accb
* master
$ git branch --list 'a*b'
  ac/cb
  accb
$ git branch --list 'a[bc/]*'
  a/d
  abcd
  ac/cb
  accb
$ 

 

How do you stop tracking a remote branch in Git?

As mentioned in Yoshua Wuyts' answer, using git branch:

git branch --unset-upstream

Other options:

You don't have to delete your local branch.

Simply delete the local branch that is tracking the remote branch:

git branch -d -r origin/<remote branch name>

-r, --remotes tells git to delete the remote-tracking branch (i.e., delete the branch set to track the remote branch). This will not delete the branch on the remote repo!

See "Having a hard time understanding git-fetch"

there's no such concept of local tracking branches, only remote tracking branches.
So origin/master is a remote tracking branch for master in the origin repo

As mentioned in Dobes Vandermeer's answer, you also need to reset the configuration associated to the local branch:

git config --unset branch.<branch>.remote
git config --unset branch.<branch>.merge

Remove the upstream information for <branchname>.
If no branch is specified it defaults to the current branch.

(git 1.8+, Oct. 2012, commit b84869e by Carlos Martín Nieto (carlosmn))

That will make any push/pull completely unaware of origin/<remote branch name>.

 

Find out which remote branch a local branch is tracking

回答1

Here is a command that gives you all tracking branches (configured for 'pull'), see:

$ git branch -vv
  main   aaf02f0 [main/master: ahead 25] Some other commit
* master add0a03 [jdsumsion/master] Some commit

You have to wade through the SHA and any long-wrapping commit messages, but it's quick to type and I get the tracking branches aligned vertically in the 3rd column.

If you need info on both 'pull' and 'push' configuration per branch, see the other answer on git remote show origin.


Update

Starting in git version 1.8.5 you can show the upstream branch with git status and git status -sb

https://git-scm.com/docs/git-status  

-s--short

Give the output in the short-format.

-b--branch

Show the branch and tracking info even in short-format.

 

回答2

git branch -vv | grep 'BRANCH_NAME'

git branch -vv : This part will show all local branches along with their upstream branch .

grep 'BRANCH_NAME' : It will filter the current branch from the branch list.

 

 

 

 

推荐阅读