2011-01-04

Git Tips

git ignore

http://github.com/guides/ignore-for-git

pull 할 때 다음 에러가 뜰 경우

You asked me to pull without telling me which branch you
want to merge with, and ‘branch.master.merge’ in
your configuration file does not tell me either. Please
specify which branch you want to merge on the command line and
try again (e.g. ‘git pull ‘).
See git-pull(1) for details.

If you often merge with the same branch, you may want to
configure the following variables in your configuration
file:

branch.master.remote =
branch.master.merge =
remote..url =
remote..fetch =

See git-config(1) for details.

git config -l 로 살펴보면 이미 remote 들은 등록이 되어 있다. 여기서 할 일은 branch master 를 설정하는 거다.

git config branch.master.remote origin
git config branch.master.merge refs/heads/master

이걸 해주면 된다. via http://stackoverflow.com/questions/2335343/how-to-tell-git-to-always-pull-the-master-branch

작업한 모든 파일을 버리고 이전 버전으로 되돌리고 싶을 때

git reset --hard HEAD

변경된 모든 파일을 HEAD 버전으로 복구한다.

git reset --hard origin/master

변경 사항을 모두 버리고 리모트 브랜치의 내용으로 복구한다.

git clean -f

새로 만들어졌지만 아직 stage 상태가 아닌 파일들을 삭제한다.

이미 커밋한 내용을 수정하고 싶은 경우

git commit --amend

이전 커밋 로그를 변경하거나, 이번에 바꾼 파일을 이전 커밋으로 합쳐버릴 때 사용한다.

git revert HEAD

가장 마지막 커밋(HEAD)을 취소하고 되돌린다. HEAD^2, HEAD~3

git revert HEAD^

마지막 커밋의 바로 이전 커밋(즉 HEAD+1)으로 되돌린다.

git revert -m 1 {commit}

파일이나 폴더를 버전관리하고 싶지 않을 때

.gitignore 에 파일 이름을 등록한다.

#exclude folder
TEMP/*
#include file in exclude folder
!TEMP/include_me

이미 추가된 파일이나 폴더를 빼고 싶은 경우

git rm --cached {FILE}
git rm --cached -r {DIR}

특정 폴더를 실제로 지우진 않고, 버전관리에서만 삭제한다. 이때 .gitignore 에 추가해둬야만 다른 사람이 하위 파일을 수정한 경우에 또다시 추가되는 걸 막을 수 있다.

git ignore 는 아직 커밋되지 않은 파일을 무시할 때 사용한다.

git update-index assume-unchanged FILENAME 은 커밋된 파일을 더이상 추적하지 않을때 사용한다.

.gitignore 는 현재 폴더 기준으로 특정 패턴을 무시한다.

.git/info/exclude 는 글로벌 패턴을 등록한다. 다만 양쪽 모두 웬지 레파지토리에 등록되어 공유되지는 않는 느낌이란 말이지

파일이 너무 많아서 무엇을 커밋할 지 알기 힘들 때

git add -i

인터랙티브 모드를 이용해서 변경된 파일을 stage 상태로 만든다.

최근 N 개의 커밋을 하나로 만들려면

git rebase -i HEAD~N

으로 하면 커밋 편집기가 뜬다. 이때 삭제하고픈 커밋 앞에 pick 대신 squash (또는 s)를 써 넣으면, 최종적으로 커밋 로그 입력모드로 들어간다. 단,

  • rebase 를 취소하고 싶으면 q! 대신 모든 라인을 지워야 된다.
  • 이미 외부 서버로 push 되었을 경우엔 rebase 하면 인생이 꼬인다.

브랜치끼리 병합하되, 커밋은 하고 싶지 않은 경우

git merge master..new_branch --no-commit --no-ff


comments powered by Disqus