git 사용법 (add/commit/push/reset/revert/reflog)

Hyun·2022년 6월 16일
0

기타등등

목록 보기
5/11

tistory 블로그 내용 옮겨오고, 추가
git Error 정리

요약

git bash에서

git add *						// 모든 파일 add
git rm --cached -r [file name]	// github에 올라가있는 파일을 제거
git status						// 변경사항 확인
git commit -m "message"
git push -u origin master		// commit한 내용 github에 반영
// push까지 완료해야 github에서 해당 폴더가 제거된다.

git staging area


git init, remote

  • init
    git init
  • init 제거
    rm -r .git		// .git 폴더 삭제 == git local 저장소 지정 해제
    git remote -v	// 저장소 경로 확인 (연결되어있는 git 주소 확인)
  • remote(원격 연결)
    git remote -v // 연결되어있는 것 확인
    git remote add origin https://~   // 코드를 올릴 github repository, 처음에만 연결 
  • remote(원격 연결) 끊기
    git remote remove origin		// 연결 제거
    git remote -v 					// 연결 확인

Stage Fixes (working directory <-> staging area, local에서 upload)

  • add

    git add *		// add all
    git add -u	// 수정하거나 삭제된 파일을 반영할 수 있다.
  • staging된 파일 확인

    git status
  • add 취소 ( remove ) : staged상태가 된다 -> commit까지 해주어야 git에 반영

    git rm [file name]	// 파일 삭제
    git rm src/\*.css	// src 폴더 내에 있는 css확장명인 파일을 모두 삭제
    git rm \*~			// ~으로 끝나는 파일을 모두 삭제
    git reset HEAD [file name]		// 파일 상태를 unstaged로 변경(git add 취소)
    git checkout -- CONTRIBUTING.md	// 수정된 파일을 변화 없는 상태로 복원
    
    // untracked 파일 삭제(.gitignore에 명시하여 무시되는 파일은 지우지 않는다.)
    git clean -f		// dir 제외 파일들만 삭제
    git clean -f -d		// dir까지 삭제
    git clean -f -d -x	// ignored된 파일까지 삭제

Commit (staging area <-> repository)

  • commit (staging area -> repository)

    git commit -m "message"
    git commit -a -m "message"		// 수정되거나 삭제된 파일만 commit 해준다(-a 옵션)
  • commit 취소(reset, repository -> staging area)

    • push 전 commit 수정 ( 일부 파일 추가, commit msg를 변경할 때)
    git commit -m 'initial commit'
    git add forgotten_file
    git commit --amend	// 이 commit은 최근 commit에 덮어쓴다.
    • reset 명령어는 push 후에도 사용 가능하다(commit의 id로 복원하기 때문)
    git reset --soft HEAD^		// 최근 commit 취소, 해당 파일들은 staged상태(add된상태)로 working dir에 보존
    
    // commit 취소, 해당 파일들은 unstaged상태로 working dir에 보존
    git reset --mixed HEAD^
    git reset HEAD^			// 위와 동일
    git reset HEAD~2		// 마지막 2개의 commit을 취소

repository -> git

  • github에 올리기
  • commit했던 내용 원격에(github 페이지에) 올림, origin 원격저장소 이름, main 브랜치
  git push -u origin main
  git push <원격저장소 이름> <브랜치 이름>

Checkout the project (repository -> working directory)

  • github에서 내리기(없애기) : 원하는 시점으로 working dir를 복원

    • 자신의 local 내용을 remote에 강제로 덮어쓰기를 하는 것
    • 협업시에는 reset과 revert중에 revert 추천
    • 매개변수 soft/hard 차이
      • --soft : 해당 파일들은 staged상태로 working dir에 보존, 바로 commit 가능
      • --hard : 해당 파일들은 unstaged상태(default)
  • commit list 보기 : log를 그만 보고 싶다면 q 입력(:옆에)

    git log -g		// 현재 branch의 commit 이력을 보는 명령어
    git log -oneline		// log 한줄로 보기
    git log --oneline --reverse	// 오래된 이력부터 한줄로 보기
  • reset : 과거의 특정 commit으로 되돌림

    • 되돌아간 commit 이후의 모든 commit 정보는 log로 확인할 수 없다.(reflog 명령어로 확인 가능)

    • 장점 : commit history를 깔끔하게 유지 가능, 주로 혼자 사용하는 branch일 경우 사용

    • 단점 : 협업시 commit이 뒤섞여버릴 수 있음

      git reset HEAD@{number} 	// 원하는 시점으로 working dir를 되돌림
      git reset [commit id]		// 위와 동일
      
      git commit -m "message"		// 되돌려진 상태에서 다시 commit
      git push -f origin master	// 강제로 push
      
      git reset --soft "commit id"	// commit id로 되돌림
  • revert : 현재까지 남긴 commit list를 유지한 채 해당 commit id에 삭제된 이력(Revert)만 남는 것

    • 장점 : 중간에 어떤 문제때문에 commit을 되돌렸는지 기록 가능, 코드 충돌 최소화
    • 단점 : commit list가 길어질 수 있음
    git revert [commit id]  
    git revert HEAD 	// 최근 commit 취소
  • reflog : 삭제된 commit id, branch 확인

    git reflog		// 삭제된 commit id, 브랜치 이름 확인
    git reflog --hard <commit id>		// commit id상태로 되돌리기
    
    git reflog |grep <브랜치 이름>
    git checkout -b <삭제한 브랜치 이름> <커밋 id>

참고

git book
reset 옵션 정리 blog
Git-reset과-revert-알고-사용하기
Git add, commit, push 취소하기

0개의 댓글