IT story

모든 원격 자식 분기를 로컬 분기로 추적

hot-time 2020. 6. 2. 22:16
반응형

모든 원격 자식 분기를 로컬 분기로 추적


단일 원격 지점을 로컬 지점으로 추적하는 것은 간단합니다.

$ git checkout --track -b ${branch_name} origin/${branch_name}

모든 로컬 브랜치를 원격으로 푸시하고 필요에 따라 새 원격 브랜치를 생성하는 것도 쉽습니다.

$ git push --all origin

나는 반대로하고 싶다. 단일 소스에 X 개의 원격 분기가있는 경우 :

$ git branch -r 
branch1
branch2
branch3
.
.
.

각 원격 지점을 수동으로 만들 필요없이 모든 원격 지점에 대해 로컬 추적 지점을 만들 수 있습니까? 다음과 같이 말하십시오 :

$ git checkout --track -b --all origin

나는 구글과 RTM을 봤지만 지금까지 이층 적으로 등장했다.


bash 사용 :

자식 1.9.1 이후
for i in `git branch -a | grep remote | grep -v HEAD | grep -v master`; do git branch --track ${i#remotes/origin/} $i; done

크레딧 : Val Blant, elias 및 Hugo

자식 1.9.1 이전

참고 : 이후 버전의 git (> v1.9.1) 버전에서 사용되는 경우 다음 코드는

  1. (버그) 마스터를 추적하기 위해 생성 된 모든 브랜치
  2. (불쾌) 접두사가 붙일 모든 작성된 로컬 브랜치 이름 origin/
for remote in `git branch -r `; do git branch --track $remote; done

로컬 추적 분기에 변경 사항이 없다고 가정하고 분기를 업데이트하십시오.

for remote in `git branch -r `; do git checkout $remote ; git pull; done

모호한 참조 이름 경고를 무시하십시오 .git은 로컬 브랜치를 선호하는 것처럼 보입니다.


오토 (Otto)의 답변은 좋지만 생성 된 모든 브랜치는 이름의 시작으로 "origin /"을 갖습니다. 마지막 부분 (마지막 / 뒤)을 결과 분기 이름으로 사용하려면 다음을 사용하십시오.

for remote in `git branch -r | grep -v /HEAD`; do git checkout --track $remote ; done

또한 모호한 참조에 대해 경고하지 않는 이점이 있습니다.


다음은 내가 사용하는 하나의 라이너입니다 (bash 쉘에서 msysgit1.7.4로 테스트 됨).

복사 붙여 넣기의 경우 :

remote=origin ; for brname in `git branch -r | grep $remote | grep -v master | grep -v HEAD | awk '{gsub(/^[^\/]+\//,"",$1); print $1}'`; do git branch --set-upstream-to $remote/$brname $brname; done

가독성을 높이려면 :

remote=origin ; // put here the name of the remote you want
for brname in `
    git branch -r | grep $remote | grep -v master | grep -v HEAD 
    | awk '{gsub(/^[^\/]+\//,"",$1); print $1}'
`; do 
    git branch --set-upstream-to $remote/$brname $brname; 
done
  • remote변수에 지정한 리모콘에서 업스트림 분기 만 선택 합니다 ( ' origin'또는 현재 Git 저장소의 리모콘 중 하나에 설정 한 이름이 될 수 있음 ).
  • 표현식을 origin/a/Branch/Name => a/Branch/Name통해 분기의 이름을 추출합니다 awk.
  • 업스트림 브랜치 --set-upstream-to(또는 -u)을 통해 설정 하지 않습니다 --track.
    브랜치가 이미 존재하는 경우 브랜치가 실패하지 않고 해당 브랜치 원점을 변경하지 않으면 branch.xxx.(remote|merge)설정 만 구성한다는 이점이 있습니다 .

    branch.aBranchName.remote=origin
    branch.aBranchName.merge=refs/heads/a/Branch/Name
    

이 명령은 모든 원격 업스트림 브랜치에 대한 로컬 브랜치를 만들고 원격 및 병합 설정을 해당 원격 브랜치로 설정합니다.


여기에 대한 답변의 대부분은의 출력 구문 분석을 복잡하게 만듭니다 git branch -r. 다음 for루프를 사용하여 리모컨의 모든 분기에 대해 추적 분기를 만들 수 있습니다 .

이 원격 지점이 있다고 가정 해보십시오.

$ git branch -r
  origin/HEAD -> origin/master
  origin/development
  origin/integration
  origin/master
  origin/production
  origin/staging

Confirm that we're not tracking anything other than master already, locally:

$ git branch -l    # or using just git branch
* master

You can use this one liner to create the tracking branches:

$ for i in $(git branch -r | grep -vE "HEAD|master"); do 
    git branch --track ${i#*/} $i; done
Branch development set up to track remote branch development from origin.
Branch integration set up to track remote branch integration from origin.
Branch production set up to track remote branch production from origin.
Branch staging set up to track remote branch staging from origin.

Now confirm:

$ git branch
  development
  integration
* master
  production
  staging

To delete them:

$ git br -D production development integration staging 
Deleted branch production (was xxxxx).
Deleted branch development (was xxxxx).
Deleted branch integration (was xxxxx).
Deleted branch staging (was xxxxx).

If you use the -vv switch to git branch you can confirm:

$ git br -vv
  development xxxxx [origin/development] commit log msg ....
  integration xxxxx [origin/integration] commit log msg ....
* master      xxxxx [origin/master] commit log msg ....
  production  xxxxx [origin/production] commit log msg ....
  staging     xxxxx [origin/staging] commit log msg ....

Breakdown of for loop

The loop basically calls the command git branch -r, filtering out any HEAD or master branches in the output using grep -vE "HEAD|master". To get the names of just the branches minus the origin/ substring we use Bash's string manipulation ${var#stringtoremove}. This will remove the string, "stringtoremove" from the variable $var. In our case we're removing the string origin/ from the variable $i.

NOTE: Alternatively you can use git checkout --track ... to do this as well:

$ for i in $(git branch -r | grep -vE "HEAD|master" | sed 's/^[ ]\+//'); do 
    git checkout --track $i; done

But I don't particularly care for this method, since it's switching you among the branches as it performs a checkout. When done it'll leave you on the last branch that it created.

References


You could script that easily enough, but I don't know when it'd be valuable. Those branches would pretty quickly fall behind, and you'd have to update them all the time.

The remote branches are automatically going to be kept up to date, so it's easiest just to create the local branch at the point where you actually want to work on it.


for i in `git branch -a | grep remote`; do git branch --track ${i#remotes/origin/} $i; done

If you want to use powershell and your remote is called origin. Then this works.

git fetch    
git branch -r  | %{$_ -replace "  origin/"} | %{git branch --track $_ "origin/$_"}

without any scripting (in an empty directory):

$ git clone --bare repo_url .git
$ git config core.bare false
$ git checkout

after that, all remote branches will be seen as local.


original (in russian).


for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master`; do  git branch --track ${branch##*/} $branch; done

Use this and you will not have such warning as: refname 'origin/dev' is ambiguous


Here is my solution of BASH command referring by @tjmcewan:

for remote in `git branch -r | grep -v /HEAD `; do git branch --track ${remote/"origin/"/""}; done

My goal is to solve the problem that all the created branches will have "origin/" as the start of the name, because I tested that $remote variables are still include "origin/":

for remote in `git branch -r | grep -v /HEAD`; do echo $remote ; done

To do the same as tjmcewan's answer but on Windows, call this from a batch file:

for /f "delims=" %%r in ('git branch -r ^| grep -v master') do git checkout --track %%r

Or this from the command line:

for /f "delims=" %r in ('git branch -r ^| grep -v master') do git checkout --track %r

In case you already have some branches checked out and want to

  • check out all remaining branches from the remote
  • make sure all local branches track the remote branches

you can use the following bash- and zsh-compatible script:

git branch -r | while read b; do if git branch | grep -q " ${b##*/}$"; then git branch --set-upstream ${b##*/} $b; else git branch --track ${b##*/} $b; fi; done

for rembranch in `git remote update 2>&1 > /dev/null ; git branch -r|egrep -wv "HEAD|master"`
do 
    git checkout --track -b `echo $rembranch|awk -F\/ '{print $2}'` $rembranch; 
done

Explanation:

line 1: 'git branch -r' (followed by 'git remote update' to update the info on changes to remote) lists all remote branches; 'egrep -vw' is used to knock entries having HEAD and master in the result.

line 3: Track the named remote branch while checking it out locally. A simple awk is used to avoid 'origin/' being the suffix for local branches.


Using bash, If you want to checkout all branches:

for remote in `git branch -r`; do git checkout $(echo $remote | cut -d'/' -f 2); done

It’s important to note that when you do a fetch that brings down new remote-tracking branches, you don’t automatically have local, editable copies of them.

참고URL : https://stackoverflow.com/questions/379081/track-all-remote-git-branches-as-local-branches

반응형