컨셉

일반 프로젝트(스케줄 예시) "project_schedule.svg", iseohyun.com, public

다음과 같은 프로젝트가 있다고 가정해봅시다.

  1. 공모단계: (교수(강사), 지자체, 발주자, 기업 등) 모집 주제와 범위, 전체 프로세스 일정(발표 시기와 장소), 채점 방식, 제출 양식 등을 공유(배포)합니다.
  2. 중간/최종 발표: ppt형태로 제출되며, 보완/요구사항이 있을 수 있습니다.
  3. 발표자료에는 사진과 데이터, 참조 문헌(사이트, 출처..)에 대한 정보를 수록해야 합니다.
  4. 발표자는 각 단계의 변경사항을 하나의 파일로(zip) 저장하고자 합니다.
    backup 폴더를 만들고, 이 폴더 안에 저장합니다.
  5. 변경 사항의 주요 내용은 "파일 이름.zip" 입니다.

위 내용으로 산출된 작업물은 "[그림 2] 프로젝트 예시"와 같이 작성 될 수 있습니다.

일반 프로젝트(폴더 예시) "project_example.png", iseohyun.com, public

발표자는 각 일정마다 제출한 양식을 back-up하였습니다. 이 내용을 자동으로 해 주는 프로그램이 git입니다. 가장 큰 차이점은 다음과 같습니다.

메뉴얼과 git의 비교
메뉴얼 git
저장폴더 backup .git
저장할 내용 변경시점에 사용된 파일 모두 변경시점에 변경된 파일만(메모리 절약)
저장 방법 하나의 파일로 압축 개별 압축
작업폴더
(최상위 폴더)
지금까지 작업한 모든 파일 최신 파일만 보관
변경이력 관리 압축파일명으로 기재 각 시점의 snap shoot(해당 시점의 파일 목록 및 데이터(변경 사항만))
log 기록(저장 일자 자동 기록, 여러줄 가능, 범인(잘못된 업로더) 찾기)
장점 직관적임 빠름, 메모리 절약, 공유가 쉬움(+github), 추적이 쉬움
단점 여러사람과 history 공유가 어려움 git프로그램 설치(링크)
명령어로만 제어 가능(사람손 X)
관리 잘못으로 .git이 비대해지는 문제
추가 기능 변경 내용 비교 기능(diff), 병렬 프로젝트 기능(branch), 서버 업/다운로드(push/pull/clone) 등..

이 모든 작업을 자동화하기 위해 다음과 같은 특징이 발생합니다.

Git의 특징
범주 내용 비고
충돌 방지 파일 이름이 (해쉬값)으로 변경 SHA-1 (160bit)
관리 대상 폴더 내 모든 파일(기본) - 변경된 파일 자동 검출
- 업데이트(commit)시 반영 파일 선별(수동, 반드시)

기본 기능 시나리오

scnario1.init
git 기본 시나리오 "git_basic.svg", iseohyun.com, public

프로젝트 생성(init)

이 명령어는 작업폴더 안에서 수행되어야 합니다(작업폴더가 생성되었고,).

git init

이 명령어는 .git폴더를 생성하고, .git 이하에 폴더/파일을 생성합니다. 생성된 폴더/파일의 목적은 다음과 같습니다:

git directory
순번 폴더/파일명 종류 상세
1 objects 폴더 관리할 파일을 압축 저장
2 logs 폴더 변경 이력을 저장
3 refs 폴더 branch(병렬구조), 원격서버 정보, tag(버전관리, 이력 추적 등 사용)
4 info 폴더 설정 정보 저장(예:info/exclude 무시파일 목록(.gitignore))
5 hooks 폴더 이벤트(commit, merge 등) 발생시 자동 수행할 스크립트
6 HEAD 파일 현재 최신 버전(참조 이름) 기록
7 index 파일 추적 파일 목록
8 config 파일 로컬 설정(user.name, user.email)등 저장
9 description 파일 저장소 설명을 기재
10 COMMIT_EDITMSG 파일 commit log에 작성할 내용을 담은 임시파일
새로운 git repo를 생성
C:\myProject> git init
Initialized empty Git repository in C:/myProject/.git/

변경된 파일 확인(status)

이 명령어는 작업폴더 내 변경된 파일이 있는지 검색합니다.

git status
변경된 파일 조회
C:\myProject> git status
On branch master

No commits yet

Untracked files:
  (use "git add ..." to include in what will be committed)
        file1.txt
        file2.txt
        source/

nothing added to commit but untracked files present (use "git add" to track)

※ 2개의 파일(file1.txt, file2.txt)과 1개의 폴더(source/)가 변경(생성)되어 있습니다.

업데이트 파일 지정(add)

이 명령어는 업데이트를 진행하기 전에, 어떤 파일을 업데이트에 적용할 것인지 지정하는 단계입니다. 이 단계에서 지정된 파일은 stage에 올립니다. 폴더 이름 기재시 폴더 이하 모든 파일이 staging 됩니다. 만약 폴더가 자기자신(.)인 경우 모든 파일을 staging합니다.

