잡다한 Git 메모

1. Github 홈페이지에서 작업하여 원격 저장소에는 반영이 되어 있고, 로컬 저장소에는 반영이 되어 있지 않을 때

error: failed to push some refs

hint: Updates were rejected because the remote contains work that you do not have locally.
This is usually caused by another repository pushing to the same ref.
You may want to first integrate the remote changes(e.g., 'git pull ...') before pushing again.
See the 'Note about fast-forwards' in 'git push --help' for details.

→ 로컬 저장소에 반영해 주어야 함. remote 저장소(github 홈페이지 에디터)에서만 작업 했기 때문에 발생

  • 방법 1)
    git stash
    git pull
    git stash pop
    git add, commit, push
    
  • 방법 2)
    git fetch
    git rebase
    
  • git pull로 remote-local 내용만 맞춰줘도 해결되긴 한다.


2. local에서 작업한 것과 remote에 올라가 있는 파일 내용이 다를 때

  • git pull 하면 아래와 같은 오류가 남
$ git pull
error: Your local changes to the following files would be overwritten by merge:
Please commit your changes or stash them before you merge.
  • 방법
    • git stash: 작업중이던 파일을 임시로 저장해 두고 현재 브랜치의 상태를 마지막 커밋의 상태로 초기화


3. 원격 저장소에 있는 파일이나 폴더 삭제하고 싶을 때

  • .gitignore에 미처 입력하지 않아 원격 저장소에 잘못 올린 파일이나 폴더를 삭제할 경우에 사용한다.

  • 로컬 및 원격 저장소 모두 삭제할 경우
    git rm -rf {파일이나 폴더명}
    
  • 원격 저장소(Github)에서만 삭제할 경우
    git rm -r --cached {파일이나 폴더명}
    
  • git rmgit commit, git push



참고 문서