git hub

JiWOn·2021년 10월 4일
0

GDSC 리액트 스터디 2021.10.04
Git에 대해 공부해오기

공부자료 : https://www.youtube.com/playlist?list=PLRx0vPvlEmdD5FLIdwTM4mKBgyjv4no81

git hub 시작하기

git 설치하기

  1. 가입하기
  2. repository 만들기
  3. https://www.git-scm.com/ 다운받기
  4. 명령 프롬포트에서 git 다운되었는지 확인 : git
  5. 환경설정
    git config --global user.name 내 이름
    git config --gloabl user.email 이메일

git test

  1. c드라이브에 폴더 만들기 ex) Education

  2. 명령프롬포트에서 디렉토리 이동하기
    cd c:\ Education

  3. git clone 명령을 이용하여 git-tutorial.git 이동하기
    git clone https://github.com/woOONi/git-tutorial.git

  • clone: 다운로드 명령

git hub으로 파일 올리기

  1. 명령 프롬포트에서 파일이 있는 디렉토리로 이동
    cd c:\Education\git-tutorial
  2. 정상적으로 수정이 된 파일을 Staging Area로 add 하기
    git add document.txt
  3. Staging Area에 있는 파일을 Local Repository로 commit
    git commit -m "Add Text File [document.txt]"
  4. git push를 해줘서 Remote Repository로 올려줌
    git push
  5. 깃허브에서 올라간 것을 확인할 수 있음.

소스코드 수정, git hub에 올리기

  • git status : 추가된 파일이나 수정된 파일, commit된 파일이 있는지 확인할 수 있음.

  • dir : 새롭게 만들어진 폴더가 github에 있는게 불러와졌는지 확인

  • git checkout : 수정된 파일을 원래 상태로 돌릴 수 있음
    git checkout -- my_module.py

  • git pull : github에 있는 파일 불러오기

  • git log : 반영된 내용들을 확인할 수 있음.

  • add된 파일을 reset 시키고 싶다면
    git reset my_module.py
    git reset --hard 로그번호 : 로그번호 뒤에 일어난 일들을 모두 삭제,Stagin Area 뿐만아니라 working directory에서도 삭제
    git reset --soft 로그번호 : HEAD가 가리키는 브랜치를 옮김
    git reset --mixed 로그번호 : Stagin Area에서만 사라짐 working directory에서는 삭제되지 않음.

  • 리셋하고 난 후에는 github와 다른 상태임. 따라서 강제 push 해야함
    git push -f
  • commit message 바꾸기
    git commit --amend

Git Branch 사용하기

브랜치란

동시에 여러 개발자들이 프로젝트에서 각기 다른 기능을 개발 할 수 있도록 함.

구성

통합 브랜치

  • Master Branch
    일반적으로 생성되는 브랜치, 배포가 가능한 수준의 안정화된 버전

토픽 브랜치

  • Develop Branch
    추후 개발된 것 뒤에 병합
  • Bug Fix Branch
    버그 수정하고 먼저 병합

실행

- 브랜치 확인

c:\tutorial\git-tutorial>git branch
* master   

- 브랜치 만들기

c:\tutorial\git-tutorial>git branch develop

c:\tutorial\git-tutorial>git branch
  develop
* master

- develop 브랜치로 head 옮기기

c:\tutorial\git-tutorial>git checkout develop
Switched to branch 'develop'

c:\tutorial\git-tutorial>git branch
* develop
  master

- 소스코드에 새로운 함수 만들기

c:\tutorial\git-tutorial>git add.
git: 'add.' is not a git command. See 'git --help'.

The most similar command is
        add

c:\tutorial\git-tutorial>git add .

c:\tutorial\git-tutorial>git commit -m "Add my_module [Mul]"
[develop e08eea6] Add my_module [Mul]
 1 file changed, 2 insertions(+)
: 헤드가 develop에 있어서 master에는 반영 안되고 당연히 git에서도 반영 안 됨.

- master로 헤드 옮기고 develop과 master 통합하기

c:\tutorial\git-tutorial>git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

