- @참고: https://kcmschool.com/194

 

git ignore 파일 작성 예시

- .gitignore

## 파일 무시
test.txt

## 다음과 같은 확장자는 전체 무시
*.text
*.exe
*.zip

## 폴더 무시
test/

* 기존 git 의 관리를 받는 (commit 된) 파일, 폴더는 ignore 되지 않음

 

기존 git 관리를 받는 파일, 폴더에 대하여 ignore 하기

방법 1. 기존에 가지고 있는 cache 를 지우기

## 파일 이라면
git rm --cached test.txt

## 전체파일 이라면
git rm --cached *.txt

## 폴더 라면
git rm --cached test/ -r

* staging area 에서 파일을 제거하고 working directory 에서는 파일을 유지함.

* 위 명령어 실행 후 commit 이 필요함.

 

방법 2. cache 무시하기

# cache 무시하기
$ git update-index --assume-unchanged [파일명]

# cache 무시 취소하기
$ git update-index --no-assume-unchanged [파일명]

# 수정사항 무시 파일 조회
$ git ls-files -v|grep '^h'

 

블로그 이미지

uchacha

개발자 일지

,

[Git] branch naming

Git 2022. 9. 21. 10:39

- @참고: https://junjunrecord.tistory.com/131

- @참고(git branch & naming): https://velog.io/@kim-jaemin420/Git-branch-naming

 

branch 네이밍 규칙

1) master branch, develop branch

그대로 사용이 일반적

2) feature branch

feature/기능요약 형식 추천. ex) feature/login

feature/{issue-number}-{feature-name} 의 이슈추적 형식. ex) feature/1-init-project, feature/2-build-gradle-script-write

3) release branch

release-{version} 형식 추천. ex) release-1.2

4) hotfix branch

hotfix-{version} 형식 추천. ex) hotfix-1.2.1

블로그 이미지

uchacha

개발자 일지

,
블로그 이미지

uchacha

개발자 일지

,

@참고: https://stackoverflow.com/questions/9984223/what-happens-to-git-commits-created-in-a-detached-head-state

 

상황

detached head 상태인줄을 모르고 commit 한 후 다른 branch로 checkout 을 하였으며, commit 내역은 master에 merge 되어야 하는 부분이였다.

 

해결

1. commit 된 hash 값을 확인한다.

$git reflog
HEAD가 참조했던 commit들을 역순으로 보여준다. 특히 "lost" commit 내용도 볼 수 있다.

2. 해당 commit(SHA-1 해쉬값)을 끼고 별개로 새로운 임시 branch를 만든다.

$git branch solve_detach dac3n88

3. 본래 사용하던 branch로 checkout 하여 임시 branch와 merge 한다.

 

블로그 이미지

uchacha

개발자 일지

,

@참고: https://song8420.tistory.com/138

1. Fetch from Upstream 한다. 

2. Synchronize Workspace 로 충돌난 (빨간 <-> 화살표로 표시됨) 파일을 원하는 방향으로 수정해준다.

3. 충돌난 파일을  Add to Index 한다.

4. Git 으로 가서 프로젝트에 대해서  Pull을 진행한다.

 

블로그 이미지

uchacha

개발자 일지

,

[Git] Submodule

Git 2021. 4. 1. 10:50

1.  도입 고려 조건

  • 두 프로젝트를 서로 별개로 다루면서도 그 중 하나를 다른 하나 안에서 사용할 수 있어야 한다.
  • 다른 독립된 Git 저장소를 Clone 해서 내 Git 저장소 안에 포함할 수 있으며 각 저장소의 커밋은 독립적으로 관리한다.
  • MainProject 에서 submodule 의 이력을 관리한다.

 

2.  기존 프로젝트에 submodule 붙이기

  • 가정
    기존 프로젝트를 MainProject, 붙이려는 submodule를 DbConnector 라 하자.

 

  • 실행 명령어

$MainProject>> git submodule add https://github.com/chaconinc/DbConnector 

  • 명령어 실행 시 MainProject 디렉토리에 submodule repository 명으로 서브프로젝트가 추가된다.

 

  • 아래 명령어로 status 를 확인해 보면 .gitmodules, DbConnector 가 추가됨을 알 수 있다.

$MainProject>> git status

 

  • .gitmodules 파일 내용은 서브모듈의 정보를 관리하며 예시는 아래와 같다.
[submodule "DbConnector"]
path = DbConnector
url = https://github.com/chaconinc/DbConnector

 

  • 서브모듈의 변경사항 내역을 보고 싶은 경우 diff에 --submodule을 추가하여 실행한다.

$MainProject>> git diff --submodule

 

  • 서브모듈이 추가된 MainProject 는 기존과 동일하게 commit, push 해주면 remote에 변경사항을 등록할 수 있다.

 

3. 서브모듈과 함께 프로젝트를 clone 하기

  • clone, submodule init, submodule update 세번의 명령어로 실행할 수 있으며,
    한번에 실행하는 방법은 아래 명령어와 같다.

worspace_dir>> git clone --recurse-submodules https://github.com/chaconinc/MainProject 

 

  • submodule 을 init, update 하는 명령어는 아래와 같이 축약될 수 있다.

$MainProject>> git submodule update --init --recursive

 

