└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # git-usage 2 | 자주 사용하는 깃 명령어 모음 3 | 4 | 5 | ## 구조 6 | 7 | 코드는 아래 세 단계에 걸쳐 저장된다. 8 | 9 | 스테이징 -> 커밋 -> 원격저장소 10 | 11 | 1. git add {파일명} 으로 파일을 스테이징 상태에 넣는다. 12 | 2. git commit 으로 스테이징 상태에 있는 모든 변경사항을 커밋한다. 여기까지가 로컬에서의 작업 13 | 3. git push 로 커밋된 저장소를 원격 저장소로 밀어넣는다. 14 | 15 | 16 | ## 기본 명령어 17 | 18 | 저장소 생성 19 | 20 | ``` 21 | git init 22 | ``` 23 | 24 | 원격 저장소로부터 복제 25 | 26 | ``` 27 | git clone {url} 28 | ``` 29 | 30 | 변경 사항 체크 31 | 32 | ``` 33 | git status // 34 | ``` 35 | 36 | 특정 파일 스테이징 37 | 38 | ``` 39 | git add {파일명} 40 | ``` 41 | 42 | 변경된 모든 파일 스테이징 43 | 44 | ``` 45 | git add * 46 | ``` 47 | 48 | 커밋 49 | 50 | ``` 51 | git commit -m “{변경 내용}” 52 | ``` 53 | 54 | 원격으로 보내기 55 | 56 | ``` 57 | git push origin master 58 | ``` 59 | 60 | 원격저장소 추가 61 | 62 | ``` 63 | git remote add origin {원격서버주소} 64 | ``` 65 | 66 | 참고 페이지 67 | 68 | - download(osx): http://code.google.com/p/git-osx-installer/downloads/list 69 | - download(windows): http://git-scm.com/download/win 70 | - 설치 메뉴얼: http://blog.outsider.ne.kr/389 71 | - 사용 메뉴얼:http://dogfeet.github.io/articles/2012/how-to-github.html 72 | - git 간편 안내서: http://rogerdudler.github.com/git-guide/index.ko.html 73 | - 한장으로 핵심 기능만: http://rogerdudler.github.com/git-guide/files/git_cheat_sheet.pdf 74 | 75 | 76 | ## Commit 77 | 78 | 커밋 합치기 79 | 80 | ``` 81 | git rebase -i HEAD~4 // 최신 4개의 커밋을 하나로 합치기 82 | ``` 83 | 84 | 커밋 메세지 수정 85 | 86 | ``` 87 | $ git commit --amend // 마지막 커밋메세지 수정(ref) 88 | ``` 89 | 90 | 간단한 commit방법 91 | 92 | ``` 93 | $ git add {변경한 파일병} 94 | $ git commit -m “{변경 내용}" 95 | ``` 96 | 97 | 커밋 이력 확인 98 | 99 | ``` 100 | $ git log // 모든 커밋로그 확인 101 | $ git log -3 // 최근 3개 커밋로그 확인 102 | $ git log --pretty=oneline // 각 커밋을 한 줄로 표시 103 | $ git reflog // reset 혹은 rebase로 없어진 과거의 커밋 이력 확인 104 | ``` 105 | 106 | 커밋 취소 107 | 108 | ``` 109 | $ git reset HEAD^ // 마지막 커밋 삭제 110 | $ git reset --hard HEAD // 마지막 커밋 상태로 되돌림 111 | $ git reset HEAD * // 스테이징을 언스테이징으로 변경, ref 112 | ``` 113 | 114 | 115 | ## Branch 116 | 117 | master 브랜치를 특정 커밋으로 옮기기 118 | 119 | ``` 120 | git checkout better_branch 121 | git merge --strategy=ours master # keep the content of this branch, but record a merge 122 | git checkout master 123 | git merge better_branch # fast-forward master up to the merge 124 | ``` 125 | 126 | 브랜치 목록 127 | 128 | ``` 129 | $ git branch // 로컬 130 | $ git branch -r // 리모트 131 | $ git branch -a // 로컬, 리모트 포함된 모든 브랜치 보기 132 | ``` 133 | 134 | 브랜치 생성 135 | 136 | ``` 137 | git branch new master // master -> new 브랜치 생성 138 | git push origin new // new 브랜치를 리모트로 보내기 139 | ``` 140 | 141 | 브랜치 삭제 142 | 143 | ``` 144 | git branch -D {삭제할 브랜치 명} // local 145 | git push origin :{the_remote_branch} // remote 146 | ``` 147 | 148 | 빈 브랜치 생성 149 | 150 | ``` 151 | $ git checkout --orphan {새로운 브랜치 명} 152 | $ git commit -a // 커밋해야 새로운 브랜치 생성됨 153 | $ git checkout -b new-branch // 브랜치 생성과 동시에 체크아웃 154 | ``` 155 | 156 | 리모트 브랜치 가져오기 157 | 158 | ``` 159 | $ git checkout -t origin/{가져올 브랜치명} // ref 160 | ``` 161 | 162 | 브랜치 이름 변경 163 | 164 | ``` 165 | $ git branch -m {new name} // ref 166 | ``` 167 | 168 | 169 | ## Tag 170 | 171 | 172 | 태그 생성 173 | 174 | ``` 175 | git tag -a {tag name} -m {tag message} {commit hash} 176 | git tag {tag name} {tag name} -f -m "{new message}" // Edit tag message 177 | ``` 178 | 179 | 태그 삭제 180 | 181 | ``` 182 | git tag -d {tag name} 183 | git push origin :tags/{tag name} // remote 184 | ``` 185 | 186 | 태그 푸시 187 | 188 | ``` 189 | git push origin --tags 190 | git push origin {tag name} 191 | git push --tags 192 | ``` 193 | 194 | 195 | ## 기타 196 | 197 | 파일 삭제 198 | 199 | ``` 200 | git rm --cached --ignore-unmatch [삭제할 파일명] 201 | ``` 202 | 203 | 히스토리 삭제 204 | 205 | - 목적: 패스워드, 아이디 같은 비공개 정보가 담긴 파일을 실수로 올렸을 때 삭제하는 방법이다. (history에서도 해당 파일만 삭제) 206 | 207 | ``` 208 | $ git clone [url] # 소스 다운로드 209 | $ cd [foler_name] # 해당 폴더 이동 210 | $ git filter-branch --index-filter 'git rm --cached --ignore-unmatch [삭제할 파일명]' --prune-empty -- --all # 모든 히스토리에서 해당 파일 삭제 211 | $ git push origin master --force # 서버로 전송 212 | ``` 213 | 214 | 히스토리에서 폴더 삭제: 215 | 216 | ``` 217 | git filter-branch --tree-filter 'rm -rf vendor/gems' HEAD 218 | ``` 219 | 220 | 리모트 주소 추가하여 로컬에 싱크하기 221 | 222 | ``` 223 | $ git remote add upstream {리모트 주소} 224 | $ git pull upstream {브랜치명} 225 | ``` 226 | 227 | 최적화 228 | 229 | ``` 230 | $ git gc 231 | $ git gc --aggressive 232 | ``` 233 | 234 | ## 서버 설정 235 | 236 | 강제 푸시 설정 237 | 238 | ``` 239 | git config receive.denynonfastforwards false 240 | ``` 241 | 242 | ## Alias 243 | 244 | ~/.gitconfig 파일을 설정하여 깃 명령어의 앨리어스를 지정할 수 있다. 245 | 246 | ~/.gitconfig > alias 부분: 247 | 248 | ``` 249 | 250 | [alias] 251 | br = branch 252 | co = checkout 253 | rb = rebase 254 | st = status 255 | cm = commit 256 | pl = pull 257 | ps = push 258 | lg = log --graph --abbrev-commit --decorate --format=format:'%C(cyan)%h%C(reset) - %C(green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(yellow)%d%C(reset)' --all 259 | ad = add 260 | tg = tag 261 | df = diff 262 | ``` 263 | --------------------------------------------------------------------------------