git 사용법 정리

J. Hwang·2024년 6월 30일

git이란

git은 소스코드를 효과적으로 관리할 수 있게 해주는 소프트웨어이다. 공동 개발자들 사이에서 소스코드를 별도로 주고 받을 필요 없이, 같은 파일을 동시에 병렬 개발할 수 있도록 해준다. 개인이 브랜치를 통해 개발한 후, 본 프로그램에 합치는 방식으로 개발을 진행한다.

사용법 모음

1. git 최초 설정

  • 유저 설정
git config --global user.name "username"
git config --global user.email "id@mail.com"
  • 유저 설정 확인
git config --global user.name
git config --global user.email
  • color code output 설정
git config --global color.ui "auto"
  • 줄바꿈 문자열 관련 설정
git config --global core.autocrlf input    # for Mac and Linux
git config --global core.autocrlf true     # for Windows

OS 간에 줄바꿈 문자열이 다르기 때문에 이때문에 생기는 오류를 방지해줌

  • 기본 브랜치명 변경
git config --global init.defaultBranch main
  • default editor 설정
git config --global core.editor "nano"

위는 에디터를 nano로 설정하겠다는 의미이다. VS code 등 다양한 툴을 데이터로 설정할 수 있다.

2. 프로젝트 생성/업데이트 하기

  • git 연동

우선 repository로 지정할 디렉토리로 이동해서

git init

위 명령어를 입력하면 해당 디렉토리에 git이 연동된다. 그러면 .git이라는 디렉토리가 생기는데 여기에 프로젝트의 히스토리가 저장된다.

  • git 현재 상태 확인
git status
  • 파일 추가하기
git add filename.py
git add .    # 모든 파일을 추가하고 싶을 때
  • commit 메시지 작성
git commit -m "This is ver 2. Changed matching radius 3" -> 1"."

commit 메시지는 버전 (업데이트) 내용을 기록하는 것이다.

  • git add 취소하기
git rm --cached <file>
  • commit 이력 확인
git log
  • 파일 변경 내용 확인
git diff filename.py

여기서 git diff 뒤에 HEAD~1을 붙이면 가장 최신 버전과 그 전 최신 버전의 변경 내역을 확인할 수 있다. HEAD가 가장 최신 버전이고, HEAD~1, HEAD~2, HEAD~3, ...,는 각각 그 전 최신, 그 전전 최신, 그 전전전 최신 버전을 의미한다.

** 프로젝트 루틴을 정리하자면
git init (최초 1회) → git status → 파일 작성 → git add → git commit -m → 파일 수정 → git status → git diff → git add → git commmit -m (업데이트 내용 코멘트) → ...

3. 기타 기능

  • 파일 최신 버전 복구하기
    간혹 실수로 이름이 같은 파일로 덮어쓰기가 되거나 해서 파일이 유실될 수가 있다. 그럴 때 최신 버전으로 다시 파일을 복구하는 명령어가 있다.
git checkout HEAD filename.py

References

https://goddaehee.tistory.com/91
https://gin-girin-grim.tistory.com/10
https://steady-coding.tistory.com/277#google_vignette
https://swcarpentry.github.io/git-novice/02-setup.html
https://velog.io/@sangyeon217/git-config
https://otzslayer.github.io/git/2021/11/12/make-vscode-default-git-editor.html

profile
Let it code

0개의 댓글