💽 CICD/Git

[Git] Git 사용법

heywantodo 2023. 3. 24. 16:12
728x90
반응형

Git 사용법

Git 설치 (CLI)

 

Git - Installing Git

This book was written using Git version 2.0.0. Though most of the commands we use should work even in ancient versions of Git, some of them might not or might act slightly differently if you’re using an older version. Since Git is quite excellent at pres

git-scm.com

 

Linux

  • CentOS (Fedora)
#패키지로 설치
sudo yum install git 

#소스코드로 설치
sudo yum install curl-devel expat-devel gettext-devel \
  openssl-devel zlib-devel
  • Ubuntu (Devian)
#패키지로 설치
sudo apt-get install git

#소스코드로 설치
apt-get install libcurl4-gnutls-dev libexpat1-dev gettext \
  libz-dev libssl-dev

 

Windows

  • 아래 링크에서 자신의 운영체제에 맞는 Git 다운로드

https://git-scm.com/download/win

 

Git - Downloading Package

Download for Windows Click here to download the latest (2.40.0) 32-bit version of Git for Windows. This is the most recent maintained build. It was released 10 days ago, on 2023-03-14. Other Git for Windows downloads Standalone Installer 32-bit Git for Win

git-scm.com

  • GUI까지 포함된 버전

https://desktop.github.com/

 

GitHub Desktop

Simple collaboration from your desktop

desktop.github.com

 

Git 최초 설정

설정 파일 위치

  • /etc/gitconfig

시스템의 모든 사용자와 모든 저장소에 적용되는 설정

git conifg --system 옵션으로 이 파일을 읽고 쓸 수 있음

 

  • ~/.gitconfig, ~/.config/git/config

특정 사용자 (즉, 현재 사용자) 에게만 적용되는 설정

특정 사용자의 모든 저장소 설정에 적용됨 

git config --global 옵션으로 이 파일을 읽고 쓸 수 있음

 

  • .git/config

Git 디렉토리에 있고 특정 저장소(혹은 현재 작업중인 프로젝트)에만 적용

git config --local 옵션을 사용하면 이 파일을 사용하도록 지정 가능 (기본적으로 옵션이 적용되어있음)

 

git 설정 파일의 우선 순위 

 

1️⃣ ./git/config

2️⃣ .gitconfig

3️⃣ /etc/gitconfig 

 

최초 설정

#사용자 설정
git config --global user.name "name"
git config --global user.emall hello@example.com

#설정 확인
git config --list

#특정 설정 값만 보고 싶을 때
git config [설정값]
ex) git config user.name

#도움말 보기
git help config

 

Git 저장소 만들기

 

기존 디렉토리를 Git 저장소로 만들기

#저장소가 될 디렉토리로 이동
cd /my_project 

#git 저장소로 만들기
git init

기존 저장소를 clone 하기

git clone <기존 저장소 url>

 

Git 저장소에 파일 Commit 하기

✔ Committed

: 데이터가 로컬 데이터베이스에 안전하게 저장됐다는 것을 의미함

 

✔ Modified

: 수정한 파일을 아직 로컬 데이터베이스에 커밋하지 않은 것을 의미함

 

✔ Staged

: 현재 수정한 파일을 곧 커밋할 것이라고 표시한 상태를 의미

 

파일의 상태 확인하기 

git status

#짧게 확인하기
git status -s
git status --short

README 파일 만들기

echo 'My Project' > README

파일을 새로 추적하기

  • 파일을 Staging Area에 추가
git add README
git status

Modified  상태의 파일을 Stage 하기

  • 로컬 데이터베이스에 저장하기 전 단계
vi README
(내용 수정)
git status

git add README
git status

파일 Commit 하기

  • 변경사항 커밋하기
git commit #아무것도 안하고 저장하면 commit 안됨
git commit -m "first commit" #메시지 저장
git commit -a #add와 commit을 한 번에 실행

git commit -a -m 'added new file"

Staged와 Unstaged 상태의 변경 내용을 보기

git diff
git diff --staged

 

Git  파일 무시하기, 변경하기, 삭제하기

 

파일 무시하기

 

.gitignore

 

GitHub - github/gitignore: A collection of useful .gitignore templates

A collection of useful .gitignore templates. Contribute to github/gitignore development by creating an account on GitHub.

github.com

  • 아무것도 없는 라인이나, '#' 로 시작하는 라인은 무시
  • 표준 Glob 패턴을 사용, 이는 프로젝트 전체에 적용 
  • 슬래시 (/) 로 시작하면 하위 디렉토리에 적용되지 않음 
  • 디렉토리는 슬래시 (/) 를 끝에 사용하는 것으로 표현
  • 느낌표 (!) 로 시작하는 패턴의 파일은 무시하지 않음

 

파일 이름 변경하기

git mv [변경할 파일] [변경할 이름]
git status

 

파일 삭제하기 

  • 디렉토리에서 삭제하면 삭제 안됨 ❌
git rm [삭제할 파일]
git status

 

참조

https://git-scm.com/book/it/v2/Per-Iniziare-Installing-Git

 

728x90
반응형