[git] git add, commit, push 취소

moo·2023년 4월 7일
0

git add 취소 (파일 상태를 unstage로 변경)

 git reset HEAD [file] 
  • 뒤에 파일명이 없으면 add한 파일 전체를 취소한다.

git commit 취소

//commit 목록 확인
git log

//commit을 취소하고 해당 파일들은 staged 상태로 워킹 디렉터리에 보존
git reset --soft HEAD^

//commit을 취소하고 해당 파일들은 unstaged 상태로 워킹 디렉터리에 보존
git reset --mixed HEAD^ // 기본 옵션
git reset HEAD^ // 위와 동일
git reset HEAD~2 // 마지막 2개의 commit을 취소

//commit을 취소하고 해당 파일들은 unstaged 상태로 워킹 디렉터리에서 삭제
git reset --hard HEAD^

// 워킹 디렉터리를 원격 저장소의 마지막 commit 상태로 되돌린다.
t reset --hard HEAD
  • reset 옵션
    • –soft : index 보존(add한 상태, staged 상태), 워킹 디렉터리의 파일 보존. 즉 모두 보존.
    • –mixed : index 취소(add하기 전 상태, unstaged 상태), 워킹 디렉터리의 파일 보존 (기본 옵션)
    • –hard : index 취소(add하기 전 상태, unstaged 상태), 워킹 디렉터리의 파일 삭제. 즉 모두 취소.

git push 취소

// 가장 최근의 commit을 취소 (기본 옵션 : --mixed)
git reset HEAD^

// Reflog(브랜치와 HEAD가 지난 몇 달 동안에 가리켰었던 커밋) 목록 확인
git reflog 또는 $ git log -g
// 원하는 시점으로 워킹 디렉터리를 되돌린다.
git reset HEAD@{number} 또는 $ git reset [commit id]

//되돌려진 상태에서 다시 커밋
git commit -m "Write commit messages"

//원격 저장소에 강제로 push
git push origin [branch name] -f

참고
https://gmlwjd9405.github.io/2018/05/25/git-add-cancle.html

profile
front developer

0개의 댓글