我的主分支和开发分支在GitHub上被远程跟踪。如何克隆这两个分支?


当前回答

在这里,我为您编写了一个很好的函数,使其易于重复

gitCloneAllBranches() { # clone all git branches at once easily and cd in
  # clone as "bare repo"
  git clone --mirror $1
  # rename without .git extension
  with_extension=$(basename $1)
  without_extension=$(echo $with_extension | sed 's/.git//')
  mv $with_extension $without_extension
  cd $without_extension
  # change from "bare repository" to not
  git config --bool core.bare false
  # check if still bare repository if so
  if [[ $(git rev-parse --is-bare-repository) == false ]]; then
    echo "ready to go"
  else
    echo "WARNING: STILL BARE GIT REPOSITORY"
  fi
  # EXAMPLES:
  # gitCloneAllBranches https://github.com/something/something.git
}

其他回答

您可以轻松切换到分支,而无需使用花哨的“gitcheckout-b somebranch origin/somebranch”语法。您可以执行以下操作:

git checkout somebranch

Git将自动执行正确的操作:

$ git checkout somebranch
Branch somebranch set up to track remote branch somebranch from origin.
Switched to a new branch 'somebranch'

Git将检查在一个远程中是否存在同名的分支,如果存在,它将以与您明确指定的远程分支相同的方式对其进行跟踪。从git 1.8.2.1的git checkout手册页面:

如果未找到<branch>,但中确实存在跟踪分支只有一个具有匹配名称的远程(称为<remote>),视为相当于$git checkout-b<branch>--track<remote>/<branch>

我试图找出如何删除我在本地删除的远程分支。起源不是我的,我不想经历重新克隆一切的麻烦。

这对我有用:

假设您需要在本地重新创建分支:

git checkout -b recreated-branch-name
git branch -a (to list remote branches)
git rebase remotes/remote-origin/recreated-branch-name

因此,如果我从gituser/master分支到sjp,然后将其分支到sjp/mynewbranch,则如下所示:

$ git checkout -b mynewbranch

$ git branch -a
  master
  remotes/sjp/master
  remotes/sjp/mynewbranch

$ git fetch (habit to always do before)

$ git rebase remotes/sjp/mynewbranch

如果使用Bitbucket,则可以使用import Repository。这将导入所有Git历史记录(所有分支和提交)。

您正在执行的提取应该获取所有远程分支,但不会为它们创建本地分支。如果您使用gitk,您应该看到远程分支被描述为“remotes/origin/dev”或类似的名称。

要基于远程分支创建本地分支,请执行以下操作:

git checkout -b dev refs/remotes/origin/dev

应该返回如下内容:

Branch dev set up to track remote branch refs/remotes/origin/dev. Switched to a new branch "dev"

现在,当您在dev分支上时,“git pull”会将本地dev更新到与远程dev分支相同的位置。注意,它会取出所有的树枝,但只会将你所在的树枝拉到树的顶端。

关于

git checkout-b实验来源/实验

使用

git checkout -t origin/experimental

或者更冗长,但更容易记住

git checkout --track origin/experimental

在跟踪远程存储库方面可能更好。