article thumbnail image
Published 2022. 5. 22. 20:36

Git 다운로드

https://git-scm.com/downloads

해당 os 에 맞추어 설치

 

 

Git 흐름

  • 작업 공간 : 올리고자하는 파일이 있는 곳. 이 공간에 Git을 생성하여 형상 관리
  • 준비 영역 : add를 통해 작업 공간에 파일들이 저장.
  • 로컬 저장소 : 준비 영역에 있던 파일들을 commit하게 되면 로컬 저장소로 저장
  • 원격 저장소 : 로컬 저장소의 파일들이 push를 통해 원격 저장소에 업로드

 

 

default branch 를 main 으로 설정

$ git config --global init.defaultBranch main
$ git config --get  init.defaultBranch

구글링한 자료들에 의해 실행하였을 때 간혹 마스터 브랜치로 설정되어 있는 경우가 있음

이를 main 브랜치로 변경하는 설정

 

위 사진과 같이 main 이 출력되어야 정상적으로 작동 된 것임

 

 

 

사용자 config

$ git config --global user.name {username}
$ git config --global user.email {useremail}
$ git config --list

꼭 해야 하는 설정인지는 모르겠지만,

사용자의 이름, 계정 등을 설정하고 확인

필요 없다면 추후 삭제 예정

 

user.name=SeoJiwan

user.email=wana1997@naver.com

설정된 것을 볼 수 있음

 

 

 

프로젝트 init, add

$ cd {프로젝트 폴더}
$ git init
$ git add .
$ git status

git init ~ 현재 폴더를 로컬 저장소로 지정 (.git 폴더 생성)

git add . ~ 작업 공간의 파일들을 준비 영역에 추가

git status ~ 로컬 저장소의 현재 상태를 확인

 

add 취소

git reset {파일 또는 폴더명}

파일 또는 폴더 명 생략 시 전체 add 취소

 

 

commit

$ git commit -m "commit message"

로컬 저장소에 최종 저장, commit message 와 함께 commit 진행

 

 

 

remote

$ git remote remove origin
$ git remote add origin {Git Repository 주소}
$ git remote -v

기존 repository remote 제거 후,

로컬 저장소와 원격 저장소를 연결

 

Git Repository 주소는 위 사진과 같이 copy 할 수 있음

 

Git Repository 주소 연동 확인

 

 

push

$ git push origin main

로컬 저장소의 파일들을 원격 저장소로 push

 

위와 같은 에러가 발생한다면,

원격 저장소와 로컬 저장소의 상태가 달라서 나는 에러일 것임

pull 을 먼저 해주어 둘의 상태를 같게 한 다음 push 진행

$ git pull origin {branchname} --allow-unrelated-histories
$ git push origin main

 

 

파일  또는 폴더 삭제

$ git rm -rf {파일 or 폴더명}
$ git add .
$ git commit -m "commit message"
$ git push origin main

로컬 저장소 및 원격 저장소 파일 모두 삭제

 

$ git rm -r --cached {파일명}
$ git add
$ git commit -m "commit message"
$ git push origin main

로컬 저장소는 삭제하지 않고, 원격 저장소 파일만 삭제

 

 

다른 컴퓨터에서 작업한 것  pull

$ cd {directory}
$ git add .
$ git commit -m "commit message"
$ git pull origin main
$ git pull origin 브런치명 --allow-unrelated-histories // 강제풀

// 병합 후 The following untracked working tree files would be overwritten by merge: 에러 발생시
git clean -d -f -f
git pull origin main

// pull 은 정상적으로 되었지만, merge 에러 발생 시
$ git add .
$ git commit -m "commit message" // --> main|merging 이 사라진다.

 

$ git clone {Git Repository 주소}

최초로 받을 때는 git clone 만 실행 --> 디렉토리 전체가 로컬 저장소에 저장

 

 

다른 컴퓨터에서 작업한 것  push

$ git add .
$ git commit -m ""
$ git push origin main

// 위 실행시 에러 발생하는 경우
$ git add .
$ git commit -m ""
$ git pull
$ git stash
$ git add .
$ git commit -m ""
$ git push origin main

 

clone

$ git clone {git 주소} {폴더 이름}

 

특정 브랜치만 클론
git clone -b {branch name} --single-branch {git 주소}

$ git clone -b 1004_sjw --single-branch https://github.com/leegh519/ant.git

 

 

- Just Do It -

 

반응형
복사했습니다!