Skip to main content

git

Create#

  • Clone an existing repository
    $ git clone https://github.com/realgrid/base.realgrid.com.git
  • Create a new local repository
    $ git init$ git init new_project

Branchs & Tags#

  • List all existing branches
    $ git branch -av
  • Switch HEAD branch
    $ git checkout <branch>
  • Create a new branch based on your current HEAD
    $ git branch <new-branch>
  • Create a new tracking branch based on a remote branch
    $ git checkout --track <remote/branch>
  • Delete a local branch
    $ git branch -d <branch>
  • Mark the current commit with a tag
    $ git tag <tag-name>

Local changes#

  • Changed files in your working directory
    $ git status
  • Changes to tracked files
    $ git diff
  • Add all current changes to the next commit
    $ git add .
  • Add some changes in <file> to the next commit
    $ git add -p <file>
  • Commit all local changes in tracked files
    $ git commit -a
  • Commit previously staged changes
    $ git commit
  • Change the last commit - Don‘t amend published commits!
    $ git commit --amend

Remote#

  • 리모트 origin url 추가.
    $ git remote add origin <remote_url>
  • 리모트 상세정보 보기
    $ git remote -v
  • 리모트 브랜치 syncing: origin/master - master
    $ git branch --set-upstream-to=origin/master master
  • 리모트 origin url 바꾸기
    $ git remote set-url <new_remote_url>
  • 리모트 origin 지우기
    $ git remote rm origin
  • 리모트 브랜치 삭제
    $ git push origin --delete <branch>

Reset & Revert#

  • 특정 커밋으로 되돌리고 이후의 모든 커밋을 이전상태로 되돌린다.
    $ git reset --hard 4ec7dc
  • 특정 커밋으로 되돌리지만 이후의 모든 작업은 스테이지에 추가된 상태
    $ git reset --soft 4ec7dc
  • 특정 커밋으로 되돌리지만 이후의 모든 작업은 변경된 상태로 남겨둔다.(default)
    $ git reset --mixed 4ec7dc

Submodules#

현재 repository의 내부 디렉토리에 다른 repository를 관리할 수 있다.

예를 들어 여러 서비스 애플리케이션에서 사용하는 공통 패키지를 내가 직접 개발하거나 개발에 참여한다고 할때 패키지 코드를 직접 수정/배포 하면서 동시에 현재 개발중인 서비스 애플리케이션 내에서도 사용할 수 있는 장점이 있다. 하지만, 관리의 어려움이 있다. 신경써서 관리하지 않으면 혼란스럽다.

add#

아래는 현재 애플리케이션 구조에 packages 밑에 forum-to-realdesk 라는 외부 패키지를 submodule로 추가하는 예.

$ git submodule add https://github.com/onlydel/forum-to-realdesk.git packages/forum-to-realdesk
$ tree
.
└── src
...
└── packages
└── forum-to-realdesk
├── README.md
├── forum-migger.js
├── migger.js
└── package.js

init, update#

submodule이 포함된 프로젝트 소스를 clone할 경우 submodule 정보는 별도로 update 해야 한다. 우선 init 명령을 실행하고 update 명령으로 코드를 내려 받는다.

$ git submodule init
$ git submodule update

submodule 삭제#

삭제는 의외로 쉽지 않다. 아래 절차대로 진행 한다. 위에서 추가한 packages/forum-to-realdesk 모듈을 제거해 본다.

  • .gitmodules 파일에 들어 있는 [submodule "packages/forum-to-realdesk"]의 섹션을 삭제한다.
  • git add .gitmodules 로 Staging한다.
  • .git/config 파일에서 [submodule "packages/forum-to-realdesk"] 섹션을 찾아 삭제한다.
  • git rm --cached packages/forum-to-realdesk 명령으로 슈퍼프로젝트에서 해당 index를 삭제해야 한다.
  • rm -rf .git/modules/packages/forum-to-realdesk 명령으로 해당 폴더를 삭제한다.
  • git commit -m 'Removed Submodules' 명령으로 해당 내용을 커밋한다.
  • rm -rf packages/forum-to-realdesk 명령으로 모듈 폴더를 삭제한다.

Submodule 변경시 상태#

submodule directory로 들어가서 별도로 commit하면 submodule directory에 (new commits)로 표시됨. add하고 commit하면 OK.

깃헙에서 fork 한 내용을 original repository와 sync#

  1. https://help.github.com/articles/fork-a-repo/ 참고해서 remote add upstream
  2. https://help.github.com/articles/syncing-a-fork/ 참고해서 syncing fork

fatal: refusing to merge unrelated histories 오류#

리모트로 pull 또는 merge 하는데 fatal: refusing to merge unrelated histories 오류가 발생하면 --allow-unrelated-histories옵션 사용.

$ git pull origin master --allow-unrelated-histories