본문으로 바로가기

[Git] 시작하기/push/branch

category Skills/Git 2021. 10. 29. 23:41
반응형

Git 시작하기


1. 레포지토리 생성

https://github.com/new

 

GitHub: Where the world builds software

GitHub is where over 73 million developers shape the future of software, together. Contribute to the open source community, manage your Git repositories, review code like a pro, track bugs and feat...

github.com

  • README.md 생성
  • (optional) .gitignore 생성 - 언어 선택
.gitignore
  • 클라우드 rootkey, 시스템 캐시, 임시 파일 등이 push시에 올라가지 않도록 세팅한다.
    • .hidden/**   :   hidden 아래 모든 파일 무시       ex) hidden/hell.py
    • *.java   :   java파일 모두 무시       ex) server.java
    • rootkey.*   :   rootkey라는 이름의 모든 확장자 무시       ex) rootkey.csv

 


2. /Users/사용자/Documents/dev/

  • Documents 안에 dev폴더 생성
cd
ls
cd Documents
ls
mkdir dev
ls
cd dev
ls
  • ls로 확인작업

 

3. git clone

  • 레포지토리의 주소 복사
git clone 레포지토리 주소

 

4. Documents/dev/레포지토리/

cd 레포지토리
ls


5. README.md 수정

  • vi editor를 통해 README.md에 레포지토리에서 할 일을 명시해준다
vi README.md

 

  • 내용은 아래와 같이 한다
This is my first repository to learn git.
## Requirements
- python>3.8
- requests
- beautifulsoup4
- docker

## Installation

## How to contribute


6. 파일 생성

  • 원하는 파일을 생성해준다.
touch practice.py
vi practice.py
conda activate test
python3 practice.py
cat practice.py

 

  • test라는 이름의 conda에서 python3를 통해 파이썬코드를 실행해본다.
  • cat을 이용하여 파일 그대로를 살펴본다.

 

*5,6 단계 시에 레포지토리 내에서 수정사항이 발생한다. 이때, 아래 그림과 같이 흐름을 따라 github에 push한다.

출처 : https://uidaholib.github.io/get-git/3workflow.html

 

Git 다루기 1 - push

 

1. working directory에서 작업한 내용을 staging area에 올린다.

git status
git add practice.py
git status
  • git status를 통해 잘 실행되었는지 항상 상태를 확인한다.

2. staging area에서 local repository로 commit한다.

  • commit과 함께 commit convention 규칙에 따라 메시지를 입력한다.
git commit
git status
  • commit에 들어가지 않아도 바로 메시지를 입력할 수도 있다.
git commit -m "feat: Create practice.py"
Commit convention 규칙
  • 동작 가능한 최소단위로 자주 commit을 실행한다.
  • 메시지는 commit의 구성과 의도를 충실히 작성한다.
  • 제목은 축약하여 쓰고, 한 줄 띄워서 내용은 문장형으로 작성하여 추가설명한다.
  • prefix를 사용하여 한 눈에 커밋의 용도를 알기 쉽게 한다.
    • feat: features - 새로운 기능에 대하여       ex) Add server.py
    • docs: documnetations - 텍스트 파일       ex) Add README.md, LICENSE
    • conf: configuration - 환경설정, test 과정 중 발생한 부산물들       ex) Create .env, .gitignore, dockerfile
    • test: test
    • fix: bug-fix - 잘못 동작하고 있는 코드를 바로 잡았을 때      ex) Fix Typo server.py
    • refactor: refactoring - 성능을 개선하여 더 잘 작동하도록 했을 때       ex) Refactor user classes
    • build: build
    • perf: performance
Commit 메시지 수정하기
git commit --amend

* 되도록이면 되돌아가면 안된다, 잘못 쓴 것은 잘못 쓴대로, 너무 오래 전 것을 되돌리면 안된다.

 

 

3. local repository에서 remote(github)에 push한다.

  • commit된 파일들을 github에 있는 origin 원격장소의 main 브랜치에 push한다.
git push origin main
  • -u 옵션을 적어주면 main이라는 현재 브랜치를 자동으로 origin이라는 원격저장소의 main 브랜치로 연결해 간단히 git push만 입력하거나 git pull을 입력할 때 origin이라는 원격저장소의 main 브랜치를 로컬 저장소의 main 브랜치로 merge 하도록 하겠다는 것이다.
git push -u origin main
  • 이때, 처음 push시에는 토큰이 필요하다.
    • 발행시에는 기한 무제한 + 모두 체크를 해줘야한다.
    • 한번 발행된 토큰은 다시 보여주지 않기 때문에 잘 보관해두거나 push시 잘 입력해두어야한다.
    • https://github.com/settings/tokens
 

GitHub: Where the world builds software

GitHub is where over 73 million developers shape the future of software, together. Contribute to the open source community, manage your Git repositories, review code like a pro, track bugs and feat...

github.com

 

Git 다루기 2 - branch

  • 독립적으로 어떤 작업을 진행하기 위한 개념
  • 각각의 브랜치는 다른 브랜치의 영향을 받지 않기 때문에, 여러 작업을 동시에 진행할 수 있다.

출처 : https://zepel.io/blog/how-to-create-a-new-branch-in-github/

 

1. 브랜치 생성

git branch 브랜치이름
git branch
  • git branch를 통해 현재 브랜치들을 확인한다.

 

2. 브랜치 변경

git switch 브랜치이름
  • 다른 브랜치로 이동할 수 있다.
git branch checkout 브랜치이름
  • 구버전에서 동일한 코드이다.

 

3. 브랜치 합치기

git merge 브랜치이름
  • main 브랜치를 선택한 후 다른 브랜치와 합친다.
  • main에 합친 후에는 보통 브랜치를 삭제한다.
  • 충돌 시에는 충돌된 부분만 수정 후에 add, commit한다.

 

4. 브랜치 삭제하기

git branch -D 브랜치이름
  • 브랜치를 삭제한다.

 

보조 브랜치에서 작업 -> 메인 브랜치와 merge -> 메인 브랜치를 push

 

 

Git 수정하는 법 3가지

출처 : https://uidaholib.github.io/get-git/3workflow.html

1. 변경사항을 취소하고 싶을 때 = Undoing

  • 아직 staging에 올리지 않은 상태에서 되돌리기
git restore hello.py
  • 저장했던 최신 커밋으로만 돌아갈 수 있다.
git checkout -- hello.py
  • 구버전에서 checkout을 활용하여 표현가능하다.

 

2. staging에 올리면 안되는데 올린 경우 = Unstaging

git reset HEAD hello.py
  • 작업을 했던 최신의 공간(HEAD)에서 staging에 올린 것을 다시 내린다.

 

3. 완료한 commit 취소하기 = Unstaging and Remove

git revert --no-commit HEAD~3..
  • 최신 브랜치에서 3개를 순차적으로 취소한다는 뜻
git reset --hard
  • 위 코드는 사용하지 않도록 한다.
  • 내가 위 코드로 삭제한 부분을 다른 사람이 가져간 적이 있다면 계속 따라오게 된다.
  • 따라서 revert를 통해 지운 기록도 같이 남기도록 취소하는 것이 좋다.

 

반응형

'Skills > Git' 카테고리의 다른 글

[Git] Commit Convention 규칙  (0) 2021.11.20
[Git] 협업프로젝트를 위한 fork/fetch/pull  (0) 2021.10.30
[Git] 깃 입문하기  (0) 2021.10.29