본문 바로가기
깃허브(GitHub)

0706 취소하기, 오류메세지

by 대금부는개발자 2021. 7. 6.
SMALL

1. git add 취소하기

2.git commit 취소하기

3. 작업한 내용 취소하기

4.git push 취소(?) 하기

1. git add 취소(삭제 개념)  - f.md이 추가(Staging Area에 등록)되어 있는 상황

touch f.md 새로운 파일 만들고

git add f.md로 추가

git restore --staged 파일명 여기선 git restore --staged f.md

git restore -s로 짧게 쓸 수 있어요 (파일 명뿐만 아니라 폴더 명도 됩니다.)

구 버전은

git reset HEAD 파일명이었어요 하지만 git 을 관리하는 사람들이 reset 단어 혼동 때문에 restore이 생겼어요.

→ 지금도 동작하기는 합니다.

2. commit 취소

git reset {옵션} {위치}

위치 : 위치를 표현할 때 HEAD 사용 (현재 위치는 HEAD)

한 단계 1 commit 전 HEAD ~1 (한 개일 때는 숫자 생략 가능)

2 commit 전 HEAD~2

지금은 옵션 없이 사용해 봐요(위치는 필수)

git reset HEAD~

git status, git log로 확인

옵션(선택) 3가지

--mixed(기본값) commit 했었던 파일과, 현재 WD(작업 중인 파일) 내용을 합쳐줍니다.

(commit(작업) 했던 내용은 그대로

--soft commit 했었던 파일을, Staging에 넣어줌

commit 했던 내용, 그대로

--hard(주의!!) commit 했었던 파일을, 모조리 날리고 이전 commit의 상태로 갑니다.

commit 했었던 내용 사라져요.

작업한 내용 취소(삭제) 하기 → 자주 사용!!!!!!

ㅇ아직 WD(working derectory)에 있는 있는 것들을 되돌리기 ( ctrl+z)

되돌리게 되면 원래대로 돌아갈 수 없음! (주의!!!)

git restore 파일명

구버전

git checkout --파일명

제약사항

이미 git으로 관리한 적 있는 파일들만 가능해요

( 원본이 무엇인지를 안다 → 원본과 지금 작업 중인 내용의 차이를 알고 있다! )

그렇기 때문에 되돌리는 것이 가능!!

3.git push 취소(?) 하기

git push는 결국 공개적으로 commit 한 사실을 널리 알리는 것!

그런데 이것을 되돌린다?

→ 쉽게 되지 않는다!

1. 강제(force)로 덮어 씌우기( 권장하지 않음)

git push -f : 강제로 push

쉬운 만큼 빠릅니다!

2. 되돌린 사실을 알리는 것(revert) (권장하는 방법!)

- revert도 commit이다!

git revert{위치}

상대적 위치

HEAD,HEAD~,HEAD2

 

 

오류메세지들

 

error: src refspec master does not match any
error: failed to push some refs to

commit이 없는 경우! branch가 없는 경우!

 

push가 안돼요!

 

$ git push origin master
To https://github.com/nwith/TIL-test.git
! [rejected]       master -> master (fetch first)
error: failed to push some refs to 'https://github.com/nwith/TIL-test.git'
hint: Updates were rejected because the remote contains work that you do


# 업데이트가 거절되었다. 왜냐하면 remote가 니가 local에 들고 있지 않은 commit을 들고 있다.


hint: not have locally. This is usually caused by another repository
pushing


# 이 상황은 보통 같은 주소로 다른 repository를 push할 때 발생할 수 있다.


hint: to the same ref. You may want to first integrate the remote changes


# push하기 전에 remote(GitHub)의 변경 사항을 먼저 반영해야할 수도 있다.


hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

 

  • 거절이 된 상황
    • Local(내 컴퓨터)의 commit들과 Remote(GitHub)의 commit들이 일치가 되지 않아서, 순서, 있어야 하는 것들이 없어서
  • 그렇다면, Push가 안되면 Pull을 먼저!
    • 아래와 같은 Commit message를 작성하는 화면이 나옴!
    •  

Merge branch 'master' of https://github.com/nwith/TIL-test
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.

 

  • Vim 에디터의 조작
    • esc
    • shift 누른 상태에서, z 두번 (또는 : -> q -> 엔터)

$ git pull
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 0), reused 2 (delta 0), pack-reused 0
Unpacking objects: 100% (2/2), 239 bytes | 29.00 KiB/s, done.
From https://github.com/nwith/TIL-test
  4428db6..c3e52f7 master     -> origin/master
Merge made by the 'recursive' strategy.
c.md | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 c.md

 

최근 commit 수정하기

 

  1. git commit --amend
  2. Vim!e.md 추가

    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    #
    # Date:     Tue Jul 6 11:18:41 2021 +0900
    #
    # On branch master
    # Your branch is ahead of 'origin/master' by 1 commit.
    #   (use "git push" to publish your local commits)
    #
    # Changes to be committed:
    #       new file:   e.md
    #
    • i: 편집 모드
    • commit 메시지 자유롭게 수정
    • esc: 편집 모드 종료
    • : > wq > enter! : 저장(w)하고, 종료(q)!
  3. 수정 완료!$ git commit --amend
    [master 20bcd5f] e.md 추가
    Date: Tue Jul 6 11:18:41 2021 +0900
    1 file changed, 0 insertions(+), 0 deletions(-)
    create mode 100644 e.md
LIST

'깃허브(GitHub)' 카테고리의 다른 글

0706 Git Branch  (0) 2021.07.06
0706 gitignore  (0) 2021.07.06
0706 Git 허브 시나리오  (0) 2021.07.06
0705 Git 사용법 Repository 만들기  (0) 2021.07.05
0705 git 명령어 기본정보 입력  (0) 2021.07.05

댓글