Git (2) : Git Bash 단축 키 & 명령어 (Day 10)

코딩기록·2024년 10월 21일

단축키

  • 줄 삭제 : ctrl+u(앞), ctrl+k(뒤)

  • 이전 경로로 이동 : cd -

  • 폴더 생성하면서 그 폴더로 이동 : mkdir 폴더명 && cd $_

  • 파일 2개 이상 생성 : touch 1.txt && touch 2.txt



명령어

일반

  • 폴더 아예 삭제 : rm -rf 현재위치에서 폴더 경로
  • 파일(폴더)명 변경 : mv a/aba.txt a/abc.txt

git add, git commit

  • 파일 staging
    -- 2개 이상 : git add 1.txt && 2.txt
    -- 모두 : git add .

  • 파일 unstaging
    -- 새로 생성된 파일일 때 : git rm --cached 1.txt
    -- 기존에 있던 파일에 내용을 수정했을 때 : git retore --staged 1.txt

  • 마지막 commit 변경 : git commit --amend -m ""

  • gitignore
    -- touch .gitignore > start .gitignore > 폴더명/, ./*.txt, !test.txt

브랜치

  • 브랜치명 변경 : **git branch -m (기존 브랜치명) 새 브랜치명
  • 브랜치 만들기 :
    (1) 브랜치 만들기 : git branch new브랜치명 이동하고자하는브랜치
    (2) 브랜치 만들면서 이동 : git switch -c new브랜치명 부모브랜치명
  • 브랜치 삭제 :
    (1) git branch -d 파일명 : 현재 브랜치와 병합되지 않은 변경 사항 없을 때는 삭제 안됨
    (2) git branch -D 파일명 :현재 브랜치와 병합되지 않은 변경 사항 없을 때도 삭제 됨

git diff

  • git diff : 비교 대상이 'work directory' vs '현재 속해 있는 브랜치의 staging area/commit' 비교
    -- A 를 커밋 -> B를 Git Add만 함 -> C를 수정하는 상황이라면, C와 B를 비교

  • git diff HEAD : work directory와 최근 commit 비교

  • git diff --staged or git diff --cached : 스테이징 영역과 최근 커밋을 비교

  • git diff branch1...branch2 : 브랜치간 비교

  • git diff 커밋해시...커밋해시 : 커밋 간 비교

git stash

  • git stash [save] [파일명] "stash를 해두었습니다"
  • git stash pop : 가장 최근 stash 복원
  • git stash apply : 가장 최근 stash 복원하되, stash는 유지
  • git stash list : stash lis 보기
  • stash 없애기 :
    (1) git restore 파일명
    (2) git stash drop stash@{1}
    (3) git stash clear

commit -> commit 이동

  • branch 간 이동 : git swtich 브랜치명
  • branch가 없는 곳으로 이동 :
    (1) git checkout 커밋해시
    (2) git checkout HEAD~1

git restore <파일명>

  • 작업 디렉토리(스테이징, 커밋 X)의 변경사항을 특정 상태로 되돌림
  • 파일에만 적용가능!!
  • git restore <파일명> : 작업 디렉토리의 변경사항을 최신 커밋으로 변경
  • git restore --staged <파일명> : 스테이징에서 작업디렉토리로
  • git restore --source 커밋해시값/브랜치이름 <file> : 해당 파일을커밋해시값/브랜치 상태로 원상복구

git reset

  • restore와는 달리 commit(작업 디렉토리, 스테이징 x)을 특정 상태로 되돌림
  • restore와는 달리, --soft/--mixed/--hard 옵션을 통하여 파일 변경내용을 그대로 남겨 둘 지 옵션 선택 가능
  • (1) git reset <commit-hash/file/branch/HEAD~1> : default로 --mixed.
    (2) git reset --hard <commit-hash> : 작업 디렉토리 내용도 지정 커밋 상태로 되돌림
    (3) git reset --soft <commit-hash> : 파일 변경사항은 stage로 이동
    (4) git reset --hard ORIG_HEAD : reset 잘못했을 때 취소

git revert

  • git revert <취소할 commit-hash/HEAD>
  • reset이랑 결과는 같은데 용도가 다름 (취소 한다는 커밋이 또 생기는 거임) 공동작업할 떄 유용

0개의 댓글