Git 저장소 생성
$mkdir hello-git
- git 저장소 생성
$git init
/*성공시 ls하면 .git 디렉토리 생성*/
Git의 버전관리

- working tree에서 소스코드를 생성하고 작성한다.
- add 명령어 호출시 stage 영역에 파일이 올라간다.
- stage 영역에 있는 파일은 commit시 Repository에 올라가며 새로운 버전이 생성된다.
git status 저장소 상태 & git add
$ git status
- 생성된 파일이 있는 경우
-- Utracted file : stage에 없는 경우 -> add하면 Tracted file이다.
$ vim inha.txt
$ git status
On branch master
No commits yet
Untracked files:
noting added to commit but untracked files present
$ git add inha.txt
$ git status
On branch yet
Changes to be committed:
commit 저장소에 저장
- commit하는 명령어로 stage에 있는 파일들의 새로운 버전을 생성한다.
- -m 옵션을 이용하여 메시지를 기록할 수 있다.
$ git commit -m "create inha.txt"
$ git status
On branch master
nothing to commit, working tree clean
git-log 버전 이력 확인
git diff 변경된 내용
- 현재 working tree 파일과 최신 버전의 차이를 확인 가능하다.

stage에서 제거
$ git reset HEAD "inha.txt"
git checkout
$ git checkout -- "inha.txt"
commit한 내용을 돌리고 싶음
$ git reset ^HEAD
$ git reset --hard 특정_commit_hash
stage 공간이 필요한 이유
- 수정이 완료된 파일과 진행중인 파일이 있을 경우 특정 파일만 commit이 가능하다.
- commit 전에 코드를 리뷰하거나 테스트하기가 좋다.
branch
$ git init
$ vim inta.txt
$ git add inha.txt
$ git commit -m "first commit"
branch 생성
- $ git branch 브랜치명
- $ git bracnh : 생성된 브랜치들 확인, *표시인것이 현재 브랜치
$ git branch issue1
$ git branch
branch 전환
//파일 변경
$ git checkout issue1 //해당 브랜치로 이동
$ git add inha.txt
$ git commit -m "내용 변경"
- branch master와 issue1은 다른 로그를 갖는다.
branch 병합
- 병합을 위하여 master branch에서 진행하여야한다.
- issue1 에서 수정한 파일내용과 master 파일의 내용이 합해진다.
$ git checkout master
$ git merge issue1
branch 삭제
$ git branch -d issue1
$ git branch
$ git log //issue1의 로그도 추가한 것을 확인할 수 있다.
여러 브랜치 이용
$ git branch issue2
$ git branch issue3
$ git log --graph --all
$ git checkout master
$ git merge issue2
$ git merge issue3
- issue2와 issue3가 같은 파일을 수정하여서 병합시 충돌이 발생하여 자동병합을 실패한다.
병합 및 충돌 해결
간단한 git 명령어
add : 병경된 파일 stage 추가
<<<<<<<<<< HEAD //현재 HEAD(master)에서는 commit의 설명이 존재
commit : stage에 추가된 파일 커밋
====== //issue3에서는 pull에 대한 설명있다는 표시
pull : 원격저장소 내용 가져오기
>>>>>>>>>>> issue3
- 충돌은 사람이 직접 수정해야한다.
- 그 이상한 줄 다 지우고 commit해서 충돌을 해결한다.
원격 저장소 연결
$ git init loc-git
$ cd loc-git
$ cim inha.txt
$ git add inha.txt
$ git commit -m "add inha.txt"
원격저장소 연결
- git remote add origin 원격저장소_주소
- origin이란 기본으로 지정된 원격저장소명이다.
- -v 옵션
- fetch : 원격 저장소의 메타데이터 정보를 확인하는 연결
- push : 로컬에서 원격저장소로 저장하는 연결
- 상세한 url 보여준다.
$ git remote add origin 주소
$ git remote //현재 추가된 원격 저장소 파악이 가능하다
$ git remote -v
push 원격저장소로 보내기
- -u 옵션 : 한번만 쓰면 계속 원격-로컬 저장소에 대해 push를 진행하도록 한다. 이제 push만 입력해도된다.
$ git push -u origin(원격저장소) master(로컬브랜치)
$ git push
pull 원격저장소로부터 가져오기
- 현재 HEAD인 브랜치에 해당 브랜치를 가져온다.
$ git pull origin(원격저장소) master(원격저장소의_브랜치)