개요

파일의 상태

git에서는 파일을 Modified, Staged, Commited 세 가지 상태로 관리한다.

Modified

  • 수정만 하고 Stage나 커밋하지 않은 상태

Staged

  • 현재 수정한 파일을 곧 커밋할 것이라고 표시

Commited

  • 파일이 로컬에 안전하게 저장된 상태

untracked 파일은 아에 git 파일로 등록되지 않은 것이고 tracked는 버전 관리를 위해 git에 추가된 파일이다.

사용법

저장소 만들기

새로운 프로젝트를 생성하기 위해 다음 작업을 수행
이미 존재하는 프로젝트의 최상위 디렉토리나 아니면 새로운 디렉토리를 생성하고 해당 디렉토리로 이동하여 다음 명령을 수행한다.

$ git init

변경 내용 확인

프로젝트 내에 변경된 파일이 있거나 상태가 변경된 파일이 있으면 표시해준다.

$ git status

파일 추가

버전관리 될 파일을 추가한다. 이미 버전 관리 되던 파일 중 변경된 파일을 staged 상태로 만드는 역할도 수행.

$ git add test.c

커밋

버전관리 저장소에 변경내용을 저장한다.

$ git commit -m "test.c 소스 파일 생성"

삭제된 파일이 여러개 일 때 한번에 반영하는 방법

$ git add -u       // -u 옵션은 update tracked files
$ commit -m "Comment"
$ git push

리모트 저장소

기본적으로 git는 로컬 환경에서 동작하지만 협업을 위해 서버를 두고 그곳에 변경 내용을 업데이트하거나 다운로드하는 것이 가능하다.

remote 저장소 관련 명령어

git remote add [remote-name] [url] remote 저장소를 등록
git remote rename [from] [to] remote 이름을 변경
git remote rm [remote-name] remote 저장소를 삭제
git fetch [remote-name] 로컬에는 없고 리모트에는 있는 데이터를 모두 가져옴
git pull [remote-name] pull은 fetch도 하고 자동으로 로컬과 머지를 수행
git push [remote-name] [branch-name] 리모트 저장소에 push
$ git remote add origin http://treetale.iptime.org/git/warnus/testprj.git 

remote 서버가 여러 곳이 될 수 있기 때문에 remote-name을 붙여준다. 위 명령에서는 origin이라는 이름의 서버 URL은 http://treetale.iptime.org/git/warnus/testprj.git 이다.

warnus@seedtale:~/git/testprj$ git remote add origin http://treetale.iptime.org/git/warnus/testprj.git
warnus@seedtale:~/git/testprj$ git remote -v
origin	http://treetale.iptime.org/git/warnus/testprj.git (fetch)
origin	http://treetale.iptime.org/git/warnus/testprj.git (push)
warnus@seedtale:~/git/testprj$ git push -u origin master
Username for 'http://treetale.iptime.org': warnus
Password for 'http://warnus@treetale.iptime.org': 
To http://treetale.iptime.org/git/warnus/testprj.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.
warnus@seedtale:~/git/testprj$ git push --help

push할 때에 origin master 라는 의미는 origin server(앞서 이름 붙였던 URL)의 master branch에 push를 할 거라는 의미이다.

Git log

lg1 = log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
lg2 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n''          %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all
lg = !"git lg1"

태그

태그는 일반적으로 버전을 붙여 릴리즈 용도로 사용하기 위해 이름을 붙이는 것으로 v1.0과 같이 사용한다.

git tag 태그 조회
git tag [tag-name] ([commit-ID]) 태그 달기
git tag -a [tag-name] -m [“comment”] 태그를 달고 설명을 붙인다.
git show [tag-name] 태그 정보 확인
git push origin v1.5 remote 저장소에 태그를 push
git push origin –tags 여러 태그를 한 번에 push

기타 명령어

git rm [file-name] 파일 삭제
git mv [file-from] [file-to] 파일이름 변경
git commit –amend 커밋 수정
git reset HEAD [file-name] staged 상태(git add한 파일) 파일을 unstaged로 변경
git checkout [file-name] 수정된 파일을 되돌림
git blame [file-name] line by로 어떤 커밋에서 누가 수정했는지 확인 가능

git 설정

git config color.ui true 콘솔에서 git output을 컬러로 출력
git config format.pretty oneline log 확인 시 한줄로 표시
git config user.name “이름” git 사용자 이름 설정
git config user.email (email address) git 사용자 e-mail 설정

한방에 테스트 해보는 예제

$ mkdir newprj
$ cd newprj
$ git init                         // git 로컬 저장소 생성
$ touch file1, file2, file3        
$ git status                       // 변경 이력 확인
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	file1
#	file2
#	file3
nothing added to commit but untracked files present (use "git add" to track)
$ git add .
$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   file1
#	new file:   file2
#	new file:   file3
#
$ git commit -m "Added file1, file2, file3"
$ git remote add origin http://treetale.iptime.org/git/warnus/newprj.git
$ git push origin master
$ touch file4
$ git add .
$ git commit -m "Added file4"
$ git push 

$ git branch testdev
$ git branch
* master
  testdev
$ git checkout testdev
$ git branch
  master
* testdev
$ touch branchfile1
$ git add .
$ git commit -m "Added branchfile1"
$ git push origin testdev

$ git log
$ git checkout master
$ git log
$ touch masterfile1
$ git add .
$ git commit -m "Added masterfile1"
$ git push

$ git merge master testdev
$ git push

Git Tips

git 변경된 파일만 따로 뽑기

$ git add -AN
$ cp -pv --parents `git diff --name-only` DESTINATION-DIRECTORY

참고