Repository & Branch

InSung-Na·2023년 2월 21일
1

Part 06. Git

목록 보기
2/4
post-thumbnail

해당 글은 제로베이스데이터스쿨 학습자료를 참고하여 작성되었습니다

📌Local Repository

  • GIt이 관리하는 3가지 단계로 구성됨

  • Working Directory(작업공간) - 실제 소스 파일, 생성한 파일들이 존재

  • Index(Stage) - Staging area(준비영역)의 역할, git add 한 파일들이 존재

  • HEAD - 최종 확정본, git commit 한 파일들이 존재

git Workspace 생성

명령어

  • 해당폴더로 이동
    cd

  • 폴더 생성
    mkdir

  • 해당 폴더를 Git이 관리(Working Directory로 선언)
    git init

  • 해당 폴더내에 존재하는 것 조회
    ls

  • 숨김파일 포함 조회
    ls -all

  • git의 현 상태 출력: add 또는 commit 대상이 있는지 확인가능
    git status

  • Working_Directory에서 Index(Stage)로 추가
    git add filename

  • Index(Stage)에서 HEAD로 커밋
    git commit -m "comment" filename


Local Repository 만들고 test.txt 커밋하기

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents
$ mkdir git_ws

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents
$ cd git_ws

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws
$ mkdir test_project

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws
$ cd test_project/

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/test_project
$ git init;
Initialized empty Git repository in C:/Users/HPcom/Documents/git_ws/test_project/.git/

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/test_project (master)
$ ls -all
total 4
drwxr-xr-x 1 HPcom 197121 0 Feb 21 15:39 ./
drwxr-xr-x 1 HPcom 197121 0 Feb 21 15:39 ../
drwxr-xr-x 1 HPcom 197121 0 Feb 21 15:39 .git/

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/test_project (master)
$ touch test.txt

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/test_project (master)
$ ls
test.txt

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/test_project (master)
$ 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)

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/test_project (master)
$ git add test.txt

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/test_project (master)
$ git status
On branch master

No commits yet

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

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/test_project (master)
$ git commit -m "first commit" test.txt
[master (root-commit) 413203f] first commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/test_project (master)
$ git status;
On branch master
nothing to commit, working tree clean

Remote Repository 생성

  1. 깃허브 로그인

  2. "Create repository" 클릭


  1. Repository의 이름을 생성한 폴더명 "test_project"로 하고, 연습용이므로 Private로 설정 후 생성

  1. 토큰 생성을 위해 사용자메뉴 -> settings -> 좌측 하단의 Developer settings 클릭

  1. Personal access tokens -> Tokens(classic) -> Generate new token -> Generate new token(classic)

  1. 이름 설정, 기간 없음, repo 체크 후 밑으로 내려서 생성

  1. 생성된 토큰 코드 기록해두기

  1. 생성된 repository로 이동


  1. 코드 복사하기


Local Repository와 Remote Repository 연결하기

  • 연결코드
    • 유저명과 토큰을 입력하면 매접속시 입력불필요
      git remote add origin https://<username>:<token>@github.com/<repository>.git
  • 정보확인
    git remote -v

git bash 복사하기 : ctrl insert

git bash 붙여넣기 : shift insert

test_project 연결하기

g@DESKTOP-VO6Q7TM MINGW64 ~/Desktop/ZeroBase/ds_study/git_Ws/test_project (master)
$ git remote add origin https://InSung-Na:TOEKNCODE@github.com/InSung-Na/test_project.git
  • Remote Repository 정보확인
g@DESKTOP-VO6Q7TM MINGW64 ~/Desktop/ZeroBase/ds_study/git_Ws/test_project (master)
$ git remote -v
origin  https://InSung-Na:TOEKNCODE@github.com/InSung-Na/test_project.git (fetch)
origin  https://InSung-Na:TOEKNCODE@github.com/InSung-Na/test_project.git (push)

Push & Pull

  • Git push : Local 내용을 Remote에 전달하기
    git push origin <branchname>
  • Local의 commit한 test.txt를 Remote에 push하기
HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/test_project (master)
$ git push origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 212 bytes | 212.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/InSung-Na/test_project.git
* [new branch]      master -> master

  • Git pull : Remote 내용을 Local로 가져오기
    git pull origin <branchname>
  • Remote에서 README.md를 만들고 Local로 pull하기
HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/test_project (master)
$ 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), 669 bytes | 47.00 KiB/s, done.
From https://github.com/InSung-Na/test_project
 * branch            master     -> FETCH_HEAD
   413203f..67e73dc  master     -> origin/master
Updating 413203f..67e73dc
Fast-forward
 README.md | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 README.md

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/test_project (master)
$ ls
README.md  test.txt

📌Remote Repository

파일설명