4. remote 서브모듈의 최신버전을 pull 하기

  • 서브모듈 디렉토리에서 pull 해올 수도 있으나
    submodule update 명령어를 활용하여 MainProject에서도 가능하다.

$MainProject>> git submodule update --remote DbConnector

  • ** 위 명령어는 새로운 이력이 존재할 때 Head를 detach시키므로
    submodule이 checkout한 branch로 merge하고 싶다면 --merge 옵션을 추가해야 한다.

$MainProject>> git submodule update --remote --merge DbConnector

 

  • 서브모듈 디렉토리의 현재까지의 이력 변경 내용을 자세히 알고 싶다면 diff에 --submodule 옵션을 사용할 수 있다.

$MainProject>> git diff --submodule

 

  • 위 옵션이 딸린 명령어를 diff 의 default 옵션으로 사용하기 위해 config로 등록하는 방법은 아래와 같다.

$>> git config --global diff.submodule log

 

  • MainProject 에서 추적하는 submodule 이력을 commit 하여 update 할 수 있다.

 

  • submodule의 추적 branch 변경하기

$MainProject>> git config -f .gitmodules submodule.DbConnector.branch stable

 

  • status 확인 시 submodule 간단한 변경 이력 상태를 보여주게 설정하기

$MainProject>> git config status.submodulesummary 1

 

  • 서브모듈의 log 함께 보기

$MainProject>> git log -p --submodule

 

5. MainProject 에 등록된 submodule 을 pull 하기

  • MainProject를 pull 한 후 다음 명령어를 실행한다.

$MainProject>> git submodule update --init --recursive

 

  • pull, submodule update 를 합쳐서 --recurse-submodules 옵션을 줄 수 있다.

$MainProject>> git pull --recurse-submodules

 

  • config에서 --recurse-submodules 옵션을 default 로 설정하는 방법은 아래와 같다.
$MainProject>> git config submodule.recurse true
  • 위 설정은 clone 을 제외하고 지원된다.

 

6. MainProject 에서 submodule과 함께 push 하기

- MainProject 에 기록된 submodule 이력이 현재 push 된 상태가 아닌체로 push 되면 remote 에서 pull 할 때 해당하는 submodule 의 이력이 존재하지 않아 문제가 생길 수 있다.

이를 방지하기 위해 push할 때 --recurse-submodules=[check | on-demand] 옵션을 붙여서 사용하면

MainProject push 전에 submodule 이력이 push 됐는지 체크해서 fail시키거나(check option),

MainProject push 전에 submodule을 push 한다(on-demand option).

- 위 command를 git config push.recurseSubmodules [check | on-demand] 로 설정해놓을 수 있다.

 

7. MainProject 에 등록된 submodule 이력이 remote와 다를 때 pull 하기

1. 아래 명령어로 pull 해 올 시 결과적으로  conflict 가 날 것이다.

$MainProject>> git pull --recurse-submodules

 

2. MainProject 가 충돌난 건 충돌난 파일을 수정해주면 되며, submodule이 충돌난 건 submodule단에서 merge 처리가 필요하다.

$MainProject>> git diff
diff --cc DbConnector
index eb41d76,c771610..0000000 //eb41d76은 내가 갖고 있는 DbConnector 이력이며, c771610은 remote의 DbConnector 이력이다.

$DbConnector>> git branch try-merge c771610 //현재 작업중인 브랜치는 master 이며, c771610 은 새로운 branch에 따놓는다.
$DbConnector>> git merge try-merge //master에 새로운 브랜치를 병합한다.

- 위 명령어로  merge시 conflit 가 날 것이다. 그럼 충돌 파일을 고친 후 submodule 단에서 commit 해준다.

- 그 후, MainProject 로 돌아가 고친 파일을 commit 해준다.

 

8. Submodule 팁

1. 아래와 같이 submodule foreach 를 이용하여 작업 중인 모든 submodule 에 대하여 한번에 명령어를 실행할 수 있다.

$MainProject>> git submodule foreach 'git stash'
$MainProject>> git submodule foreach 'git checkout -b featureA' //새로운 브랜치 생성 및 switch

- submodule foreach 를 사용하여 MainProject 와 submodule 의 diff를 확인할 수 있다.

$MainProject>> git diff; git submodule foreach 'git diff'

 

2. submodule 을 자주사용할 때 세팅해야 할 alias는 아래와 같다.

$MainProject>> git config alias.sdiff '!'"git diff && git submodule foreach 'git diff'"
$MainProject>> git config alias.spush 'push --recurse-submodules=on-demand'
$MainProject>> git config alias.supdate 'submodule update --remote --merge'

- git config --global 을 통해 전역으로 alias를 설정할 수 있다.

 

개인적 감상

submodule 을 다룰 때 실수할만한 부분이 꽤 많다는 생각이 든다.
개인적으로 git clone --recurse-submodule은 편리한 것 같지만,
특히 push할 때 submodule에 대한 push 없이 MainProject를 push 해버려 이력을 찾지 못하게 하는 경우를 방지하기 위해
config spush 로 설정해두고 주의해서 사용해야 하며,
git config submodule.recurse true를 설정해 놓아 실수를 방지해야 할 것 같다.
블로그 이미지

uchacha

개발자 일지

,