English
Git Intro

Git Intro

A summary of storage systems prior to Git, as well as Git's operation and commands.

Intro

This post summarizes the essential information I gathered while studying Git. Unless there are significant changes or further study is required, I believe this will serve as both an introduction and the final post.

What is Git?

It is a type of version control tool that records changes to files and allows the records from a specific point in time to be reused.

Local Version Control

  1. Copying Directories
  • The most naive method

  • You might accidentally modify files incorrectly or copy them incorrectly.

  1. VCS (Version Control System)
  • Manages file change information using a database.

  • Revision Control Systems are a representative example.

  • Manages Patch Sets (parts of a file that are changed).

  • By utilizing a series of Patch Sets, all files can be reverted to a specific point in time.

Centralized Version Control

  • Collaboration with other developers is frequent when working on a project.

  • In such cases, it is advantageous to have a separate server managing files, where clients receive and use files from the central server.

  • If a problem occurs with the central server and there is no system backup, the entire history can be lost; this is a problem that also existed locally.

Distributed Version Control System

  • It replicates the repository and history simultaneously, rather than simply storing snapshots.

  • If a problem occurs on the server, work can be resumed using the clone.

  • It is also possible to restore the server by selecting any client.

  • The existence of remote repositories enables various forms of collaboration.

  • GitHub is also a type of remote repository.

General Usage

Configuration and File Access Commands

# 전역 사용자 설정
git config --global user.name {name} # 이름
git config --global user.email {email_address} # 이메일

# 로컬 사용자 설정 (해당 디렉토리에서 실행)
git config user.name {name} # 이름
git config user.email {email_address} # 이메일

# 설정 조회
git config --list

# 생성
git mkdir {path}/{folder_name} # 폴더
git touch {file_name} # 파일
git init # 파일 관리 시스템

# 저장소 및 히스토리 복제
git clone {repository_url}

# 원격 저장소
git remote add {remote_repo_name} {remote_repo_url} # 추가
git remote -v # 정보 확인

# commit history
git log
git log --oneline # 정보 표시 간략화
git log --all # 현재 head 이후의 최신 기록들을 포함한 전체 정보를 표시
git log --graph # 브랜치를 시각화하여 분기를 표현한 그래프 형태로 정보를 표시

Commands Used for Repository Management

git status # 변경사항을 확인하는 명령어
git commit -m {message} # 현재 스테이지에 존재하는 변경 사항을 커밋

git add {file_name} # 특정한 파일을 스테이지에 추가하는 경우
git add . # 해당 레포지토리의 변경사항을 전부 스테이지에 추가하는 경우

Git Undoing

Clearing the Stage

git rm --cached # root commit이 존재하지 않을 때
git restore --staged # root commit이 존재할 때

Modifying a Commit

git commit --amend 
# 스테이지에 파일이 없는 경우: 직전 커밋의 메시지를 수정
# 스테이지에 파일이 있는 경우: 기존 커밋에 현재 스테이지를 더하여 덮어쓰기 진행

Reverting All Commits Up to a Specific Point

git reset --soft {commit_id} # 미래 파일들 전부 stage에 돌려놓음
git reset --mixed {commit_id} # default, 미래 파일들을 전부 working directory에 유지 (add 필요)
git reset --hard {commit_id} # 미래 파일 전부 제거

Removing Past Commits

git revert {commit_id} # 과거 commit을 제거 후, 제거했음을 commit으로 추가

Reset is unsuitable for collaboration because it reduces the number of commits; therefore, using revert instead of reset is recommended for collaboration.

Git Branch

Branch: A mechanism that helps divide the workspace and allow for independent work.

  • Allows you to maintain the original state. Safety

  • Increased collaboration efficiency by performing a single task on a single branch

  • In the case of Git, branch creation is fast and file sizes are small

Related Commands

# 조회
git branch # 로컬 저장소의 브랜치 대상
git branch -r # 원격 저장소의 브랜치 대상

# 생성
git branch {branch_name}
git branch {branch_name} {commit_id} # 특정 커밋 지점을 기준점으로 생성

# 제거
git branch -d {branch_name} # 병합 완료된 경우
git branch -D {branch_name} # 병합하지 못한 브랜치 강제 삭제

# 브랜치 이동
# 이동 전 반드시 현재 브랜치의 변경사항을 기록할 것
git switch {branch_name} # 이미 있는 브랜치로 이동
git switch -c {branch_name} # 해당 이름의 브랜치를 생성 후 이동
git switch -c {branch_name} {commit_id} # 특정 커밋 지점을 기준점으로 하는 해당 이름의 브랜치를 생성 후 이동

Git Merge

The process of merging branched branches

Types of Merging

  • Fast-Forward: Moves the commit pointed to by the branch to the very beginning

  • 3-Way Merge: A merge using the 2 most recent commits from each branch + the common ancestor of the two branches

  • Merge Conflict: Conflicts occur when the same file is modified => Resolve the conflict and then proceed with a 3-Way Merge

# 해당 브랜치를 현재 위치한 브랜치에 병합
git merge {merged_branch}

Git Fork

Clone a remote repository without ownership; mainly used when there are multiple collaborators, such as with open source projects

The process of my commits being reflected in the forked source

#
#    원본 원격 저장소 (upstream) <----------- 복제본 원격 저장소 (origin)
#                \           merge request      /|
#                 \                              |
#                  \                 push branch | pull master
#                   \                            |
#                    \     ( pull upstream )     |/
#                     ------------------> 로컬 저장소 (user)
#
  1. Clone the original repository to my remote repository

  2. From that remote repository to the local Save

  3. Create a branch locally, work on it, and commit the changes.

  4. Push a merge request for the changes to the original remote repository.

  5. Merge the changes in the original remote repository.

  6. Pull the changes from the original repository.

  • pull upstream

  • sync origin & pull origin

  1. Remove my merged branch locally.

댓글 작성

게시글에 대한 의견을 남겨 주세요.

댓글 0