흔한 Git 사용 실수 대응법

로컬에서 commit 메시지 잘못 작성하였을 때

  • git commit --amend "새로운 커밋 메시지"

마지막 commit에 파일 add를 하지 않았을 때

  • git add missed-file.txt

  • git commit --amend

로컬에서 올리지 말아야 할 파일을 commit 했을때

  • git reset --soft HEAD~1

  • git reset /assets/img/misty-and-pepper.jpg

  • rm /assets/img/misty-and-pepper.jpg

  • git commit

git add 한 파일을 지울때

  • git reset /assets/img/misty-and-pepper.jpg

"master"에서 개발하다가 커밋 까지 했을때

  • git branch 새로운브랜치명

  • git reset HEAD~ --hard

  • git checkout 새로운브랜치명

branch 이름을 잘못 지었을때

  • git branch -m 기존명 바꿀명

  • 만약 잘못된 이름 branch 로 원격 저장소에 push 까지 했을 경우

    • git push origin --delete 기존명

    • git push origin 바꿀명

모든 것이 잘못되어, history를 보고 싶을때

  • git reflog : 아래 처럼 현재까지 한 모든 작업을 볼 수 있다. (무거운 작업이기에 유의해서 사용한다.)

  • git reset HEAD@{가고자 하는 인덱스} : 특정 index로 간다.

원격 git 저장소에 잘못된 파일이 푸시 됐을 때

  • git rebase -i

    • 마지막 커밋이었을 경우, git rebase -i HEAD~2

  • force push

    • git push origin +브랜치명

  • tree 가 더러울때

    • git stash 그리고 git stash apply

    • 또는, git reset --hard HEAD~1

참고 자료

  • https://about.gitlab.com/blog/2018/08/08/git-happens/

  • https://stackoverflow.com/questions/33004187/what-is-the-practical-difference-between-git-rm-cached-git-reset-and

Last updated