noodle-dev 2020. 1. 20. 22:33

GIT

GIT은 분산버전관리시스템(DVCS)이다.

소스코드의 버전 및 이력을 관리할 수 있다.

준비하기

윈도우에서 git을 활용하기 위해서 git python를 설치한다.

git을 활용하기 위해서 GUI 툴인 source tree, github desktop 등을 활용할 수도 있다.

초기 설치를 완료한 이후에 컴퓨터에 author 정보를 입력한다. 이메일 정보를 github에 가입된 이메일로 일치시켜야 커밋 이력들이 관리된다.

$ git config --global user.name 깃헙이름
$ git config --global user.email 깃헙이메일

로컬저장소 (Repository) 활용하기

1. 저장소 초기화

$ git init
Initialized empty Git repository in C:/Users/student/Desktop/TIL/.git/
(master) $
  • .git 폴더가 생성되며, 여기에 git과 관련된 모든 정보가 저장된다.
  • git python에 (master)라고 표현되는데, 이는 master 브랜치에 있다는 뜻이다.

2. add

working directory, 즉 작업공간에서 변경된 사항을 이력으로 저장하기 위해서는 반드시 staging area를 거쳐야 한다.

$ git add markdown.md    # 특정 파일
$ git add images/        # 특정 폴더
$ git add .                #현재 디렉토리
  • add 전 상태

    $ git status
    On branch master
    
    No commits yet
    
    # 트래킹되고 있지 않는 파일들
    # => commit 이력에 한번도 담기지 않은 파일들
    Untracked files:
    # 커밋될 것들에 포함시키려면 add 명령어를 사용해 
      (use "git add <file>..." to include in what will be committed)
            git.md
            images/
            markdown.md
    
    # 아직 커밋될 것들은 없지만,
    # untracked files은 존재한다.
    nothing added to commit but untracked files present (use "git add" to track)
  • add 후 상태

    $ git status
    On branch master
    
    No commits yet
    # 커밋될 변화들
    # => staging area에 있는 파일들
    Changes to be committed:
      (use "git rm --cached <file>..." to unstage)
            new file:   images/googlelogo_color_272x92dp.png
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
            git.md
            markdown.md
    student@M5041 MINGW64 ~/Desktop/TIL (master)
    $ git add .
    
    $ git status
    On branch master
    
    No commits yet
    
    Changes to be committed:
      (use "git rm --cached <file>..." to unstage)
            new file:   git.md
            new file:   images/googlelogo_color_272x92dp.png
            new file:   markdown.md

3. commit

commit은 이력을 확정짓는 명령어로, 해당 시점의 스냅샷을 기록한다.

커밋 시에는 반드시 메세지를 작성해야하며, 메세지는 변경사항을 알 수 있도록 명확하게 작성한다.

$ git commit -m '마크다운 및 git 정리'
[master (root-commit) a4d2871] 마크다운 및 git 정리
 3 files changed, 182 insertions(+)
 create mode 100644 git.md
 create mode 100644 images/googlelogo_color_272x92dp.png
 create mode 100644 markdown.md

커밋 이후에는 아래의 명령어를 통해 지금까지 작성된 이력을 확인하자.

$ git log
commit ~ (HEAD -> master)
Author: ~
Date:   ~

$ git log --oneline
aaaaaaa (HEAD -> master) 마크다운 및 git 정리

$ git log -1
commit ~ (HEAD -> master)
Author: ~
Date:   ~

    마크다운 및 git 정리

커밋은 해시코드를 바탕으로 구분된다.

원격 저장소(Remote Repository) 활용하기

원격 저장소 기능을 제공하는 다양한 서비스 중에서 github을 기준으로 설명한다.

0. 준비사항

  • Github에 Repository 생성

1. 원격 저장소 등록

$ git remote add origin 깃허브url
  • 원격저장소(remote)로 origin이라는 이름으로 깃허브url을 등록(add)한다.

  • 등록된 원격 저장소 목록을 보기 위해서는 아래의 명령어를 활용한다.

    $ git remote -v
    origin  https://github.com/dy5299/TIL.git (fetch)
    origin  https://github.com/dy5299/TIL.git (push)

2. push - 원격저장소 업로드

$ git push origin master

origin으로 설정된 원격저장소에 master 브랜치로 업로드(push)

이후 변경사항이 생길 때마다, add - commit, push를 반복하면 된다.

그리고, 항상 모든 명령어 이후에 연관된 상태를 확인하자.

status, log, remote -v

테스트

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   git.md

no changes added to commit (use "git add" and/or "git commit -a")

$ git add git.md
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   git.md

$ git commit -m '깃마크다운 문서'
[master 549744b] 깃마크다운 문서
 1 file changed, 92 insertions(+), 1 deletion(-)

$ git push origin master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 2.31 KiB | 2.31 MiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/dy5299/TIL.git
   a4d2871..549744b  master -> master

3. pull

$ git pull origin master

원격 저장소의 변경 사항을 받아온다.

4. clone

$ git clone 깃허브url

원격 저장소를 복제한다.

주의! init 명령어와 같이 기억하자! NOT push

ETC

$ git remote rm origin
# origin 제거

$ git push -f origin master
# 강제 덮어쓰기 (folder to web)

GIT 히스토리 삭제하기(초기화)

1. 기존의 히스토리 삭제

$ rm -rf .git

2. 파일정리 후 새로운 git 설정

$ git init
$ git add .
$ git commit -m "first commit"

3. git 저장소 연결 후 강제 push

$ git remote add origin 깃주소
$ git push -u --force origin master