[Git] Git Basics
Study/Git

[Git] Git Basics

Snapshots, Not Differences

  • Git uses snapshot-based version control
    c.f. delta-based version control which is used in many other VCSs
  • Git stores the snapshots of every file modified in each version.
    Faster lookups, but uses more disk space

 

Delta-based
Snapshot-based

Git은 다른 VCS와 데이터를 다루는 방법에 차이가 있다.

VCS 시스템 대부분은 관리하는 정보가 파일들의 목록이다. CVS, Subversion, Perforce, Bazaar 등과 같은 delta-based VCS는 각 파일의 변화를 시간 순으로 관리하면서 파일들의 집합을 관리한다.

한편, Git은 데이터를 파일 시스템 스냅샷의 연속으로 취급하고 크기가 아주 작다. 파일이 달라지지 않았으면 Git은 파일을 새로 저장하지 않고 이전 상태의 파일에 대한 링크만 저장한다.

 

 

Nearly Every Operation is Local

  • Most operations in Git need only local files and resources to operate.
  • This is very convenient especially when the network is unavailable.
  • Local operations are done faster than remote ones.

Git은 거의 모든 명령이 로컬 파일과 데이터만 사용하기 때문에 네트워크에 있는 다른 컴퓨터는 필요 없다. 프로젝트의 모든 히스토리가 로컬 디스크에 있기 때문에 모든 명령이 순식간에 실행된다.

오프라인 상태이거나 VPN에 연결하지 못해도 commit이 가능하다.

 

 

Git Generally Only Adds Data

  • When you do actions in Git, nearly all of them only add data to the Git database.
  • It is hard to make the system to erase data in any way if you regularly push your local repo to the remote.

Git으로 무얼 하든 Git database에 데이터가 추가된다. 되돌리거나 데이터를 삭제할 방법이 없다. 다른 VCS처럼 Git도 커밋하지 않으면 변경사항을 잃어버릴 수 있지만 일단 스냅샷을 커밋하면 데이터를 잃어버리기 어렵다.

 

 

.git directory

If you initialize a Git repo (or cloning a Git repo), a hidden directory named ".git" is created.

This is the local database of the Git repo, and you must not delete or modify this directory.

 

 

Four States of Files

Your files in a Git repo will be in any of the following 4 states:

  • Untracked: The file is not under version control. Ignored by Git.
  • Unmodified: The file is under VC, but not modified. You don’t have to record changes for this file.
  • Modified: The file is under VC and modified. You should record the changes.
    수정한 파일을 아직 로컬 데이터베이스에 커밋하지 않음
  • Staged: The file is under VC, modified, and the changes to the file are ready to commit.
    현재 수정한 파일을 곧 커밋할 것
  • Committed: The changes were committed. This is the same as the “unmodified” state.
    데이터가 로컬 데이터베이스에 안전하게 저장됨

 

Why Modified and Staged?

Why is an extra state "staged"?

 

Suppose you modified 10 files. Sometimes, you want to record the changes only made to specific files.

e.g., commit the changes made to "interface.h"

You can commit files selectively by staging them only.

10 modified, 5 staged for commit

 

 

Three Main Sections

  • Working directory(or working tree) is a single checkout of one version of the project.
    • Initially, all files are in an unmodified state.
    • Modify some files (using a text editor).
    • Selectively stage files that you want to be part of your next commit (git add).
  • Staging area(or index) has files that are staged for the next commit.
    • Check out the staged files (git status), and do a commit (git commit)
  • Local Git repo(.git) stores commits.

Note that we don’t consider a remote repo for now.

git-scm.com

Git directory는 Git이 프로젝트의 메타데이터와 객체 데이터베이스를 저장하는 곳을 말한다. 이 Git directory가 Git의 핵심이다. 다른 컴퓨터에 있는 저장소를 Clone할 때 Git directory가 만들어진다.

Working directory는 프로젝트의 특정 버전을 Checkout한 것. Git directory는 지금 작업하는 디스크에 있고 그 디렉토리 안에 압축된 데이터베이스에서 파일을 가져와 working directory를 만든다.

Staging area는 git directory에 잇는 단순한 파일로 곧 커밋할 파일에 대한 정보를 저장한다.

 

 

Basic Git Workflow

- Addition

Suppose you want to add a new file "new.txt" to an existing Git repo.

  1. Create "new.txt" using your favorite editor (untracked for now).
  2. Add "new.txt" to the staging area by git add new.txt (staged).
  3. Commit the file by git commit (committed and unmodified).

untracked -> staged -> committed

 

- Modification

Suppose you want to modify the file "new.txt"

  1. new.txt is in an unmodified state.
  2. Modify new.txt (modified).
  3. Stage new.txt by git add new.txt (staged).
  4. Commit the file by git commit (committed and unmodified).

unmodified -> modified -> staged -> committed

 

- Deletion

Suppose you want to delete the file "new.txt".

  1. new.txt is in an unmodifiedstate.
  2. Delete new.txt (modified).
  3. Stage new.txt by git add new.txt (staged).
  4. Commit the file by git commit (untracked).

unmodified -> modified -> untracked

'Study > Git' 카테고리의 다른 글

[Git] Version Control System (VCS)  (0) 2021.09.13