Git 내부 - Refspec

송준섭 Junseop Song·2024년 1월 7일

Git

목록 보기
8/8
post-thumbnail

Git - Refspec

Refspec

아래는 새로운 origin이라는 리모트 저장소를 추가하는 명령어

$ git remote add origin https://github.com/~~~

이 명령어로 origin이라는 저장소 이름, URL, Fetch할 Refpsec을 .git/config 파일에 추가

[remote "origin"]
    url = https://github.com/schacon/simplegit-progit
    fetch = +refs/heads/*:refs/remotes/origin/*

기본적으로 Git은 git remote add 명령으로 생성한 설정을 참고하여 리모트 서버에서 refs/heads/ 에 있는 Refs를 가져다 로컬의 refs/remotes/origin/ 에 기록

만약 로컬에서 서버에 있는 master 브랜치에 접근할 때는 아래와 같이 여러 방법으로 가능

$ git log origin/master
$ git log remotes/origin/master
$ git log refs/remotes/origin/master

Refspec 형식은 + 와 <src>:<dest> 로 되어 있음

+ 는 생략 가능하고, <src> 는 리모트 저장소의 Refs 패턴이고 <dst> 는 매핑되는 로컬 저장소의 Refs 패턴

+ 는 Fast-forward가 아닌 업데이트를 허용하는 것

master 브랜치만 가져올 수 있게 하려면 fetch 부분을 아래와 같이 바꿔줌

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

이는 해당 리모트 저장소에서 git fetch 명령을 실행할 때 자동으로 사용되는 Refspec

다른 Refspec을 가져오려면 명령의 아규먼트로 넘기면 됨

$ git fetch origin master:refs/remotes/origin/mymaster
$ $ git fetch origin master:refs/remotes/origin/mymaster \
     topic:refs/remotes/origin/topic
From git@github.com:schacon/simplegit
 ! [rejected]        master     -> origin/mymaster  (non fast forward)
 * [new branch]      topic      -> origin/topic

두 번째 명령어처럼 Refspec을 여러 개 넘겨도 됨

여기서 master 브랜치는 Fast-forward가 아니라서 거절

하지만, Refspec 앞에 + 를 추가하면 강제로 덮어쓸 수 있음

설정 파일에도 Refspec을 여러 개 적을 수 있음

master 와 experiment 브랜치를 둘 다 적으면 origin 리모트에서 항상 함께 가져옴

[remote "origin"]
    url = https://github.com/schacon/simplegit-progit
    fetch = +refs/heads/master:refs/remotes/origin/master
    fetch = +refs/heads/experiment:refs/remotes/origin/experiment
fetch = +refs/heads/qa*:refs/remotes/origin/qa*

위처럼 네임스페이스 형식으로 적어도 됨

만약 QA 팀이 Push 하는 브랜치가 있고 이 브랜치를 가져오고 싶으면 아래와 같이 설정

[remote "origin"]
    url = https://github.com/schacon/simplegit-progit
    fetch = +refs/heads/master:refs/remotes/origin/master
    fetch = +refs/heads/qa/*:refs/remotes/origin/qa/*

QA 팀뿐만 아니라, 일반 개발자, 통합 팀 등이 사용하는 브랜치를 네임스페이스 별로 구분해 놓으면 좀 더 Git을 편리하게 사용할 수 있음

→ 이래서 새로운 기능 브랜치 만들 때 feat/~~ 이렇게 하는건가?

**Refspec Push 하기**

만약 QA팀이 네임스페이스를 사용하지 않는 브랜치를 리모트에 네임스페이스를 써서 Push 하려면?

QA 팀이 master 브랜치를 리모트 저장소에 qa/master 로 Push 하려면 아래와 같이 하면 됨

$ git push origin master:refs/heads/qa/master

git push origin 을 실행할 때마다 Git이 자동으로 Push 하게 하려면 아래와 같이 설정 파일에 push 항목을 추가

[remote "origin"]
    url = https://github.com/schacon/simplegit-progit
    fetch = +refs/heads/*:refs/remotes/origin/*
    push = refs/heads/master:refs/heads/qa/master

Refs 삭제하기

$ git push origin :topic

Refspec의 형식은 <src>:<dst> 이니까 <src> 를 비우고 실행하면 <dst> 를 비우라는 명령이 됨

따라서 위 명령은 리모트의 topic 브랜치를 삭제하라는 명령

Git 버전 v1.7 이상에서는 아래 명령어로도 삭제 가능

$ git push origin --delete topic

0개의 댓글