git add ['파일 이름' 또는 '폴더 이름']
trace할 파일 지정
C:\myProject> git add file1.txt source
C:\myProject> git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached ..." to unstage)
        new file:   file1.txt
        new file:   source/file1.txt

Untracked files:
  (use "git add ..." to include in what will be committed)
        file2.txt

※ file1.txt와 source폴더의 내용이 staging 되어 있습니다. stage를 해제하려면 rm --cached 명령을 수행하라고 안내하고 있습니다. 추가 stage를 진행하려면 add 명령을 수행하라고 안내하고 있습니다.

반영하기(commit)

stage된 파일들의 업데이트를 반영하는 명령어입니다.

git commit -m "로그 내용"
업데이트 반영
C:\myProject> git commit -m "version 1.0"
[master (root-commit) 505c895] version 1.0
  1 file changed, 0 insertions(+), 0 deletions(-)
  create mode 100644 file1.txt

※ 커밋을 진행하되, 로그 내용은 "version 1.0"으로 간략하게 작성하였습니다.

실행 예(미리 작성한 파일을 이용한 커밋):

C:\myProject> git add source
C:\myProject> git status
On branch master
Changes to be committed:
  (use "git restore --staged ..." to unstage)
    new file:   source/file1.txt

Untracked files:
  (use "git add ..." to include in what will be committed)
    file2.txt
    log.txt
C:\myProject> git commit --file .\log.txt
[master 60ddf2f] 업데이트 2.3.1
  1 file changed, 0 insertions(+), 0 deletions(-)
  create mode 100644 source/file1.txt
업데이트 2.3.1
  - 점프 버그가 수정되었습니다.
  - 리그 보상을 수정하였습니다(20G -> 30G)

결과 확인하기(log)

이 명령어로 지금까지 commit활동을 추적 할 수 있습니다.

git log
업데이트 history 조회
C:\myProject> git log
commit 60ddf2fa989e5882cf774c356b65693d6b7f25cb (HEAD -> master)
Author: Seohyun Jung <iseohyun@hanmail.net>
Date:   Sun Dec 22 18:24:37 2024 +0900

    업데이트 2.3.1
            - 점프 버그가 수정되었습니다.
            - 리그 보상을 수정하였습니다(20G -> 30G)

commit 505c895b58423b61a8a9c5319f79e81557a7012f
Author: Seohyun Jung <iseohyun@hanmail.net>
Date:   Sun Dec 22 18:03:27 2024 +0900

    version 1.0
git log --oneline
업데이트 history 조회(한줄로 출력)
C:\myProject> git log --oneline
commit 60ddf2fa989e5882cf774c356b65693d6b7f25cb (HEAD -> master) 업데이트 2.3.1
commit 505c895b58423b61a8a9c5319f79e81557a7012f version 1.0

기본 편집 기능

mv

trace중인 (적어도 on-stage) 파일의 이름을 변경합니다. object가 새로 생성되지 않고, tree의 내용 중, 파일 이름이 변경됩니다.

git mv [기존 파일명] [변경 파일명]

변경 전 file1.txt와 변경 후 file1_new.txt를 같은 object를 사용하고 있습니다.

move의 실제 동작
C:\myProject> git mv file1.txt file1_new.txt
C:\myProject> git add File1_new.txt
C:\myProject> git commit -m "filename change"
C:\myProject> git log --oneline
a9091c2 (HEAD -> master) filename change
78845b9 second commit
f39d6ff first commit
C:\myProject> git cat-file -p 78845b9
tree 222f4e19eaa3bcaa87c6c54b0d97eff8290c9b52
parent f39d6ffa8f9324f4e5a19425935aa3359a8ba7c5
author iseohyun <iseohyun@hanmail.net> 1735465083 +0900
committer iseohyun <iseohyun@hanmail.net> 1735465083 +0900

second commit

C:\myProject> git cat-file -p 222f4e1
100644 blob 10189fe9e039b47c352d9b0936d24f0f6fcffdbf    file1.txt
100644 blob 233789ce69d7450712c42402e4a4e2ae35bbad5c    hello.txt
040000 tree c52260cfb14342e8b1f92b891e008930702875f7    src

C:\myProject> git cat-file -p a9091c2  
tree 978bd10a0abed1e4b035f5f3e0bd10cfa70c99b4
parent 78845b9138519b6fa4d71a6b915789b666cf7665
author iseohyun <iseohyun@hanmail.net> 1735580478 +0900
committer iseohyun <iseohyun@hanmail.net> 1735580478 +0900

filename change

C:\myProject> git cat-file -p 978bd10
100644 blob 10189fe9e039b47c352d9b0936d24f0f6fcffdbf    file1_new.txt
100644 blob 233789ce69d7450712c42402e4a4e2ae35bbad5c    hello.txt
040000 tree c52260cfb14342e8b1f92b891e008930702875f7    src

restore

restore 용법
명령어 상태변화 해설
git restore --staged file1 on-stage -> off-stage 편집된 파일의 add를 취소
[문제해결: add 번복 참조]
git restore file1 off-stage -> last-commit 편집 후(add 이전)을 편집 전(commit)으로 되돌림
git restore --source=<commit hash> file1 -> commit hash 특정 상태로 되돌림

rm

파일을 삭제합니다. 이미 commit에 포함된 파일의 object는 삭제되지 않습니다.

rm 용법
명령어 상태변화 해설
git rm --cached file1 on-stage -> off-stage 편집된 파일의 add를 취소 [문제해결: add 번복 참조]
git rm file1 off-stage -> delete 이전 commit의 object는 남아있음 [문제해결: 큰 파일 업로드 실수 참조]

문제 해결

add 번복(rm, restore)

해당 파일의 이전 commit이 없는 경우

git rm --cached [파일명]
git rm --cached [폴더명] -r
C:\myProject> git rm --cached source/file1.txt
rm 'source/file1.txt'
C:\myProject> git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached ..." to unstage)
        new file:   file1.txt

Untracked files:
  (use "git add ..." to include in what will be committed)
        file2.txt
        source/

※ 'source/file1.txt'를 unstage하였습니다. 만약 폴더를 해제하려면 -r 옵션은 붙이면 됩니다.

commit이 있는 경우

git restore --staged [파일명]
C:\myProject> git add .
C:\myProject> git status
On branch master
Changes to be committed:
  (use "git restore --staged ..." to unstage)
        modified:   file1.txt
        new file:   file2.txt
        new file:   log.txt

C:\myProject> git restore --staged .\log.txt
C:\myProject> git status
On branch master
Changes to be committed:
  (use "git restore --staged ..." to unstage)
        modified:   file1.txt
        new file:   file2.txt

Untracked files:
  (use "git add ..." to include in what will be committed)
        log.txt

※ git add .(ALL) 모든 파일을 staging하였습니다. git restore 명령을 통해서 log.txt파일을 unstage하였습니다.

commit 번복(reset)

git reset [hash(최소 첫 4자리)]
변경된 파일이 없는 경우
C:\myProject> git log --oneline
commit 60ddf2fa989e5882cf774c356b65693d6b7f25cb (HEAD -> master) 업데이트 2.3.1
commit 505c895b58423b61a8a9c5319f79e81557a7012f version 1.0

C:\myProject> git reset 505c
Unstaged changes after reset:
M       file1.txt

C:\myProject> git log --oneline
commit 505c895b58423b61a8a9c5319f79e81557a7012f (HEAD -> master)
git reset의 종류 "git-reset.svg", iseohyun.com, public

reset의 옵션이 발생하는 이유는 reset의 시점 때문입니다. 작업 디렉토리에 유효한 수정이 남아 있는 경우 이를 버릴지(hard), 남겨둘지(soft) 결정해야 합니다. default option은 mixed입니다. 수정 내용이 유효한지 여부는 수정 사항이 있으면 보존하는 방식입니다.

log 번복(commit --amend)

git commit --amend

코드 예:

C:\myProject> git commit --amend
[master aa71507] version 1.0 (modified)
  Date: Sun Dec 22 18:03:27 2024 +0900
  1 file changed, 0 insertions(+), 0 deletions(-)
  create mode 100644 file1.txt

C:\myProject> git log --oneline
commit aa715074b07eed99a532d7274bb48bad8ff565ce (HEAD -> master) version 1.0 (modified)

※ 이 예제는 vi편집기에 대한 실습이 생략되어 있습니다. 기존 로그에 "(modified)"문구를 추가하였습니다. 만약 기존 파일(예:log.txt)를 이용하여 업데이트 하고자 하면:

git commit --amend -F .\log.txt

총정리

기본 명령어
수행내용 명령어 번복
새 저장소 생성 init -
현재 상태 확인 status -
반영 파일 선택 add [파일명] rm --cached [파일명]
restore --staged [파일명]
변경 사항 반영 commit [-m "message" | --file 파일명] reset [--hard | --mixed | --soft]
commit --amend
반영 history 확인 log [파일명]
관리중인 파일 이동 git mv [파일명] [새파일명]
파일 복구 git restore [파일명]
git restore --staged [파일명]
git restore --source=<commit hash> [파일명]
파일 삭제 git rm [파일명]
rm --cached [파일명]
용어
용어 설명
repo repository의 약자. (init이 된, .git폴더가 있는) git 저장소
working directory 작업 디렉터리, (git init이 된) 최상위 폴더
stage 변경 사항을 반영할 가상의 파일 리스트
staging: 리스트에 올리는 행위
unstaging: 리스트에서 제거하는 행위