Git Commands

J·2025년 6월 23일

환경설정
git config --global core.editor "nvim" 편집기를 기본값 vim이 아닌 neovim 사용

로그 보기
옵션 설명

  • --oneline 한 줄로 요약해서 보여줌
  • --graph 브랜치가 갈라지고 합쳐진 것을 보여줌
  • -(숫자) 해당 수만큼 최신 로그를 보여줌

용례
git log --graph --oneline

revert

요약

  1. 먼 과거의 commit으로 되돌아갈 때 오류가 난다면 한 단계 씩 revert를 하면 된다.
  2. 혹은 범위로 revert 하면 된다.

세부

관련 내용 StackOverflow: Revert a range of commits in git
A -> B -> C -> D -> E -> HEAD
현재부터 B로 돌아가기 위해선
git revert B^..D

switch vs checkout

요약

  • switch가 더 권장된다.
    - 더 안전함
    • 에러 로그가 더 자세함
  • chekcout은 이전의 commit들을 볼 때(시간여행 할 때)에만 사용

rebase

요약

  • git rebase <target> = "Rebase myself after head of target"
  • public branch는 rebase를 하면 안 된다.

on feature branch, git rebase main

// before
feature: A---B---C---D  ← feature's commits (you are here)
main:    A---B---E---F  ← main's head

// after `git rebase main`
feature: A---B---E---F---C'---D'  ← rebase myself after main's head
main:    A---B---E---F  ← main's head

public branch는 rebase하면 안 된다.

rebase를 하면 해당 브랜치의 commit 이력이 재작성된다. 이는 다른 공동 작업자가 이 브랜치에 pull, push 할 때 충돌을 일으킨다.

# Safe: Rebasing private work onto public base
git checkout private-branch
git rebase public-branch     # ✅ okay

# Dangerous: Rebasing public commits  
git checkout public-branch
git rebase any-branch        # ❌ DANGEROUS!

push -u

요약

  • upstream으로 설정하면 이후의 git push, git pull 에서 remote/branch 명시 불필요

세부

# Without -u, you have to specify remote and branch every time
git push origin main
git pull origin main

# After using -u:
# First time with -u
git push -u origin main

# Now Git remembers the connection, so you can just use:
git push    # Git knows to push to origin/main
git pull    # Git knows to pull from origin/main
git status  # Shows "ahead/behind" info relative to origin/main

0개의 댓글