Git 로컬 작업 폴더 원격 저장소에 연결하기 (HTTPS 방식 기준)
1. 로컬 Git 저장소 초기화 및 원격 연결
1.1 Git 초기화
git init
- 현재 폴더를 Git 저장소로 초기화 (
.git
폴더 생성)
1.2 원격 저장소 연결
git remote add origin https://github.com/사용자명/저장소명.git
- GitHub에서 생성한 저장소 주소를
origin
이라는 이름으로 등록
2. GitHub Personal Access Token(PAT) 사용
GitHub는 2021년부터 비밀번호 인증을 중단하고 토큰(PAT) 인증 방식만 허용함
2.1 토큰 발급 방법
- GitHub 로그인
- 우측 상단 →
Settings > Developer settings > Personal access tokens
Tokens (classic)
→Generate new token
repo
,workflow
등의 권한 선택 후 생성
2.2 토큰 저장 방법
방법 1: Git 사용 시 직접 입력
git push origin main
- GitHub 사용자명 입력 후
비밀번호 자리에 토큰 붙여넣기
방법 2: 캐시 저장
git config --global credential.helper store
- 한 번 입력하면 이후 저장됨 (로컬 평문 저장 주의)
3. Git 기본 명령어 흐름
3.1 기존 원격 저장소 내용 가져오기 (선택)
git pull origin main --allow-unrelated-histories
- 로컬과 원격 저장소에 공통 커밋이 없을 때 충돌 방지용
- 초기 1회만 사용
3.2 작업 파일 스테이징
git add .
- 현재 폴더의 모든 변경 파일을 스테이징 영역에 추가
3.3 커밋
git commit -m "커밋 메시지"
- 변경 내용을 로컬 저장소에 기록
3.4 원격 저장소에 푸시
git push -u origin main
-u
옵션은 이후git push
만으로도 기본 원격 브랜치 지정됨
최초 연결시
git init
git remote add origin https://github.com/username/repo.git
git pull origin main --allow-unrelated-histories
git add .
git commit -m "초기 커밋"
git push -u origin main