깃의 상태
특정 사용자에게만 적용되는 설정 파일 위치 (git config --global)
~/.gitconfig
~/.config/git/config
설정 방법
$ git config --global user.name "d2h10s"
$ git config --global user.email "ecanus9@gmail.com"
$ git config --global core.editor code
$ git config --list // 설정값 전체 확인
$ git config user.name // 특정 설정값 확인
도움말 보기
$ git help <verb>
$ git <verb> --help
$ man git-<verb>
기존 디렉토리를 깃 저장소로 만들기
$ git init
기존 저장소 clone하기
$ git clone https://github.com/d2h10s/git_test
기존 저장소를 새로운 폴더 이름으로 clone하기
$ git clone https://github.com/d2h10s/git_test mygit
파일의 상태 확인하기
$ git status
$ git status -s
$ git status --short
리눅스에서 간단히 새 파일 만들기
$ echo "파일에 들어갈 텍스트" > "파일이름"
리눅스 cat 명령어
$ cat "파일이름" // 파일 내용을 볼 수 있다.
$ cat > "파일이름" // 파일을 원하는 내용으로 생성한다.
$ cat >> "파일이름" // 파일에 이어서 글을 쓸 수 있다.
파일을 새로 추적하기
$ git add README
.gitignore
$ cat .gitignore
*.[oa] // .a 혹은 .o로 끝나는 파일은 무시
*~ // ~로 끝나는 파일은 무시
!lib.a // .a로 끝나는 파일은 무시하지만 lib.a는 무시하지 않는다.
/TODO // 현재 디렉토리에 있는 TODO 파일만 무시
BUILD/ // BUILD/ 디렉토리에 있는 모든 파일은 무시
doc/*.txt // doc 디렉토리의 .txt 파일만 무시
doc/**/*.txt // doc 디렉토리 아래의 모든 .txt 파일을 무시
git diff는 워킹 디렉토리에 있는 것과 Staging Area에 있는 것을 비교한다. 그래서 수정하고 아직 staged하지 않는 것을 보여준다.
만약 수정한 파일을 모두 Staging Area에 넣었다면 git diff 명령은 아무것도 출력하지 않는다.
$ git diff
$ git diff --cached // staged 상태인 파일 확인
$ git diff --staged
변경사항 커밋하기
Git에 설정된 지정한 편집기가 실행되고 커밋 메세지를 추가할 수 있다.
$ git commit // 편집기가 열리고 메세지를 입력한다.
$ git commit -v // 편집기에 diff 메세지가 추가된다.
$ git commit -m // 인라인으로 메세지를 첨부할 수 있다.
$ git commit -a // Tracked 상태의 파일을 자동으로 넣는다.
파일 삭제하기
$ git rm README // 워킹 디렉토리와 스테이지 같이 삭제
$ git rm --cached README // 스테이지 에서만 삭제
$ git rm log/\*/.log // log/ 디렉토리에 있는 .log 파일 모두 삭제
$ git rm \*~ // ~로 끝나는 파일을 모두 삭제
파일 이름 변경하기
$ git mv from to
커밋 히스토리 조회하기
$ git log
$ git log -p // 각 커밋의 diff 결과를 보기
$ git log -p -2 // 최근 두개의 결과만 보기
$ git log --stat // 통계와 요약 정보 확인
$ git log --pretty=oneline // short, full, fuller
$ git log --pretty=format:"%h - %an, %ar : %s" // 결과를 파싱
$ git log --since=2.weeks // 지난 2주 동안 만들어진 커밋만 조회
$ git log --grep "커밋 메세지" // 특정 메세지 검색
$ git log -S "funcion_name)// 특정 함수가 추가/제거 된 기록 조회
되돌리기
$ git commit -m "initial commit" // 커밋을 한다.
git add forgotten_file // 파일 하나를 빼먹어서 추가한다.
$ git commit --amend // 이전 커밋을 덮어씌운다.
Staging Area에 잘못 올린 파일 다시 Working Directory로 옮기기
$ git HEAD "file_name"
$ git add *
$ git status -s
A file1.txt
A file2.txt
?? .gitignore
$ git reset HEAD file2.txt
$ git status -s
A file1.txt
?? .gitignore
?? file2.txt
특정 Modified 파일을 이전 커밋 상태로 되돌리기
$ git checkout -- "file_name"
현재 리모트 저장소의 단축이름을 표시
$ git remote
origin
단축이름과 url 같이 확인
$ git remote -v
origin https://github.com/d2h10s/git_test (fetch)
origin https://github.com/d2h10s/git_test (push)
로컬에는 없지만 리모트 저장소에 있는 데이터를 모두 가져오기
$ git fetch [remote-name]
fetch의 기능 + 자동 Merge
$ git pull [remote-name]
Upstream 저장소에 Push
$ git push [리모트 저장소 이름] [브랜치 이름]
리모트 저장소 이름 변경
$ git remote rename [이전이름] [나중이름]
리모트 저장소 삭제
$ git remote rm [리모트 저장소 이름]
태그의 종류
이미 만들어진 태그 확인
$ git tag
v0.1
v0.3
검색 패턴 사용
$ git tag -l 'v1.8.5*'
Annotated tag
$ git tag -a v1.4 -m "my version 1.4"
태그 정보와 커밋 정보 확인
$ git show v1.4
Lightweight tag
$ git tag v1.4
이전 커밋에 대해서 태그하기
$ git log --pretty=oneline
852ac355d8da8c50b4c9875ae9c0ee2aa58eb360 (HEAD -> main, tag: v0.1, origin/main, origin/HEAD) new
6aa0a445221e2b8931ab89a16a6a13e0cfbfceeb new
bd12c6b5419fc60956aa4127ea53b42f96f942cd new
66c7d0fb5c709f775fcad072f691a3337e2c641b new
9192db6199bd898adbb447e93f00dff4f6470a78 test
cd9ac13b5d2d1532e1ce8d51520ca97fff89fafa delete
7dcb89fa07fe198d56e51726fe2536ee466b0970 new
0f755f21b52a8db8ee036555cd63269c1ee591e8 test
d0fa42a38b9d8c8f780d9b4c4d6c356d83e4bf57 test
65296605f8af4010d1b3737009a260fbb5bb997f new
46fe854f3441ec59938a33df2cbb5c73910882c1 new files
8ece63e5554d0a48e83e1957e7e3bb9d2b7d7eb8 Initial commit
git tag 뒤에 체크섬을 명시한다.
$ git tag -a v0.0 8ece63
git push는 자동으로 태그를 서버로 전송하지 않는다. 태그를 만들었으면 서버에 별도로 Push 해야한다.
$ git push origin v1.5
여러개의 태그를 동시에 푸시
$ git push roigin --tags
특정 태그를 기준으로 새로운 브랜치 만들기
$ git checkout -b version2 v2.0.0
별명 등록
$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status
$ git config --global alias.unstage 'reset HEAD --'
$ git config --global alias.last 'log -1 HEAD'
$ git config --global alias.visual '!gitk' // 외부 명령을 실행하는 커스텀 스크립트
새로운 브랜치 생성
$ git branch testing
HEAD가 가리키는 브랜치 변경
$ git checkout testing
브랜치를 만들고 각각 커밋하게 되면 분기된다.
그래프로 분기 확인
$ git log --oneline --decorate --graph --all
* 366069c (HEAD -> testing, origin/testing) my
| * 6c8579a (origin/main, origin/HEAD, main) new
|/
* 852ac35 (tag: v0.1) new
* 6aa0a44 new
* bd12c6b new
* 66c7d0f new
* 9192db6 test
* cd9ac13 delete
* 7dcb89f new
* 0f755f2 test
* d0fa42a test
* 6529660 new
* 46fe854 new files
* 8ece63e (tag: v0.0) Initial commit
브랜치를 만들면서 체크아웃 동시에 하기
$ git checkout -b iss53
브랜치를 다른 브랜치에 합치기
$ git checkout master
$ git merge hotfix
Updating f42c576..3a0874c
Fast-forward
index.html | 2 ++
1 file changed, 2 insertions(+)
브랜치 삭제
$ git branch -d [브랜치 이름]