README File

  • 프로젝트에 대한 설명, 사용방법, 라이센스, 설치방법 등에 대한 내용을 기술하는 파일

  • 프로그램 사용자(협업)를 위해 존재

.gitignore

  • Git 버전 관리에서 제외할 파일을 지정하는 파일

  • 사용자가 원하지 않는 파일들을 자동으로 commit 대상에서 제외시켜 줌


README file, .gitignore-Python 설정 후 생성


Default Branch

  • master : 로컬에서 만든 Git를 remote에 등록하면 master로 표기됨

  • main : 깃허브에서 만든 git은 main으로 표기됨

Branch명 변경하기

이미 생성된 respository의 main->master로 편집하기

※주의 : 이미 사용중인 다른 사람들에게도 영향을 끼칠 수 있음, 팀원과 합의 후 진행할 것

  • main -> View all branches -> 편집(펜) 클릭해서 수정 가능


앞으로 생성될 branch를 master로 설정하기

  • 사용자메뉴 -> settings -> Repositories

  • 수정 후 Update 클릭


Remote Repository 복제하기

  • git clone
    • git init, git pull 등을 한번에 수행
      git clone https://github.com/<repository>.git
  • HelloGit을 git clone 하기
HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws
$ git clone https://InSung-Na:TOKEN@github.com/InSung-Na/HelloGit.git
Cloning into 'HelloGit'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (4/4), done.

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws
$ ls
HelloGit/  exam_project/  test_project/

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws
$ cd HelloGit/

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/HelloGit (master)
$ ls -all
total 9
drwxr-xr-x 1 HPcom 197121    0 Feb 21 17:42 ./
drwxr-xr-x 1 HPcom 197121    0 Feb 21 17:42 ../
drwxr-xr-x 1 HPcom 197121    0 Feb 21 17:42 .git/
-rw-r--r-- 1 HPcom 197121 1928 Feb 21 17:42 .gitignore
-rw-r--r-- 1 HPcom 197121   10 Feb 21 17:42 README.md

📌Branch

명령어

Branch 조회

Local Branch 조회

git branch

  • Local branch 조회
HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/HelloGit (master)
$ git branch
* master

Remote Branch 조회

git branch -r

  • Remote branch 조회
HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/HelloGit (master)
$ git branch -r
  origin/HEAD -> origin/master
  origin/master

Local&Remote Branch 조회

git branch -a

  • Local&Remote branch 조회
HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/HelloGit (master)
$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

Branch 생성 및 이동

Local Branch 생성

git branch branchname

  • Local branch 생성
HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/HelloGit (master)
$ git branch branch01

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/HelloGit (master)
$ git branch
  branch01
* master

Local Branch 이동

git checkout branchname

  • branch 이동
HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/HelloGit (master)
$ git checkout branch01
Switched to branch 'branch01'

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/HelloGit (branch01)
$ git branch
* branch01
  master

Local Branch 생성&이동

git checkout -b branchname

  • branch 생성&이동
HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/HelloGit (branch01)
$ git checkout -b branch02
Switched to a new branch 'branch02'

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/HelloGit (branch02)
$ git branch
  branch01
* branch02
  master

Remote Branch 생성

git push origin branchname

  • local branch를 remote에 push
HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/HelloGit (branch02)
$ git push origin branch01
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: Create a pull request for 'branch01' on GitHub by visiting:
remote:      https://github.com/InSung-Na/HelloGit/pull/new/branch01
remote:
To https://github.com/InSung-Na/HelloGit.git
 * [new branch]      branch01 -> branch01

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/HelloGit (branch02)
$ git push origin branch02
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: Create a pull request for 'branch02' on GitHub by visiting:
remote:      https://github.com/InSung-Na/HelloGit/pull/new/branch02
remote:
To https://github.com/InSung-Na/HelloGit.git
 * [new branch]      branch02 -> branch02

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/HelloGit (branch02)
$ git branch -a
  branch01
* branch02
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/branch01
  remotes/origin/branch02
  remotes/origin/master


Branch 삭제

Local Branch 삭제

git branch -d branchname

  • 현재 접속 중인 branch는 삭제불가능
  • branch 삭제하기
HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/HelloGit (branch02)
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/HelloGit (master)
$ git branch -d branch02
Deleted branch branch02 (was c96194c).

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/HelloGit (master)
$ git branch -d branch01
Deleted branch branch01 (was c96194c).

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/HelloGit (master)
$ git branch
* master

Remote Branch 삭제

git push origin --delete branchname

  • Remote branch 삭제하기
HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/HelloGit (master)
$ git push origin --delete --delete branch02
To https://github.com/InSung-Na/HelloGit.git
 - [deleted]         branch02

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/HelloGit (master)
$ git push origin --delete --delete branch01
To https://github.com/InSung-Na/HelloGit.git
 - [deleted]         branch01

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/HelloGit (master)
$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

0개의 댓글