#title Git & Tips [[TableOfContents]] === 일반 === ==== 알아두면 좋은 기능 ==== ===== alias g='git' ===== 예) .bashrc 에 alias 기록 alias gs='git status -s' alias gp='git pull' ===== stashing ===== 현재 브랜치에서 작업중인 내용을 임시로 저장하고 다른 브랜치로 이동할 때 사용한다. ===== blame ===== 파일 라인 단위로 누가 마지막으로 수정했는지 보여준다. http://git-scm.com/docs/git-blame ==== 작업의 취소 ==== http://www.evernote.com/l/AMKuBFPL-uZItrAAGvEbqooTf52dvpkR1rQ/ ==== push의 취소 ==== http://whiteship.me/?p=13516 === commit === ==== commit 메시지 수정 ==== ===== 마지막 커밋을 수정하기 ===== [[Code($ git commit --amend)]] 마지막 커밋을 수정하는 방법은 매우 간단하다. 이 명령으로 텍스트 편집기가 열리고 메시지를 수정하면 된다. ===== 커밋 메시지를 여러 개 수정하기 ===== 예를 들어 마지막 3번째에 있는 커밋 로그를 수정하고 싶다면 rebase -i를 사용해서 수정해야 한다. 주의할 점은 이미 서버에 push된 커밋은 SHA-1 값이 바뀌기 때문에 가능하면 수정하지 말아야 한다. 만약 rebase에 익숙하지 않다면 다음을 주의 깊게 봐야 한다. https://git-scm.com/book/ko/v1/Git-%EB%8F%84%EA%B5%AC-%ED%9E%88%EC%8A%A4%ED%86%A0%EB%A6%AC-%EB%8B%A8%EC%9E%A5%ED%95%98%EA%B8%B0 [[Code($ git rebase -i HEAD~3)]] 텍스트 편집기가 열리면 다음 같은 커밋 목록이 첨부되어 있다. {{{ pick ... pick ... pick ... }}} 여기서 커밋 목록 순서는 위쪽이 오래된 것이다. 마지막 3번째 커밋 메시지를 수정하려면 pick을 edit로 변경하고 저장 및 종료한다. {{{ edit ... pick ... pick ... }}} 저장하고 편집기를 종료하면 Git은 목록에 있는 커밋 중에서 가장 오래된 커밋으로 이동하고, 아래와 같이 다음은 어떻게 해야하는지 메시지를 보여준다. {{{ $ git rebase -i HEAD~3 Stopped at 7482e0d... updated the gemspec to hopefully work better You can amend the commit now, with git commit --amend Once you’re satisfied with your changes, run git rebase --continue }}} 마지막 커밋 메시지를 변경했을 때와 같이 [[code(git commit --amend)]] 으로 메시지를 변경하고 [[code(git rebase --continue)]]로 rebase를 계속한다. 이것을 반복하면 어떤 위치의 커밋 메시지도 수정할 수 있다. 다시 말하지만 주의할 점은 이미 서버에 push된 커밋은 rebase를 하면 SHA-1 값이 바뀌기 때문에 동료 개발자들을 혼란스럽게 할 가능성이 있으며 가능하면 수정하지 말아야 한다. === log === ==== commit 메시지의 첫 번째 줄만 출력 ==== [[code(git log --pretty=short)]] ==== 파일의 변경된 내용까지 출력 ==== [[code(git log -p)]] ==== 브랜치를 시각적으로 확인 ==== [[code(git log --graph)]] === branch === ==== 브랜치 확인 ==== 현재 브랜치 [[code(git branch)]] 모든 브랜치 [[code(git branch -a)]] ==== 브랜치를 만들고 변경 ==== 예) hotfix-A 브랜치로 변경 [[code(git checkout -b hotfix-A)]] [[code(checkout -b)]]는 [[code(git branch hotfix-A)]] 브랜치를 만들고, [[code(git branch checkout hotfix-A)]] hotfix-A 브랜치로 이동하는 것과 동일함. ==== 브랜치 제거 ==== [[code(git branch -D hotfix-A)]] === merge === ==== 머지하는 것을 기록으로 남김 ==== 예) hotfix-A 브랜치에서 master 브랜치로 이동하여 master에 hotfix-A 브랜치를 merge. [[code(git checkout master)]] 마스터로 이동 [[code(git merge --no-ff hotfix-A)]] [[code(--no-ff)]] 옵션을 주면 merge commit 메시지 작성을 위한 에디터가 실행됨. {{{ Merge Branch 'hotfix-A' # Please enter a commit message to explain why this merge is necessary, # ... }}} ==== 머지 conflict 해결 ==== 예) 1. 충돌 발생 {{{ <<<<<<< HEAD ... ======= ... >>>>>>> hotfix-A }}} 2. ===, <<<, >>> 표시를 삭제하고 충돌문제를 해결한 후 저장한다. 3. 문제를 해결하고 commit 한다. [[code(git commit -m 'Fix conflict')]] 4. 다시 merge 한다. === 기타 === ==== cherry-pick 중 conflicts 해결 ==== 1. conflict 발생 파일 수정 2. Merge 상태 제거(reset) 후 add stash 하는 방법도 있지만 이 방법이 가장 쉬운 방법이다. [[code(git reset [filename])]] [[code(git add [filename])]] ==== CRLF 문제 ==== https://www.lesstif.com/pages/viewpage.action?pageId=20776404 2줄 요약 * CORE.EOL * {{{{color:red}core.eol = native}}} 기본 설정. 시스템에서 line ending 을 처리하는 방법에 따른다. windows에서는 CRLF 를 사용하고 Linux, OS X 는 LF 만 사용한다. * {{{{color:red}core.eol = crlf}}} CRLF 를 line ending 으로 사용한다. * {{{{color:red}core.eol = lf}}} LF를 line ending 으로 사용한다. * CORE.AUTOCRLF * {{{{color:red}core.autocrlf = false}}} 기본 설정이다. 파일에 CRLF 를 썼든 LF 를 썼든 git 은 상관하지 않고 파일 그대로 checkin, checkout 한다. 이 설정은 line ending 이 다른 OS 에서는 text file 이 변경되었다고 나오므로 위에서 언급한 여러 가지 문제가 발생할 수 있다. * {{{{color:red}core.autocrlf = true}}} text file을 object database 에 넣기전에 CRLF 를 LF 로 변경한다. * {{{{color:red}core.autocrlf = input}}} LF를 line ending 으로 사용한다. {{{ ## 설정 $ git config --global core.eol native ## 설정 확인 $ git config --global --list|grep core.eol }}} ==== git config 순서 및 위치 ==== {{{ $ git config --system ## /etc/gitconfig $ git config --global ## ~/.gitconfig $ git config --local ## repo/.gitconfig }}} 각 설정 파일에 중복된 설정이 있으면 system부터 순서대로 덮어 씌운다. 즉, git은 가장 먼저 [[code(/etc/gitconfig)]]을 찾고, 다음으로 [[code(~/.gitconfig)]] 순서로 찾는다. ==== diff 패치 생성 및 적용 ==== {{{ git diff --no-prefix > patchfile # 패치 생성 cd path/to/top/ # 이동 patch -p0 < patchfile # 적용 }}} --no-prefix 옵션 없이 생성된 patch 파일이 있다면, {{{ patch -p1 < patchfile }}} ==== .gitignore ==== ===== Visual Studio ===== https://github.com/github/gitignore/blob/master/VisualStudio.gitignore (* [[code(.gitignore)]]는 확장자가 아니다. [[code(.gitignore)]] 파일만 적용 된다.) ==== Emoji for GitHub ==== http://www.emoji-cheat-sheet.com/ Emoji emoticons listed on this page are supported on Campfire, GitHub, Basecamp, Redbooth, Trac, Flowdock, Sprint.ly, Kandan, Textbox.io, Kippt, Redmine, JabbR, Trello, Hall, Plug.dj, Qiita, Zendesk, Ruby China, Grove, Idobata, NodeBB Forums, Slack, Streamup, OrganisedMinds, Hackpad, Cryptbin, Kato, Reportedly, Cheerful Ghost, IRCCloud, Dashcube, MyVideoGameList, Subrosa, Sococo, Quip, And Bang, Bonusly, Discourse, Ello, Twemoji Awesome, Got Chosen, Flow, ReadMe.io, esa, DBook, Groups.io, TeamworkChat, Damn Bugs, Let's Chat, Buildkite, ChatGrape, Dokuwiki, Usersnap, Discord, Status Hero, Bitbucket, Gitter, and YouTube. ==== GUI Clients ==== https://git-scm.com/downloads/guis/ ---- CategoryDev