c:\tutorial\git-tutorial>git merge develop
Updating b990b24..e08eea6
Fast-forward
 my_module.py | 2 ++
 1 file changed, 2 insertions(+)

- 브랜치 제거

c:\tutorial\git-tutorial>git branch -d develop
Deleted branch develop (was e08eea6).

c:\tutorial\git-tutorial>git branch
* master

브랜치 충돌 처리

c:\tutorial\git-tutorial>git branch
* master

c:\tutorial\git-tutorial>git branch develop

c:\tutorial\git-tutorial>git branch
  develop
* master

c:\tutorial\git-tutorial>git checkout develop
Switched to branch 'develop'

c:\tutorial\git-tutorial>git branch
* develop
  master

c:\tutorial\git-tutorial>git add .

c:\tutorial\git-tutorial>git commit -m "Add my_module [Div]"
[develop 5ea0a02] Add my_module [Div]
 1 file changed, 3 insertions(+)
  • 이 상태에서 소스코드 변경되면 서로 커밋 메세지도 다르고 내용도 다르기 때문에 병합이 불가능
  • 이때 병합을 해보면 아래와 같은 결과가 나옴
c:\tutorial\git-tutorial>git merge develop
Auto-merging my_module.py
CONFLICT (content): Merge conflict in my_module.py
Automatic merge failed; fix conflicts and then commit the result.
  • 소스 코드에서도 아래와 같은 결과로 보임
def div(a,b):
<<<<<<< HEAD
    return a//b 
=======
    return a/b
>>>>>>> develop
  • 코드 선택하고 삭제한 다음 저장하고 add함
c:\tutorial\git-tutorial>git add .

c:\tutorial\git-tutorial>git commit -m "Add my module [div]"
[master d33eac3] Add my module [div]

c:\tutorial\git-tutorial>git merge develop
Already up to date.

원격 저장소 관리

  • git을 이용하여 협업할려면 원격 저장소를 사용해야함
  • git remote : 원격 저장소 확인
  • git remote show origin : 상세내용 확인
  • git remote add 이름 깃허브주소 : 특정 원격저장소 등록
  • git remote -v : 전체 목록 확인
  • git remote rename 변경전이름 변경후이름 : 이름 바꾸기
  • git log origin/이름 : 로그 확인
  • git remote rm 이름 : 원격저장소 삭제

log 다루기

  • git log : 로그확인 (q로 나올 수 있음)
  • --stat : 해당 파일에 라인이 얼마나 추가되었는가
  • --graph : 브랜치와 병합정보를 그래프 형태로 출력함
  • --p : 커밋에 적용된 구체적인 항목들을 출력
  • --pretty : 커밋정보를 지정한 내용으로 출력

    oneline : 한 줄로 커밋내용 출력
    format 종류
    %h : 해쉬값 출력
    %an : 작성자 이름
    %ar : 작성날짜
    %s : 커밋 주제

c:\tutorial\git-tutorial>git log --pretty=format"%h -> %an, %ar : %s" --graph
*   formatd33eac3 -> woOONi, 21 minutes ago : Add my module [div]
|\
| * format5ea0a02 -> woOONi, 33 minutes ago : Add my_module [Div]
* | format601eca7 -> woOONi, 25 minutes ago : Add my_module [Div edit]
* | format253354a -> woOONi, 27 minutes ago : Add my_module [div]
|/
* formate08eea6 -> woOONi, 39 minutes ago : Add my_module [Mul]
* formatb990b24 -> woOONi, 57 minutes ago : Add my_module [Add & Sub]
* format545bdf5 -> woOONi, 60 minutes ago : Add my_module [Add]
* format2fc2b85 -> woOONi, 3 hours ago : Add Text File [documnet.txt]

README.md

소스코드 압축

git archive --format=zip master -o Master.zip
:명령 --포멧형태설정, 브랜치설정, output 이름.

특정 커밋 수정/삭제

git rebase -i HEAD~3 : 최근 3개의 커밋 수정(-i edit모드 나옴)

수정

수정하고자 하는 커밋을 reword 해서 변경

삭제

삭제하고자 하는 커밋을 drop해서 삭제

0개의 댓글

관련 채용 정보