[Git] 헷갈리는 git 명령어 모음

wisdom·2021년 3월 22일
0

언젠가는 Git을 반드시 마스터 하고

Overview


평소에는 VSCode에서 commit, push, pull, checkout, pull request 등 필요한 기능들은 모두 GUI로 이용할 수 있는데, 협업을 하다 보면 conflict를 관리할 때 골치 아픈 상황들이 종종 생기기 마련이다. 그런 상황에서 시행착오에 드는 시간을 줄이고자 트러블슈팅 내역을 아카이빙 한다.

rebase 절차


branch에서 PR을 넣기 전에 직접 master와 동기화 하는 작업이다. 이 과정에서 같은 커밋이 2번 되는 등 성가신 일이 많이 발생했다. 어찌됐든 마지막에 commit 통합으로 해결하면 된다 😎

1. master와 rebase

  • git pull --rebase master
  • merge...
  • git rebase --continue

2. branch와 rebase

  • git pull --rebase origin [branch]
  • merge...
  • git rebase --continue

3. branch에 push

  • git add [file path]
  • git commit -m "Rebase complete"
    • git rebase --continue를 더 해야 할 수도 있음
  • git push origin [branch]

4. Pull Request

commit 통합


가끔 rebase를 하다 보면 같은 커밋이 2번씩 push 되어 커밋 내역이 굉장히 지저분해질 때가 있다. 그럴 때 커밋을 통합해 주면 깔끔하게 레포를 유지할 수 있어서 좋다.

rebase

git rebase -i HEAD~2
  • 통합할 커밋 만큼 상대참조
git rebase -i HEAD~2

pick 1111111 first commit
pick 2222222 second commit
git rebase -i HEAD~2

s 1111111 first commit
pick 2222222 second commit
  • 합쳐질 커밋 앞의 pick을 지우고 squash 또는 s 입력한다.

  • 저장 후 나가면 커밋 메시지를 설정할 수 있는데, 나는 지우고 싶은 커밋 메시지에 # 주석 처리를 해주었다.

command

# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

push

git push --force origin [branch]

Reference

master, branch 동기화


아주 가끔 작업을 하다가 로컬 브랜치에서의 작업 내역과 관계 없이 무조건 master의 최신 코드를 동기화 하고 싶을 때가 있다. 그냥 pull 받고 merge 해서 conflict를 제거하면 되지만, 이러한 케이스에서는 그냥 강제로 동기화 시켜버려도 딱히 상관이 없다. 그럴 때 아래 명령어 3줄만 입력해 주면 간단히 해결된다.

git fetch --prune origin 
git reset --hard origin/master 
git clean
profile
블로그 이전 -> wisdom-lee.xyz

0개의 댓글