[error]the requested upstream branch 'origin/feature-rsv' does not exist


새로운 브랜치를 만들고, remote에 넣어준 다음 tracked 시키려고 했는데 자꾸 아래와 같은 에러 메시지가 발생했다.

Error Message

error: the requested upstream branch 'origin/feature-rsv' does not exist
hint: 
hint: If you are planning on basing your work on an upstream
hint: branch that already exists at the remote, you may need to
hint: run "git fetch" to retrieve it.
hint: 
hint: If you are planning to push out a new local branch that
hint: will track its remote counterpart, you may want to use
hint: "git push -u" to set the upstream config as you push.

Error 전 진행 과정

$ git remote -v
origin	https://github.com/hyex/w1.git (fetch)
origin	https://github.com/hyex/w1.git (push)
upstream	https://github.com/boostcamp-2020/w1.git (fetch)
upstream	https://github.com/boostcamp-2020/w1.git (push)

$ git branch
* J062

$ git checkout -b feature-rsv
Switched to a new branch 'feature-rsv'

$ git branch
  J062
* feature-rsv

$ git branch -vv
  J062        2a563f9 [origin/J062: ahead 16] merge
* feature-rsv 2a563f9 merge

$ git push -u origin feature-rsv
Total 0 (delta 0), reused 0 (delta 0)
remote: 
remote: Create a pull request for 'feature-rsv' on GitHub by visiting:
remote:      https://github.com/hyex/w1/pull/new/feature-rsv
remote: 
To https://github.com/hyex/w1.git
 * [new branch]      feature-rsv -> feature-rsv
Branch 'feature-rsv' set up to track remote branch 'feature-rsv' from 'origin'.

$ git branch -vv
  J062        2a563f9 [origin/J062: ahead 16] merge
* feature-rsv 2a563f9 merge # problem

원래는 # problem 부분에서 traked가 되었다면 [origin/feature-rsv:] ... 이런 식으로 떠야 한다. git push -u 명령어를 통해 traked가 될 것이라고 생각했는데 실패해서 아래 명령어를 사용하고, 위 에러 메시지를 만났다.

$ git branch --set-upstream-to origin/feature-rsv
error: the requested upstream branch 'origin/feature-rsv' does not exist
hint: 
hint: If you are planning on basing your work on an upstream
hint: branch that already exists at the remote, you may need to
hint: run "git fetch" to retrieve it.
hint: 
hint: If you are planning to push out a new local branch tha

해결 과정

1. .git/config 수정

이 것 저 것 해봐도 다 안되어서 열받던 찰나에, .git/config를 고쳐야 한다는 를 보게되었다.

.git/config

	[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
        precomposeunicode = true
	[remote "origin"]
	        url = https://github.com/hyex/w1.git
	        fetch = +refs/heads/J062:refs/remotes/origin/J062 # here
	[branch "J062"]
	        remote = origin
	        merge = refs/heads/J062
	[remote "upstream"]
	        url = https://github.com/boostcamp-2020/w1.git
	        fetch = +refs/heads/J062:refs/remotes/upstream/J062
	[branch "feature-rsv"]
	        remote = origin
	        merge = refs/heads/feature-rsv                     

여기서 감이 왔다. # here 부분에서 fetch에 브랜치 이름이 들어가있고, 뭔가 에러 메시지에서 fetch를 해보라는데 안되는 걸로 봐서는 저게 문제인 것 같았다. 다음과 같이 수정했다.

fetch = +refs/heads/*:refs/remotes/origin/*

그리고 확인해봤다.

$ git branch -vv
  J062        2a563f9 [origin/J062: ahead 16] merge
* feature-rsv 780d9bd [origin/feature-rsv: gone] Refactor: DB structure

하지만 이 방법은 사실 --single-branch를 무산시키는 것과 비슷한 느낌을 받았다.

2. remote 에 브랜치 추가

.git/config 파일이 위와 같이 설정되어 있어서 remote branch 중 J062 에 대해서만 가져올 수 있는 것인 것 같다. 지금까지 track 을 하기 위한 방법이 아닌 다른 길로 새다가 드디어 제대로 된 질문을 던졌다.

“싱글 브랜치로 레포를 클론해온 상태에서, 또 다른 브랜치를 tracked 하는 방법은?”

답은 remote 에 내가 traked 하고 싶은 브랜치를 추가하는 것이다. 이제와서야 하나의 브랜치와 연결되어 있는데 당연히 다른 브랜치를 추적할 수 없다는 사실을 깨닳았다.

새로운 브랜치를 내 remote에서 fetch할 수 있도록 추가하고, 로컬 브랜치와 리모트 브랜치를 연결해준다.

git remote set-branches --add origin [remote-branch]
git push -u origin [remote-branch]

그럼 이제 tracked 되는 것을 확인할 수 있다.

$ git branch -vv
  J062    17788d4 [origin/J062: ahead 27] merge
* feature c7ed95c [origin/feature] Docs: Add ehroku URL to README

또한 config 파일을 보면 아래와 같이 변경되어 있다.

# .git/config
[remote "origin"]
        url = https://github.com/hyex/javascript-w1-airbnb.git
        fetch = +refs/heads/J062:refs/remotes/origin/J062
        fetch = +refs/heads/feature:refs/remotes/origin/feature

이유

clone할 때 single-branch 옵션을 주어서 fetch는 하나의 브랜치만 가능하도록 .git/config 파일에 설정되었다. 사실 tracked을 위해서가 아니라면 굳이 remote의 또 다른 브랜치를 추가하지 않고 git fetch origin [remote-branch]:[local-branch] 명령어를 통해 가져오는 것이 가능하지만, 난 tracked를 원했기 때문에 먼 길을 돌아왔다.

Reference

그냥 보기 좋았던 글 답을 준 글