繁体   English   中英

如何使新的本地分支跟踪与其创建的本地分支相同的远程分支?

[英]How do I make a new local branch track the same remote branch as the local branch it's created from?

简洁版本:

我想从本地分支(A)创建一个本地分支(B),并使其跟踪(A)正在跟踪的同一远程分支。 我怎么能用一个命令做到这一点? 有什么办法可以将此行为设置为默认行为?

完整说明:

我最近已从使用git-svn转换为“纯” git。 我的工作流程的某个方面确实令人沮丧,我正在尝试找出一种还原该工作流程的方法。 这就是git-svn的样子:

  • 创建一个跟踪远程分支(X)的新本地分支(A)
  • 做一些工作,做一些本地提交。
  • git svn rebase -A被重新建立到X的HEAD
  • 做一些工作,做一些本地提交。
  • 有一个分支想法,从(A)建立一个新的本地分支(B)
  • 做一些工作,做一些本地提交。
  • git svn rebase -B被重新建立到X的HEAD
  • 等等

在纯git的世界中,我遇到的问题是,尽管(A)跟踪远程分支,但(B)不继承该分支。 我知道我可以通过执行git branch --set-upstream (B) (X)来显式设置此设置。 我正在寻找的是一种自动继承此跟踪行为的方法,因此我不必记住要这样做,然后在我的git pull --rebase无法在(B)上工作时让所有事情感到沮丧。

我意识到与此有关的“问题”是它可能会失去由(A)制成的(B)的传统。 我只是不在乎。

有任何想法吗?

您正在尝试将集中式工作流程应用于分布式系统。 您需要考虑在本地而不是集中地做事。 共享(中央)存储库只是放置您要与他人共享的内容或检索他们想要共享的内容的地方。

这实际上是您要的。 但是,我认为这不是最好的工作流程。 参见下面的修改版本。

Create a new local branch (A) that tracks a remote branch (X)
   git clone <url> my_repo
Do some work, make some local commits.
   work, work work
   git add .
   git commit -m "commit of work"

A is rebased up to the HEAD of X.  We're operating on the same branch 
we want to rebase from the remote, so we can do it all with one command in 
this case.
   git pull --rebase

Do some work, make some local commits.
   work, work work
   git add .
   git commit -m "commit of work"

Have an offshoot idea, make a new local branch (B) from (A)
  git checkout -b idea

Do some work, make some local commits.
   work, work work
   git add .
   git commit -m "commit of work"

B is rebased up to the HEAD of X

   git rebase origin master

但是....整个工作流程都围绕着遥控器作为“真理之源”。 做这件事的更多方法是,恕我直言,您应该将本地视为真实的来源,并使用中央存储库中的共享内容对其进行更新。 这是同一回事,只是从不同的角度来对待它。

这是我如何处理您的工作流程:

创建一个跟踪远程分支(X)的新本地分支(A)git clone my_repo做一些工作,进行一些本地提交。 工作,工作工作git add。 git commit -m“工作的提交”

A is rebased up to the HEAD of X.  We use two commands here, but doing
it this way makes it easy for me to view the differences before 
doing the rebase, if I choose.
   git fetch
   git rebase origin/master

Do some work, make some local commits.
   work, work work
   git add .
   git commit -m "commit of work"

Have an offshoot idea, make a new local branch (B) from (A)
  git checkout -b idea

Do some work, make some local commits.
   work, work work
   git add .
   git commit -m "commit of work"

B is rebased up to the HEAD of X

   git fetch
   git rebase origin/master

当然,这两种情况都取决于您在将源/主控重新部署到想法分支之前,您没有在本地主控分支上进行其他工作的事实。 如果这样做,您将不会拥有在本地对master所做的提交,这样做会更有效:

git fetch
git checkout master
git rebase origin/master  --(make sure master is up-to-date locally)
git checkout idea
git rebase master (apply idea on top of the updated local master)

好吧,最简单的解决方案,但可能是部分解决方案,就是要做:

git checkout -b B <remote>/X
git merge A

如果您在需要B之前已将A推回X,则无需执行第二步。根据遥控器签出B会自动设置跟踪。

至于一步:

function myGitCo () { git checkout -b $1 $3; git merge $2 }   # <smiley>

撒脱醇” bashperl脚本的魔力,并炮制了这个解决方案,拖放到一个脚本调用my-new-branch

#/bin/bash
REMOTETRACKINGBRANCH=`git branch -vvv | perl -ne '/^\*[^\[]*\[[^:\]]+[:\]]+.*$/ && s/^\*[^\[]*\[([^:\]]+)[:\]]+.*$/\1/ && print;'`
if [ -z "$REMOTETRACKINGBRANCH" ];
then
    echo "ERROR: No remote tracking branch." 1>&2
    exit 1
else
    git checkout -b $1 && git branch --set-upstream $1 $REMOTETRACKINGBRANCH
fi

我敢肯定,这可能会更简短,但是我厌倦了使用shell逃避正则表达式,并使用perl强制使用它。

如果有一种更易于解析的方法(并且不太可能随着git revs的变化而改变)来更好地识别远程跟踪分支,那将是很好的选择,但是AFAICT的配置文件对于这些信息和解析是权威的。这似乎比解析git branch -vvv更痛苦。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM