본문 바로가기
프로그래밍/Git

[git] 생성부터 원격 저장소로 push하기 (add, remote, pull, commit)

by 서울에서 살아가기 2023. 2. 8.

 

 

 

 

 

 

현시점 개발자에게 git은 기본 중의 기본입니다.

git을 사용하기 위한 명령어 init, add, commit, pull, push 등 여러 가지가 있습니다. 이번 내용에는 git 기본 명령어와 git 흐름(Workflow)에 대해 알아보겠습니다.

 

 

  git 흐름(Workflow)

git-Staging-Area
출처: https://iseunghan.tistory.com/322

Working Directory

  • git init 명령어로 설정해둔 디렉터리로 현재 내가 작업하고 있는 디렉터리 공간입니다.

Staging Area

  • git add 명령어 수행시 Working Directory에 있는 파일들이 Staging Area로 이동합니다.
  • 수정이 완료되어 commit을 기다리고 있는 파일들의 공간입니다.

Repository (local)

  • git commit 명령어 수행시 Staging Area에서 Local Repository로 파일들이 이동합니다.
  • git push 명령어를 통해 원격 저장소(Git Repository)로 이동할 수 있는 상태입니다.

 

 

  원격 저장소(git repository)에 push 하기 

1. git init

현재 디렉토리 경로를 git working directory로 설정하는 초기화 명령어입니다.

로컬 저장소로 사용할 경로로 이동하고, git init 을 진행합니다.

> cd 로컬 저장소 주소

> git init
Initialized empty Git repository in Z:/Dev/git/test/InitProject/.git/

 

해당 경로 안에. git 폴더가 생성됩니다.

 

2. git remote

로컬저장소와 git repository를 연결하기 위한 명령어 입니다.

> git remote add origin https://gitlab.tistory.com/Diagnostics/server/oemupdate_kmc/service_test.git
// 자신이 생성한 git clone 주소를 넣어주세요

> git remote -v
origin https://gitlab.tistory.com/Diagnostics/server/oemupdate_kmc/service_test.git
// 잘 연결 되었다면 연결된 주소가 표출됩니다.

 

연결 주소는 git repository로 이동하여 아래 이미지를 참고해 주세요.

git clone 주소

 

3. git pull

git pull 명령어는 git repository에 있는 파일들을 로컬 저장소로 내려받아 파일을 동기화합니다.

> git pull origin master
remote: Enumerating objects: 63, done.
remote: Total 63 (delta 0), reused 0 (delta 0), pack-reused 63
Unpacking objects: 100% (63/63), 41.93 KiB | 129.00 KiB/s, done.
From https://gitlab.tistory.com/Diagnostics/server/oemupdate/service_test
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master

 

git pull 명령어로 파일 동기화를 시키지 않으면 git push 명령어 수행 시 오류가 발생할 수 있습니다.

이 부분은 따로 다루도록 하겠습니다.

 

4. git add

파일을 working directory경로에서 staging area로 이동합니다.

먼저 로컬 저장소 경로에 업로드할 파일을 추가해 주세요. 저는 inputInit.txt 파일을 추가했습니다.

git add 명령어를 수행합니다.

> git add . // 모든 파일 추가

> git add 파일명 // 입력한 파일만 추가 (ex. git add inputInit.txt)

 

git add .(점)은 모든 파일을 추가한다는 의미이고, git add inputInit.txt(파일명)을 입력하면  해당 파일만 추가됩니다.

 

5. git status

현재 작업중인 디렉토리 상태를 보여주는 명령어입니다.

git add 수행 전 status를 확인해 보면 아래와 같이 Untracked files로 표시됩니다

> git status

On branch master
No commits yet
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        inputInit.txt
        
nothing added to commit but untracked files present (use "git add" to track)

 

git add를 수행하여 commit이 가능한 상태가 되었습니다.

> git status

On branch master
No commits yet
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   inputInit.txt

 

6. git commit

git commit 명령어를 수행하면 staging area의 파일들이 로컬 저장소에 저장됩니다.

> git commit -m 'Init project'  // 'Init project'는 커밋 메세지로 원하시는 내용으로 기입하시면 됩니다.

[master (root-commit) 884fc99] Init project
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 inputInit.txt

 

7. git push

git push 명령어를 수행하면 변경된 사항을 최종적으로 git repository(원격 저장소)에 저장합니다.

> git push origin master

Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 234 bytes | 117.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
To https://gitlab.tistory.com/Diagnostics/server/oemupdate/service_test.git
   c853fa6..386105b  master -> master

 

여기까지 git 초기화부터 git repository(원격 저장소) 저장까지 전체 명령어에 대해 알아보았습니다.

 

 

부록. 초기 세팅 완료 후 git repository에 추가로 업데이트하는 방법

git add, git commit, git push 이 세 개의 명령어만 반복해서 사용하시면 됩니다.

1. git add

> git add .

2. git commit

> git commit -m 'message'

3. git push

> git push origin master

 

 

 

 

 

 

 

반응형

댓글