ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Git시작하기
    Git 2022. 9. 17. 17:19

    Git이란?!



    1. 레포지토리(repository)
    - 커밋이 저장되는 곳
    2. 커밋commit 
    - 프로젝트 디렉토리의 특정 모습을 하나의 버전으로 남기는 행위 & 결과물

     

     

    repository만들기

    yunajoe@yunajoe MINGW64 ~/Git
    $ mkdir MathTool
    
    yunajoe@yunajoe MINGW64 ~/Git
    $ cd MathTool
    
    yunajoe@yunajoe MINGW64 ~/Git/MathTool
    $ git init  
    Initialized empty Git repository in C:/Users/yunaj/Git/MathTool/.git/  => 비어있는 레포지토리를 생성
    
    yunajoe@yunajoe MINGW64 ~/Git/MathTool (master)
    $ ls -al
    total 4
    drwxr-xr-x 1 yunajoe 197609 0 Sep 17 16:44 ./
    drwxr-xr-x 1 yunajoe 197609 0 Sep 17 16:41 ../
    drwxr-xr-x 1 yunajoe 197609 0 Sep 17 16:44 .git/ => 레포지토리 
    
    yunajoe@yunajoe MINGW64 ~/Git/MathTool/.git (GIT_DIR!)
    $ ls -al
    total 11
    drwxr-xr-x 1 yunajoe 197609   0 Sep 17 16:44 ./
    drwxr-xr-x 1 yunajoe 197609   0 Sep 17 16:44 ../
    -rw-r--r-- 1 yunajoe 197609  23 Sep 17 16:44 HEAD
    -rw-r--r-- 1 yunajoe 197609 130 Sep 17 16:44 config
    -rw-r--r-- 1 yunajoe 197609  73 Sep 17 16:44 description
    drwxr-xr-x 1 yunajoe 197609   0 Sep 17 16:44 hooks/
    drwxr-xr-x 1 yunajoe 197609   0 Sep 17 16:44 info/
    drwxr-xr-x 1 yunajoe 197609   0 Sep 17 16:44 objects/
    drwxr-xr-x 1 yunajoe 197609   0 Sep 17 16:44 refs/

    첫 커밋 해보기 

    순서 
    1. 이름과 이메일 
    2. 커밋하기전에 커밋할 파일을 지정하는 add 실행
    3. 커밋하기(with message)

     

     

    calculator.py, Lincense 파일을 만들어 MathTool 폴더 안에 넣어보기

    **********git에게 commit한 사람 알려주기**************


    yunajoe@yunajoe MINGW64 ~/Git/MathTool (master)
    $ git config user.name "yuna"   => 이름

    yunajoe@yunajoe MINGW64 ~/Git/MathTool (master)
    $ git config user.email "yunajoe@gmail.com" => 이메일 

     

     


    *******add => 커밋할 파일을 미리 지정해 줘야 함 *****

    yunajoe@yunajoe MINGW64 ~/Git/MathTool (master)
    $ git add calculator.py

    yunajoe@yunajoe MINGW64 ~/Git/MathTool (master)
    $ git add License

     

    **** commit 메시지와 함께 커밋하기****



    yunajoe@yunajoe MINGW64 ~/Git/MathTool (master)
    $ git commit -m "Create caculator.py and License"
    [master (root-commit) c30c6dc] Create caculator.py and License
     2 files changed, 7 insertions(+)
     create mode 100644 License
     create mode 100644 calculator.py

    \

    ==============================================

    Git은 내부적으로 크게 3가지 종류의 작업 영역을 두고 동작합니다.

    각 작업 영역의 이름은

    1. working directory
    - 작업을 하는 프로젝트 디렉토리
    - MathTool 디렉토리가 working directory

    2. staging area
    - git add를 한 파일들이 존재하는 영역
    - staging area는 git add를 한 파일들이 존재하는 영역
    - 커밋을 하게되면 staging area에 있는 파일들만 커밋에 반영

    3. repository
    - working directory의 변경 이력들이 저장되어 있는 영역
    - 커밋들이 저장되는 영역

    ''' 
    working directory에서 뭔가 작업을 하고,
    작업한 파일들을 git add 해주고,
    커밋을 하면 staging area에 있던 파일들의 모습이 마치 영화의 한 장면, 스냅샷(snapshot)처럼 이 repository에 저장
    '''

    두개의 파일을 수정하고, 
    staging area에 calculator.py 를 올려 놓는다 
    
    
    yunajoe@yunajoe MINGW64 ~/Git/MathTool (master)
    $ git add calculator.py
    
    
    yunajoe@yunajoe MINGW64 ~/Git/MathTool (master)
    $ git status => 깃이 인식하고 있는 프로젝트 디렉토리의 현재 상태를 보여줌
    On branch master
    Changes to be committed:   => 커밋에 반영될 변경사항
      (use "git restore --staged <file>..." to unstage)
            modified:   calculator.py
    
    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:   License
    
    # 이제 licesnse로 add 해주깅 
    
    yunajoe@yunajoe MINGW64 ~/Git/MathTool (master)
    $ git add License
    
    yunajoe@yunajoe MINGW64 ~/Git/MathTool (master)
    $ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            modified:   License
            modified:   calculator.py
    
    
    ======================
    
    yunajoe@yunajoe MINGW64 ~/Git/MathTool (master)
    $ mkdir meeting-log
    
    yunajoe@yunajoe MINGW64 ~/Git/MathTool (master)
    $ cd meeting-log
    
    yunajoe@yunajoe MINGW64 ~/Git/MathTool/meeting-log (master)
    $ touch day1
    
    yunajoe@yunajoe MINGW64 ~/Git/MathTool/meeting-log (master)
    $ touch day2
    
    
    yunajoe@yunajoe MINGW64 ~/Git/MathTool/meeting-log (master)
    $ ls -al
    total 4
    drwxr-xr-x 1 yunajoe 197609 0 Sep 17 17:32 ./
    drwxr-xr-x 1 yunajoe 197609 0 Sep 17 17:32 ../
    -rw-r--r-- 1 yunajoe 197609 0 Sep 17 17:32 day1
    -rw-r--r-- 1 yunajoe 197609 0 Sep 17 17:32 day2
    
    ========================================
    
    
    yunajoe@yunajoe MINGW64 ~/Git/MathTool/meeting-log (master)
    $ cd ..
    
    # 폴더를 모두 staging area에 올려보자 
    
    yunajoe@yunajoe MINGW64 ~/Git/MathTool (master)
    $ git add meeting-log
    
    yunajoe@yunajoe MINGW64 ~/Git/MathTool (master)
    $ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            modified:   License
            modified:   calculator.py
            new file:   meeting-log/day1
            new file:   meeting-log/day2
    
    =======================
    
    # license와 cacluatlro.py 파일을 수정해주자 
    
    yunajoe@yunajoe MINGW64 ~/Git/MathTool (master)
    $ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            modified:   License
            modified:   calculator.py
            new file:   meeting-log/day1
            new file:   meeting-log/day2
    
    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:   License
            modified:   calculator.py
    
    # license 와 calculator.py 파일 한꺼번에 staging area에 올리기 
    
    yunajoe@yunajoe MINGW64 ~/Git/MathTool (master)
    $ git add . ==> 현재 프로젝트 디렉토리내에서 변경사항이 생긴 모든 파일들을 staging area에 추가하라 
    
    yunajoe@yunajoe MINGW64 ~/Git/MathTool (master)
    $ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            modified:   License
            modified:   calculator.py
            new file:   meeting-log/day1
            new file:   meeting-log/day2
    
    
    yunajoe@yunajoe MINGW64 ~/Git/MathTool (master)
    $ git commit -m "add revised texts"
    [master d5881c1] add revised texts
     4 files changed, 2 insertions(+), 1 deletion(-)
     create mode 100644 meeting-log/day1
     create mode 100644 meeting-log/day2
     
     
     일단 Git에서 파일들은 크게 다음 2가지 상태를 가집니다.
    
    Untracked 상태
    Tracked 상태 
    그리고 Tracked 상태는 다시 아래와 같은 3가지 상태로 나눌 수 있구요. 
    
    ============================================
    
    Staged 상태
    Unmodified 상태
    Modified 상태
    
    1. Untracked 상태
    
    Untracked는 '추적되지 않고 있는'이라는 뜻입니다. 이 상태는 파일이 Git에 의해서 그 변동사항이 전혀 추적되고 있지 않는 상태를 뜻합니다. 예를 들어, 파일을 새로 생성하고 그 파일을 한 번도 git add 해주지 않았다면 이 상태입니다
    
    ================================================================================================
    2. Tracked 상태
    
    파일이 Git에 의해 그 변동사항이 추적되고 있는 상태입니다. 이 상태는 다시 그 특성에 따라 3가지 상태로 나뉩니다.
    즉, add한 상태 
    
    (1) Staged 상태
    
    파일의 내용이 수정되고나서, staging area에 올라와있는 상태를 Staged(스테이징된, stage area에 올려진) 상태라고 합니다.
    
    새로 생성한 파일에 내용을 쓰고 git add를 해주거나
    한 번이라도 커밋에 포함됐었던 파일이라도 내용을 수정하고 git add를 해주면 이 상태입니다.
    
    (2) Unmodified 상태
    
    현재 파일의 내용이 최신 커밋의 모습과 비교했을 때 전혀 바뀐 게 없는 상태면 그 파일은 Unmodified(수정되지 않은, 변한 게 없는) 상태입니다. 커밋을 하고 난 직후에는 working directory 안의 모든 파일들이 이 상태가 됩니다.
    
    (3) Modified 상태
    
    최신 커밋의 모습과 비교했을 때 조금이라도 바뀐 내용이 있는 상태면 그 파일은 Modified(수정된) 상태입니다.
    
    이렇게 Git에서 파일은 매 순간 4가지 상태 중 하나의 상태에 있게 됩니다. 이 내용을 그림으로 정리하면 아래와 같습니다.
    
    ================================================================================================
    
    calculator.py 에 함수 하나를 추가해보자 
    
    # 기본계산기
    def add(a,b):
    	return a + b 
    
    def subtract(a,b):
    	return a - b 
    
    def say_hello():
    	print("안뇽!")
    
    
    yunajoe@yunajoe MINGW64 ~/Git/MathTool (master)
    $ git add calculator.py
    
    yunajoe@yunajoe MINGW64 ~/Git/MathTool (master)
    $ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            modified:   calculator.py
    
    
    
    
    
    
    
    
    
    yunajoe@yunajoe MINGW64 ~/Git/MathTool (master)
    $ git add calculator.py
    
    yunajoe@yunajoe MINGW64 ~/Git/MathTool (master)
    $ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            modified:   calculator.py
    
    # staging에 올라간 calculator.py 를 삭제해주자. reset을 해주면은 되고, working directory에는 아직 남아잇다.
    
    yunajoe@yunajoe MINGW64 ~/Git/MathTool (master)
    $ git reset calculator.py
    Unstaged changes after reset:
    M       calculator.py
    
    yunajoe@yunajoe MINGW64 ~/Git/MathTool (master)
    $ git status
    On branch master
    Changes not staged for commit:  # working directory에서 변경은 됬지만 staging area에 올라가 있지 않다.
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
            modified:   calculator.py
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    
    이제 say_hellp()함수를 지워주고 
    
    # 기본계산기
    def add(a,b):
    	return a + b 
    
    def subtract(a,b):
    	return a - b  
    
    yunajoe@yunajoe MINGW64 ~/Git/MathTool (master)
    $ git status
    On branch master
    nothing to commit, working tree clean => 이전과 달라지 내용이 없다. 
    
    
    ===========GIT 명령어 정리===============
    
    
    git init : 현재 디렉토리를 Git이 관리하는 프로젝트 디렉토리(=working directory)로 설정하고 그 안에 레포지토리(.git 디렉토리) 생성
    git config user.name 'codeit' : 현재 사용자의 아이디를 'codeit'으로 설정(커밋할 때 필요한 정보)
    git config user.email 'teacher@codeit.kr' : 현재 사용자의 이메일 주소를 'teacher@codeit.kr'로 설정(커밋할 때 필요한 정보)
    git add [파일 이름] : 수정사항이 있는 특정 파일을 staging area에 올리기
    git add [디렉토리명] : 해당 디렉토리 내에서 수정사항이 있는 모든 파일들을 staging area에 올리기 
    git add . : working directory 내의 수정사항이 있는 모든 파일들을 staging area에 올리기
    git reset [파일 이름] : staging area에 올렸던 파일 다시 내리기
    git status : Git이 현재 인식하고 있는 프로젝트 관련 내용들 출력(문제 상황이 발생했을 때 현재 상태를 파악하기 위해 활용하면 좋음) 
    git commit -m "커밋 메시지" : 현재 staging area에 있는 것들 커밋으로 남기기
    git help [커맨드 이름] : 사용법이 궁금한 Git 커맨드의 공식 메뉴얼 내용 출력

     

    댓글

Designed by Tistory.