22.04.01 TIL

조은지·2022년 4월 3일
0
post-thumbnail

Rename(파일 이름 바꾸기)

  • Worst
    ex) $ mv [바꾸기 전 파일명.확장자] [바꿀 파일명.확장자]
    : git상에서 기존 파일은 지워지고(deleted) 새 파일이 생긴 걸로 인식해서 다시 add해야함.
    또한, 나중에 다시 봤을 때 "내용은 같은데 지우고 새로운 파일이 생겼네? 왜지?" 라고 헷갈릴 수 있음.
* 여기서 다시 mv로 원래 이름으로 바꿔주면 $ git status했을 때도 원래대로 돌아감!

Q. 이름 바꾸기 전 파일(지워진 파일)인데 왜 add, commit이 가능한가?
A. ???

  • Best !
    ex) $ git mv [바꾸기 전 파일명.확장자] [바꿀 파일명.확장자]
    : git상에서 renamed로 인식해서 다시 add할 필요없음.
  • 파일의 history를 남기기 위해서는 '삭제 후 생성'이 아닌 '이름 바꾸기'로 추적

Undoing(되돌리기/복구하기)

  • 형식: $ git restore [파일명.확장자]
    (이전엔 checkout이었음)
  • restoreundo(수정 내용 복구하기)!!!!!!
    (reset이나 cancel과 다름!!)
  • working 디렉토리에서 작업했던 부분을 되돌리는 것!

    : 즉, commit까지 했던 파일을 vi로 재수정 후, restore하면 수정 전(최신 commit 상태)으로 복구됨.

  • 여러 개 파일도 취소 가능!

    : $ git restore .하면 현재 위치 아래에 있는 모든 파일에 대해 restore(복구)를 실시

cf) shell 명령 취소는 ctrl+c / git 취소는 undo

Unstaging(내리기)

  • 형식: $ git reset HEAD [파일명.확장자]
=> $ git restore --staged [파일명.확장자]도 가능함!!
  • add했던 것(staging했던 것)을 내리는 것.
    (add이전 상태로!)
=> HEAD는 최신의 상태를 의미한다.
82103@LAPTOP-NBT556I5 MINGW64 ~/Documents/dev/revert-practice (main)
$ git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   LICENSE
# 스테이징된 파일이 있다!(커밋 준비 완료)

82103@LAPTOP-NBT556I5 MINGW64 ~/Documents/dev/revert-practice (main)
$ git reset HEAD LICENSE
Unstaged changes after reset:
M       LICENSE
# git reset으로 언스테이징해줌.

82103@LAPTOP-NBT556I5 MINGW64 ~/Documents/dev/revert-practice (main)
$ git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   LICENSE
no changes added to commit (use "git add" and/or "git commit -a")
# 스테이징된 파일이 없다는 내용 확인할 수 있음.

Edit latest commit(가장 최근 커밋 수정)

$ git commit --amend
: 수정 등의 문제로 직전 커밋 메시지를 다시 열고 싶을 때

Edit prior commit(과거 커밋 수정) =>권장 X

  1. $ git log로 커밋 이름 확인
    (위로 갈수록 최신 커밋)
  2. $ git rebase --interactive(혹은-i) [바꾸고 싶은 커밋의 직전 커밋ID]

    ex) // 위쪽이 최신 커밋 //
    commit 7fb9deb (HEAD -> master)
    commit 9b6b873
    commit 95b2f13
    commit cc28abb

    • 여기서 95b2f13의 내용을 바꾸고 싶다면 하나 이전인 cc28abb를 입력해야한다.
    • $ git rebase -i cc28abb로 바로 커밋 ID로 쳐도 되고,
      $ git rebase -i HEAD~3로 입력해도 된다.
  3. vim 창에서 바꿀 커밋의 pick부분을 edit으로 바꾸고 저장한다.
  4. $ git commit --amend를 입력해 수정하고 싶은 파일들을 수정하고, 완료됐을 때 $ git rebase --continue를 입력한다.

abort rebase(rebase작업 중지) =>권장 X

위의 과정 중 $ git rebase -i [커밋]을 통해 내용을 edit으로 바꾸는 등의 과정 중, $ git rebase --abort을 입력하면 rebase작업이 중지된다.

Complete rebase =>권장 X

$ git rebase --continue

Reset Commmit(작성했던 Commit을 지우고 싶을 때)

  • Worst case
    : Reset
    없던 일로 만들 수는 있지만 다른 협업 파트너에게 이미 갔다면, 다시 pull받을 때 파일이 살아올 수 있다!
    또한 과거 이력에서 깔끔히 사라져버려서 commit log 추적이 힘들어진다!

ex) commit후 push까지 한 직전 3개의 commit을 아래와 같이 삭제한 후, remote에 강제 push

$ git reset --hard HEAD~3
$ git push -f origin [branch이름]
  • solution : 잘못한 이력도 commit으로 박제하고 수정한 이력을 남기자!
  • Best case
    : Revert
    잘못하기 전 과거로 돌아가 최신을 유지하면서 되돌렸다는 이력을 commit으로 남겨 모든 팀원이 이 사항을 공유하고 주지시킬 수 있다.

ex) 현재 HEAD에서 직전의 3개의 commit을 순서대로 거슬러 올라가 해당 내역에 대해 commit, push 수행

# 현재 3개의 파일을 각각 add commit까지 완료한 상태임.
$ git revert --no-commit HEAD~3.. 
# 최신 상태로부터 한칸씩 순차적으로 올라가며 되돌려라! 라는 뜻(..은 상위니까 3번째 꺼부터 맨위까지를 가리킴)
# 그래서 --no-commit이 없으면 돌아갈때마다 하나씩 commit해야함!
82103@LAPTOP-NBT556I5 MINGW64 ~/Documents/dev/revert-practice (main|REVERTING)
$ git status
On branch main
Your branch is ahead of 'origin/main' by 5 commits.
	(use "git push" to publish your local commits)
You are currently reverting commit e8ce93c.
	(all conflicts fixed: run "git revert --continue")
	(use "git revert --skip" to skip this patch)
	(use "git revert --abort" to cancel the revert operation)
Changes to be committed:
	(use "git restore --staged <file>..." to unstage)
     	deleted:    a.md
      	deleted:    b.md
        deleted:    c.md
# 3개의 커밋을 지웠다는 내용이 남고 이걸 커밋으로 남겨주면 다른 사람들도 확인 가능
$ git commit
$ git push origin [branch명]
  • commit을 따로 안할땐 --no-edit
  • merge commit을 되돌릴 땐 -m
    ex) $ git revert -m {1 or 2} [merge commit id])

0개의 댓글