Git 04. Local Repository

Jasmine·2023년 6월 3일
0

Git

목록 보기
2/7

Local Repository 생성

Workspace생성 (mkdir)

mkdir git_ws

Working Directory 생성
: workspace로 이동한 후 Repository생성

% cd git_ws
git_ws % mkdir test_project

Git Init

폴더에서 git을 초기화하는 명령어를 사용하면 해당 폴더를 git이 관리하기 시작
폴더가 repository가 됨. git이 폴더를 working directory로 인식하고 관리

git init

실습

git_ws % cd test_project
test_project % git init

git 확인 (ls -all)

test_project % ls -all	# -all까지 하면 숨김폴더까지 보임

파일 생성 (touch)

test_project % touch test.txt 	# 빈 파일을 생성

test_project % ls
test.txt 

git status (git에 존재하는 파일 확인)

test_project % git status 
On branch master	# 디폴트 브랜치가 마스트 브랜치임

No commits yet		# 아직 커밋은 없음

Untracked files:	# 워킹디렉토리에는 있는데 아직 스테이지에 오지 않은 파일
 (use "git add <file>..." to include in what will be committed)
 
test.txt

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

Git Add

Working Directory 에서 변경된 파일을 Index (stage)에 추가

git add <filename>
test_project % git add test.txt
test_project % git status
On branch master

No commits yet

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

Git Commit

Index (stage) 에 추가된 변경사항을 HEAD 에 반영 (확정)
버전이 매겨짐

git commit -m "commit 에 대한 설명" <filename> 
git commit -m "first commit" test.txt

[master (root-commit) e629372] first commit
1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt
 
git status

On branch master
nothing to commit, working tree clean

Local Repository에 Remote Repository 등록

remote repository 더하기

(1) 등록

git remote add origin https://github.com/<repository>.git

(2) + with Username and Token
매번 등록된 값으로 자동으로 push/pull 하게 됨

git remote add origin https://<username>:<token>@github.com/<repository>.git

Remote Repository 정보확인

git remote -v

origin  https://grownje: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@github.com/grownje/test_project.git (fetch)
origin  https://grownje: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@github.com/grownje/test_project.git (push)

Remote Repository에 변경내용 Push하기

  • Local Repository (HEAD) 에 반영된 변경내용을 Remote Repository 에도 반영하기 위해서는 Git Push 를 사용

Git Push

git push origin <branchname>
git push origin master
# 디폴트로 생성되는 branch는 master 혹은 main임

  • 이제 test.txt가 연결되어 뜨고
  • branch가 master로 설정되어있고 (아직 1 branch만 있음)
  • commit messege (first commit)도 써있음

Local Repository 에 Pull 하기

  • Remote Repository 의 내용에 맞춰 Local Repository 를 갱신하려면 Git Pull 사용

Add a README

  • 파일이 생성됨 (변경사항은 아직 remote repository에만 있음)
  • README.md (MarkDown의 약자)

Git Pull

git pull origin <branchname>
git pull origin master

remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 664 bytes | 27.00 KiB/s, done.
From https://github.com/grownje/test_project
 * branch            master     -> FETCH_HEAD
   286d271..8022ea2  master     -> origin/master
Updating 286d271..8022ea2
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 README.md

# README.md라는 파일이 하나 생겨났다는 내용 확인

잘 불러와졌는지 확인 : ls

ls -all

total 5
drwxr-xr-x 1 Jeong Eun Kwon 197121  0 Jun  7 00:21 ./
drwxr-xr-x 1 Jeong Eun Kwon 197121  0 Jun  3 21:11 ../
drwxr-xr-x 1 Jeong Eun Kwon 197121  0 Jun  7 00:21 .git/
-rw-r--r-- 1 Jeong Eun Kwon 197121 16 Jun  7 00:21 README.md
-rw-r--r-- 1 Jeong Eun Kwon 197121  0 Jun  3 21:13 test.txt

혼자 해봅시다

1. Local Repository 생성하기

• 위치 : git_ws 폴더 하위
• 이름 : exam_project

$ mkdir exam_project

2. 파일 생성 후 Git 으로 관리 시작하기

• 파일 : exam.txt
• Index 추가
• HEAD 등록

$ cd exam_project/

# 파일 생성
$ touch exam.txt

# init 명령
$ git init
Initialized empty Git repository in C:/Users/Jeong Eun Kwon/Documents/git_ws/exam_project/.git/

# Index 추가
$ git add exam.txt

$ git status
On branch master

No commits yet

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

# HEAD 등록
$ git commit -m 'add exam.txt' exam.txt
[master (root-commit) 9f110de] add exam.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 exam.txt

$ git status
On branch master
nothing to commit, working tree clean

3. Remote Repository 생성 하기

• 이름 : exam_project
• 빈 프로젝트

Git HUB 페이지에서 Repositories -> New

4. Remote Repository 등록 하기

• exam_project 의 Local Repository 에 앞서 생성한 remote repository 등록 후 확인
• token 은 앞서 수업에서 생성한 token 계속 사용

git remote add origin https://grownje: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
@github.com/grownje/exam_project.git
  • Git에서 복붙 : shift + insert or 마우스로

5. Local Repository 변경내용을 Remote Repository 에 반영하기

• commit 항목을 Remote Repository 에 반영
• Remote Repository 에서 exam.txt 확인

$ git push origin master

Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 211 bytes | 70.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/grownje/exam_project.git
 * [new branch]      master -> master

$ git status

On branch master
nothing to commit, working tree clean

6. Remote Repository 변경 내용을 Local Repository 에 반영하기

• exam.txt 파일 수정 : This is git exam.
참고 > 파일 수정하는 법

# 수정 후, 반영 전
$ ls
exam.txt

$ cat exam.txt
(아무내용안뜸)

# pull하기
$ git pull origin master

remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 637 bytes | 18.00 KiB/s, done.
From https://github.com/grownje/exam_project
 * branch            master     -> FETCH_HEAD
   9f110de..517a8e9  master     -> origin/master
Updating 9f110de..517a8e9
Fast-forward
 exam.txt | 1 +
 1 file changed, 1 insertion(+)

# 반영 후 확인
$ cat exam.txt
This is git exam.

7. Commit 만들고 각각을 Local 과 Remote 에 반영하기

• Commit 1 : Local Repository 에서 exam2.txt 생성 후 Remote Repository 에 반영, 확인

• Commit 2 : Remote Repository 에서 exam2.txt 수정 후 Local Repository 에 반영, 확인 - This is git exam2.

• Commit 3 : Local Repository 에서 exam3.txt 생성 후 Remote Repository 에 반영, 확인 - This is git exam3.

cat > exam3. txt
This is git exm3. # 넣고싶은 내용 쓰고, ctrl+D로 빠져나오기

cat exam3.txt # 내용 불러오기

• Commit 4 : Remote Repository 에서 exam3.txt 수정 후 Local Repository 에 반영, 확인 - This is commit exam.

profile
데이터직무를 위한 공부 기록

0개의 댓글

관련 채용 정보