ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • git reset
    Git 2023. 2. 19. 11:13

    HEAD 
    - 어떤 커밋 하나를 가리킴 
    - HEAD가 가리키는 커밋에 따라 working directory구성

    git reset 
    - 과거 커밋으로 아예 돌아가고 싶을 때 
    - HEAD가 과거의 커밋을 가리키게 할 수 있따 
    - working directory의 내용도 과거 커밋을 가리키게 할 수 있다 





    yunajoe@DESKTOP-ULNOHTM MINGW64 ~/Desktop/Git_Practice (master)
    $ git history
    75494b3b9c8516938bbf65d1fdf5ee8348cef63f (HEAD -> master, origin/master) Add multiply function
    23453a29860fb3bce2a87749717bfd6f1dfc0100 Add the info in README.md
    74b3b85cae59f4dc2daaee15110fc7724a1812a1 readme수정
    b69451e5813c4f433746acbf77a92e34a2fbb299 Update README.md
    095343435235eb984761d9b19bc9c39213258237 Make REAME.md looknice
    cfe4b0bcf749debcda87423ff08a0381d98d7619 add the info of calculator.py
    7bcb609ce8f66cc54e82af9275a0bfe558c03de6 Create REAME.me
    d5881c1e25101fa9b754a2fe7a1ded886169167f add revised texts
    c30c6dc1c7cdf593952cc3cd7099b3c579c0a7b2 Create caculator.py and License

    yunajoe@DESKTOP-ULNOHTM MINGW64 ~/Desktop/Git_Practice (master)
    $ git reset --hard 74b3
    HEAD is now at 74b3b85 readme수정

    yunajoe@DESKTOP-ULNOHTM MINGW64 ~/Desktop/Git_Practice (master)
    $ git history
    74b3b85cae59f4dc2daaee15110fc7724a1812a1 (HEAD -> master) readme수정
    b69451e5813c4f433746acbf77a92e34a2fbb299 Update README.md
    095343435235eb984761d9b19bc9c39213258237 Make REAME.md looknice
    cfe4b0bcf749debcda87423ff08a0381d98d7619 add the info of calculator.py
    7bcb609ce8f66cc54e82af9275a0bfe558c03de6 Create REAME.me
    d5881c1e25101fa9b754a2fe7a1ded886169167f add revised texts
    c30c6dc1c7cdf593952cc3cd7099b3c579c0a7b2 Create caculator.py and License

    yunajoe@DESKTOP-ULNOHTM MINGW64 ~/Desktop/Git_Practice (master)
    $ cat calculator.py
    # 기본계산기
    def add(a,b):
            return a + b

    def subtract(a,b):
            return a - b

    =====================================================
    1. git reset을 쓸 때 --hard는 뭐였을까?

    * git reset과 함께 쓸 수 있는 옵션에는 --hard 말고도 --soft, --mixed라는 옵션도 있다
    --hard, --soft, --mixed 

    * HEAD가 과거의 커밋을 가리키게 되는 결과는 git reset에서 어느 옵션을 쓰든 항상 똑같습니다. 
    하지만 working directory의 내부도 그 과거 커밋의 모습처럼 바뀌는 건 --hard 옵션을 썼기 때문에 
    그런 겁니다.--soft, --mixed 옵션을 쓰면 그렇지 않은데요. 다음 영상에서 자세히 알아볼게요.
    =====================================================
    2. staging area에 있던 것들은 커밋하고 나면 어떻게 될까?
    -  git add를 해서 staging area에 올린 다음 커밋
    -  그런데 커밋을 하게 되면 staging area에 있던 파일들은 대로 남아있습니다. 그러니까 커밋을 했다고 staging area가 초기화되거나 하지는 않는 겁니다.ㄴ
    그대로 남아있습니다. 그러니까 커밋을 했다고 staging area가 초기화되거나 하지는 않는 겁니다.

    계속 git add를 할 때마다 staging area에서는 새로운 파일이 추가되거나 

    원래 있던 파일이 더 새로운 버전의 것으로 교체되거나 할 뿐
    (이 말이 어떤 의미인지는 사실 Git의 내부 작동 원리를 잘 알아야 정확히 이해할 수 있습니다. 일단은 이렇게 이해하고 넘어가세요.)

    staging area에 있던 것들은 커밋을 하더라도 그것과 상관없이 계속 남아있다는 점, 잘 기억하세요!

    =====================================================
    git reset의 3가지 옵션 
    --hard, --soft, --mixed 

    사진2  

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

     

    (1) HEAD가 특정 커밋(1) HEAD가 특정 커밋을 가리키도록 함을 가리키도록 함



    *** cf. GIT의 3가지 작업영역 

    Git은 내부적으로 크게 3가지 종류의 작업 영역을 두고 동작
    각 작업 영역의 이름은  
    1. working directory
    2. staging area
    3. repository

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

    첫 번째 작업 영역인 working directory는 작업을 하는 프로젝트 디렉토리


    예를 들어, 현재 작업하고 있는 파일들이 있는 폴더가 아래라면은 
    yunajoe@DESKTOP-ULNOHTM MINGW64 ~/Desktop/Git_Practice 
    /Git_Practice 가  working directory


    두 번째 작업 영역인 staging area는 git add를 한 파일들이 존재하는 영역입니다. 
    커밋을 하게되면 staging area에 있는 파일들만 커밋에 반영


    세 번째 작업 영역인 repository는 working directory의 변경 이력들이 저장되어 있는 영역
    working directory에서 뭔가 작업을 하고,
    작업한 파일들을 git add 해주고,
    커밋을 하면 staging area에 있던 파일들의 모습이 마치 영화의 한 장면, 
    스냅샷(snapshot)처럼 이 repository에 저장되는 
     .git 디렉토리가 repository

    사진3


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



    calculator.py를 수정하기 
    - divide함수 추가 

    # 기본계산기
    def add(a,b):
    return a + b 

    def subtract(a,b):
    return a - b 

    def divide(a,b):
    yunajo a/b



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



    yunajoe@DESKTOP-ULNOHTM MINGW64 ~/Desktop/Git_Practice (master)
    $ git add .

    yunajoe@DESKTOP-ULNOHTM MINGW64 ~/Desktop/Git_Practice (master)
    $ git status
    On branch master
    Your branch is behind 'origin/master' by 2 commits, and can be fast-forwarded.
      (use "git pull" to update your local branch)

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


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

    yunajoe@DESKTOP-ULNOHTM MINGW64 ~/Desktop/Git_Practice (master)
    $ git history
    74b3b85cae59f4dc2daaee15110fc7724a1812a1 (HEAD -> master) readme수정
    b69451e5813c4f433746acbf77a92e34a2fbb299 Update README.md
    095343435235eb984761d9b19bc9c39213258237 Make REAME.md looknice
    cfe4b0bcf749debcda87423ff08a0381d98d7619 add the info of calculator.py
    7bcb609ce8f66cc54e82af9275a0bfe558c03de6 Create REAME.me
    d5881c1e25101fa9b754a2fe7a1ded886169167f add revised texts
    c30c6dc1c7cdf593952cc3cd7099b3c579c0a7b2 Create caculator.py and License

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

    soft 옵션쓰기 


    // 현재 commit 히스토리 보기 
    $ git history
    74b3b85cae59f4dc2daaee15110fc7724a1812a1 (HEAD -> master) readme수정
    b69451e5813c4f433746acbf77a92e34a2fbb299 Update README.md
    095343435235eb984761d9b19bc9c39213258237 Make REAME.md looknice
    cfe4b0bcf749debcda87423ff08a0381d98d7619 add the info of calculator.py
    7bcb609ce8f66cc54e82af9275a0bfe558c03de6 Create REAME.me
    d5881c1e25101fa9b754a2fe7a1ded886169167f add revised texts
    c30c6dc1c7cdf593952cc3cd7099b3c579c0a7b2 Create caculator.py and License
    yunajoe@DESKTOP-ULNOHTM MINGW64 ~/Desktop/Git_Practice (master)


    $ git reset --soft b6945

    yunajoe@DESKTOP-ULNOHTM MINGW64 ~/Desktop/Git_Practice (master)
    $ git history
    b69451e5813c4f433746acbf77a92e34a2fbb299 (HEAD -> master) Update README.md
    095343435235eb984761d9b19bc9c39213258237 Make REAME.md looknice
    cfe4b0bcf749debcda87423ff08a0381d98d7619 add the info of calculator.py
    7bcb609ce8f66cc54e82af9275a0bfe558c03de6 Create REAME.me
    d5881c1e25101fa9b754a2fe7a1ded886169167f add revised texts
    c30c6dc1c7cdf593952cc3cd7099b3c579c0a7b2 Create caculator.py and License


    =========================================
    working 디렉토리는 reset의 영향을 받지 않는다 

    $ cat calculator.py
    # 기본계산기
    def add(a,b):
            return a + b

    def subtract(a,b):
            return a - b

    def divide(a,b):
            return a/b


    staging 디렉토리도 영향을 받지 안흔ㄴ다 

    yunajoe@DESKTOP-ULNOHTM MINGW64 ~/Desktop/Git_Practice (master)
    $ git status
    On branch master
    Your branch is behind 'origin/master' by 3 commits, and can be fast-forwarded.
      (use "git pull" to update your local branch)

    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            modified:   README.md
            modified:   calculator.py
    ===================================
    2. mixed옵션 사용하기 
    - working 디렉토리는 변하지 않는다 
    - staging 디렉토리는 변함

    (1) HEAD가 특정 커밋을 가리키도록 함

    (2) staging area를 특정 커밋의 모습처럼 리셋함

    (3) working directory는 건드리지 않고 git reset 하기 이전 상태 그대로 둠



    yunajoe@DESKTOP-ULNOHTM MINGW64 ~/Desktop/Git_Practice (master)
    $ git history
    b69451e5813c4f433746acbf77a92e34a2fbb299 (HEAD -> master) Update README.md
    095343435235eb984761d9b19bc9c39213258237 Make REAME.md looknice
    cfe4b0bcf749debcda87423ff08a0381d98d7619 add the info of calculator.py
    7bcb609ce8f66cc54e82af9275a0bfe558c03de6 Create REAME.me
    d5881c1e25101fa9b754a2fe7a1ded886169167f add revised texts
    c30c6dc1c7cdf593952cc3cd7099b3c579c0a7b2 Create caculator.py and License

    yunajoe@DESKTOP-ULNOHTM MINGW64 ~/Desktop/Git_Practice (master)
    $ git reset --mixed 09534
    Unstaged changes after reset:
    M       README.md
    M       calculator.py

    yunajoe@DESKTOP-ULNOHTM MINGW64 ~/Desktop/Git_Practice (master)
    $ git history
    095343435235eb984761d9b19bc9c39213258237 (HEAD -> master) Make REAME.md looknice
    cfe4b0bcf749debcda87423ff08a0381d98d7619 add the info of calculator.py
    7bcb609ce8f66cc54e82af9275a0bfe558c03de6 Create REAME.me
    d5881c1e25101fa9b754a2fe7a1ded886169167f add revised texts
    c30c6dc1c7cdf593952cc3cd7099b3c579c0a7b2 Create caculator.py and License

    yunajoe@DESKTOP-ULNOHTM MINGW64 ~/Desktop/Git_Practice (master)
    $ cat calculator.py
    # 기본계산기
    def add(a,b):
            return a + b

    def subtract(a,b):
            return a - b

    def divide(a,b):
            return a/b
    yunajoe@DESKTOP-ULNOHTM MINGW64 ~/Desktop/Git_Practice (master)
    $ git status
    On branch master
    Your branch is behind 'origin/master' by 4 commits, and can be fast-forwarded.
      (use "git pull" to update your local branch)

    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:   README.md
            modified:   calculator.py
    ===================================

    3.  hard 옵션 사용하기 

    (1) HEAD가 특정 커밋을 가리키도록 함

    (2) staging area도 특정 커밋의 모습처럼 리셋함

    (3) working directory도 특정 커밋의 모습처럼 리셋함

    1

    --mixed



    yunajoe@DESKTOP-ULNOHTM MINGW64 ~/Desktop/Git_Practice (master)
    $ git history
    095343435235eb984761d9b19bc9c39213258237 (HEAD -> master) Make REAME.md looknice
    cfe4b0bcf749debcda87423ff08a0381d98d7619 add the info of calculator.py
    7bcb609ce8f66cc54e82af9275a0bfe558c03de6 Create REAME.me
    d5881c1e25101fa9b754a2fe7a1ded886169167f add revised texts
    c30c6dc1c7cdf593952cc3cd7099b3c579c0a7b2 Create caculator.py and License

    yunajoe@DESKTOP-ULNOHTM MINGW64 ~/Desktop/Git_Practice (master)
    $ git reset --hard cfe4b
    HEAD is now at cfe4b0b add the info of calculator.py

    yunajoe@DESKTOP-ULNOHTM MINGW64 ~/Desktop/Git_Practice (master)
    $ git history
    cfe4b0bcf749debcda87423ff08a0381d98d7619 (HEAD -> master) add the info of calculator.py
    7bcb609ce8f66cc54e82af9275a0bfe558c03de6 Create REAME.me
    d5881c1e25101fa9b754a2fe7a1ded886169167f add revised texts
    c30c6dc1c7cdf593952cc3cd7099b3c579c0a7b2 Create caculator.py and License

    yunajoe@DESKTOP-ULNOHTM MINGW64 ~/Desktop/Git_Practice (master)
    $ cat calculator.py
    # 기본계산기
    def add(a,b):
            return a + b

    def subtract(a,b):
            return a - b

    yunajoe@DESKTOP-ULNOHTM MINGW64 ~/Desktop/Git_Practice (master)
    $ git status
    On branch master
    Your branch is behind 'origin/master' by 5 commits, and can be fast-forwarded.
      (use "git pull" to update your local branch)

    nothing to commit, working tree clean
    ======================================================================

    reset하기 전 상태로 돌리고 싶다면은?!

    $ git status
    On branch master
    Your branch is behind 'origin/master' by 5 commits, and can be fast-forwarded.
      (use "git pull" to update your local branch)

    nothing to commit, working tree clean


    $ git pull
    Updating cfe4b0b..75494b3
    Fast-forward
     README.md     | 5 +++--
     calculator.py | 3 +++
     2 files changed, 6 insertions(+), 2 deletions(-)


    75494b3b9c8516938bbf65d1fdf5ee8348cef63f (HEAD -> master, origin/master) Add multiply function
    23453a29860fb3bce2a87749717bfd6f1dfc0100 Add the info in README.md
    74b3b85cae59f4dc2daaee15110fc7724a1812a1 readme수정
    b69451e5813c4f433746acbf77a92e34a2fbb299 Update README.md
    095343435235eb984761d9b19bc9c39213258237 Make REAME.md looknice
    cfe4b0bcf749debcda87423ff08a0381d98d7619 add the info of calculator.py
    7bcb609ce8f66cc54e82af9275a0bfe558c03de6 Create REAME.me
    d5881c1e25101fa9b754a2fe7a1ded886169167f add revised texts
    c30c6dc1c7cdf593952cc3cd7099b3c579c0a7b2 Create caculator.py and License

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

    git reset을 할 때는 보통 아래와 같은 형식으로 쓰는데요.

    git reset [옵션] [커밋 아이디]
    방금 본 표기처럼 HEAD~x는 현재 HEAD가 가리키는 커밋보다 x단계 전에 있는 커밋

    그런데 이렇게 커밋 아이디를 쓰려면 매번 커밋 아이디를 찾아야한다는 불편함이 조금 있습니다. 
    사실 [커밋 아이디] 자리에 다른 걸 써줘도 되는데요.

    -맨위부터가 최신.. 아래로 내려갈 수록 old한 커밋 
    75494b3b9c8516938bbf65d1fdf5ee8348cef63f (HEAD -> master, origin/master) Add multiply function
    23453a29860fb3bce2a87749717bfd6f1dfc0100 Add the info in README.md
    74b3b85cae59f4dc2daaee15110fc7724a1812a1 readme수정
    b69451e5813c4f433746acbf77a92e34a2fbb299 Update README.md
    095343435235eb984761d9b19bc9c39213258237 Make REAME.md looknice
    cfe4b0bcf749debcda87423ff08a0381d98d7619 add the info of calculator.py
    7bcb609ce8f66cc54e82af9275a0bfe558c03de6 Create REAME.me
    d5881c1e25101fa9b754a2fe7a1ded886169167f add revised texts
    c30c6dc1c7cdf593952cc3cd7099b3c579c0a7b2 Create caculator.py and License

    아래의 명령어는 같은뜻..
    HEAD는 가장 최신 커밋, 즉 75494b3를 말한다 
    ^는 가장 최신 커밋 전인 23453a를 말한다
    git reset --hard 23453a
    git reset --hard HEAD^

    ======================================================================
    git reset --hard HEAD~2
    git reset --hard 74b3b
    ======================================================================
    커밋에 tag 달기

    git tag [태그 이름] [커밋 아이디]

    우리는 커밋을 할 때 해당 커밋에 관한 정보를 커밋 메시지로 남기는데요.
     그런데 커밋 중에서는 다른 것들보다 좀더 중요한 의미가 있는 커밋들도 있습니다. 
    이런 중요한 커밋에는 커밋 메시지뿐만 아니라 태그(tag)라는 것을 추가적으로 달기도 합니다.

    보통 프로젝트에서 주요 버전의 시작점이 되는 커밋에 이렇게 태그를 다는데요. 잠깐 아래 그림을 봅시다. 
    아래 그림에서 첫 번째 커밋에는 Version 1이라는 태그를 달고, 여섯 번째 커밋에는 Version 2라는 태그를 달고 싶다고 해봅시다

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


    $ git history
    75494b3b9c8516938bbf65d1fdf5ee8348cef63f (HEAD -> master, origin/master) Add multiply function
    23453a29860fb3bce2a87749717bfd6f1dfc0100 Add the info in README.md
    74b3b85cae59f4dc2daaee15110fc7724a1812a1 readme수정
    b69451e5813c4f433746acbf77a92e34a2fbb299 Update README.md
    095343435235eb984761d9b19bc9c39213258237 Make REAME.md looknice
    cfe4b0bcf749debcda87423ff08a0381d98d7619 add the info of calculator.py
    7bcb609ce8f66cc54e82af9275a0bfe558c03de6 Create REAME.me
    d5881c1e25101fa9b754a2fe7a1ded886169167f add revised texts
    c30c6dc1c7cdf593952cc3cd7099b3c579c0a7b2 Create caculator.py and License



    yunajoe@DESKTOP-ULNOHTM MINGW64 ~/Desktop/Git_Practice (master)
    $ git tag Version_1 c30c


    yunajoe@DESKTOP-ULNOHTM MINGW64 ~/Desktop/Git_Practice (master)
    $ git tag Version_2 75494b



    yunajoe@DESKTOP-ULNOHTM MINGW64 ~/Desktop/Git_Practice (master)
    $ git tag
    Version_1
    Version_2


    diff --git a/License b/License
    new file mode 100644
    index 0000000..a4f9748
    --- /dev/null
    +++ b/License
    @@ -0,0 +1 @@
    +Free
    \ No newline at end of file
    diff --git a/calculator.py b/calculator.py
    new file mode 100644
    index 0000000..022e54b
    --- /dev/null
    +++ b/calculator.py
    @@ -0,0 +1,6 @@
    +def add(a,b):
    +       return a + b
    +




    댓글

Designed by Tistory.