Skip to main content

Git 기본 안내


설치

sudo apt install git
git config --global user.name "Hyeonki Hong"
git config --global user.email "[email protected]"
  • 회사 아이디, 개인 아이디 등 여러 email을 사용하는 경우 --global 대신 각 리포지토리에서 --local 옵션으로 설정하면 됩니다.
git config --global core.autocrlf input
git config --global core.eol lf \
&& git config --global core.editor vim \
&& git config --global credential.helper 'cache --timeout=86400'

clone

git clone <repository>
git clone <repository> -b <tag or branch> --single-branch
git clone <repository> -b <tag or branch> --depth <# of commit>

add, rm, mv, commit

add

git add 명령어는 Untracked file, Modified file 을 Staging area에 반영합니다.

git add <file>
git add .

rm

git rm 명령어는 Tracked file을 Untracked file로 변경하고 Staging area에 삭제된 상태라는 것을 반영합니다.

git rm <file>

파일이 변경되어 Staging area에 있는 경우 -f 옵션을 사용해 강제 삭제 해야합니다.

git rm -f <file>

mv

git mv 명령어는 파일의 위치를 변경하거나 이름을 변경을 한 후 Staging area에 반영합니다.

git mv <file> <newName|directory>

commit

git commit 명령어는 Staging area에서 파일을 제외시키고, Unmodified file로 변경한 후 commit된 하나의 작업으로 등록합니다.

git commit
git commit -m "<message>"

log

log with commit message

git log [-<# of commit>] [directory or file]

log with commit message + diff

git log -p [-<# of commit>] [directory or file]

log with commit message + statistic

git log --stat [-# of commit] [directory or file]

log with oneline commit message

git log --oneline [--graph] [-# of commit] [directory or file]
git log --pretty=format:"[format]" [--graph] [-# of commit] [directory or file]

reflog

git reflog 명령어로 git 변경 이력을 확인할 수 있습니다.

git reflog

commit hash && HEAD

commit hash는 특정 커밋을 가리키는 7f0e7030d57d469d150c17eb4746872e415fb379와 같은 해시값입니다.

커밋을 가리킬때 해시값의 앞에서부터 7자리 정도만 사용해도 구별이 되기 때문에 값 전체를 사용할 필요는 없습니다.

HEAD라는 키워드 조합으로 현재 커밋 기준으로 상대적 위치의 커밋을 가리킬 수도 있는데 규칙은 아래와 같습니다.

reflog에 따른 상대적인 커밋 위치는 HEAD@{#}으로 가리킬 수 있습니다. # 대신 숫자를 적으시면 됩니다.

reset

git reset --[soft|mixed|hard] <commit hash>
  • soft: index는 유지하고, HEAD만 해당 커밋으로 되돌립니다.
  • mixed: index, HEAD를 해당 커밋으로 되돌립니다. 파일 수정 내용은 남아있습니다.
  • hard: 해당 커밋 시점으로 완전히 되돌립니다.

remote

git remote update --prune