Git 기초: add·commit·push 흐름 & 필수 명령 이해
1. Git 초기 설정
커밋 작성자(author) 설정
- 최초 1회 설정
- 만약 설정을 하지 않고 진행하면 commit 메시지를 남기는 상황에서 아래와 같은 에러 발생
$ git commit -m "Initial commit"
Author identity unknown # 이거 누가 쓴지 모르겠다는 의미
*** Please tell me who you are. # 님이 누군지 좀 알려줘..!
Run # 아래 이거 그냥 따라하셈
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'poeun@DESKTOP-21K4HL.(none)')
- 이 설정은 인증(로그인) 정보와는 전혀 상관없음
author 설정
$ git config --global user.email "rjsdudans@naver.com"
$ git config --global user.name "ingu627"
설정 확인
$ git config --global --list # -l(소문자)
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
filter.lfs.clean=git-lfs clean -- %f
user.name=ingu627
user.email=rjsdudans@naver.com
init.defaultbranch=master
(추가 설정) 커밋 편집기 변경
-
해당 명령어는 반드시 vscode가 설치되어 있어야 함
-
기본 텍스트 편집기를
vim
에서vscode
로 변경하는 것
$ git config --global core.editor "code --wait"
로컬 저장소의 git 히스토리 삭제
$ rm -rf .git
2. 파일 생성
폴더 생성
$ mkdir 폴더명
파일 생성
$ touch 파일명
파일 열기
$ vi 파일명
3. Git Basic
로컬 저장소 설정
# git 초기화 -> master 표시가 뜰 것!
# 결과적으로 해당 폴더에 숨기 폴더로 .git/이라는 폴더가 생김
$ git init
Initialized empty Git repository in F:/test/.git/
$ touch a.txt # a.txt 파일 생성
$ touch b.txt # b.txt 파일 생성
로컬 저장소의 git 삭제할 때
$ rm -rf .git
확인
$ ls -a
./ ../ .git/ a.txt b.txt
- a.txt와 b.txt를 touch로 만들어 놨다.
주의 사항!!
.git
폴더가 또 다른 폴더 내부에 있으면 안됨! (git 속 git은 절대 금지!!!)
status
현재 git이 관리하는 폴더의 파일과 폴더 상태를 알려주는 명령어(working directory & staging area를 확인하는 명령어)
$ git status # WD & SA를 확인하는 명령어!! 정말 정말 중요하기 때문에 습관적으로 사용해야 한다.
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.txt
b.txt
nothing added to commit but untracked files present (use "git add" to track)
add
WD -> SA로 올리는 과정
$ git add a.txt
$ git status
On branch master
No commits yet
Changes to be committed: # 커밋 되어질 변경 사항들
(use "git rm --cached <file>..." to unstage)
new file: a.txt
Untracked files: # git이 아직 변경 사항을 추적하고 있지 않은 친구들
(use "git add <file>..." to include in what will be committed)
b.txt
# 기본 -> commit을 위한 폴더 & 파일을 추가!
$ git add 파일/폴더이름
$ git add a.txt # 특정 파일을 WD -> SA
$ git add . # 해당 디렉토리(하위 디렉토리 모두 포함)의 모든 폴더 & 파일을 SA
$ git add my-folder/ # 특정 디렉토리를 WD -> SA
윈도우 git 에서 한글 파일이 깨져 나올 때 해결 방법
$ git config core.quotepath false
터미널 입력 시 파일명에 공백이 있을 경우 문제
- 터미널에서 명렁어 입력 시 파일명, 폴더명에 공백이 있으면 제대로 인식이 안된다. 이럴 때는 공백 앞에 역슬래시()를 넣으면 해결된다.
에러 - 공백일 있을 경우 에러가 난다.
$ git add useful_code/한글 파일 깨짐 방지 코드.ipynb
fatal: pathspec 'useful_code/한글' did not match any files
해결법 - 역슬래시를 붙이면 제대로 된다.
$ git add useful_code/한글\ 파일\ 깨짐\ 방지\ 코드.ipynb
수정되거나 삭제된 파일 반영
git add -u
또한 git commit 에서 -a옵션을 붙여주게 되면 수정되거나 삭제된 파일만 commit하게 해준다.
git commit -a -m'message'
=> push까지 하면 github에 제대로 반영이 된다.
commit
- commit을 통해서 하나의 버전으로 기록됨
- commit 메시지는 현재의 변경 사항을 잘 나타내도록 ‘잘’ 써야함
- commit 내역은
$ git log
라는 명령어로 확인할 수 있음
commit 메시지의 기본 구조
# -m(message)
$ git commit -m "남기고 싶은 메시지"
$ git commit -m "Initial commit"
[master (root-commit) acf00f0] Initial commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a.txt
commit 내역 확인
#
$ git log
commit acf00f0792da06e949f21997a4f4226dc64a3ea7 (HEAD -> master)
Author: ingu627 <rjsdudans@naver.com>
Date: Sun Nov 21 14:36:03 2021 +0900
Initial commit
commit 이력을 더 짧게 보기
$ git log --oneline
acf00f0 (HEAD -> master) Initial commit
git 로그 정지 탈출 방법
: Q를 누른다.
퀴즈! 만약 status를 여기서 찍으면 어떤 결과가 나올까?
- 현재
b.txt
가 WD에 위치해있기 때문에 빨간색으로 표시된다. a.txt
는 commit 명령어를 통해 하나의 버전으로 기록됨
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
b.txt
nothing added to commit but untracked files present (use "git add" to track)
원격 저장소(Remote repository)
기본 설정
- 내 로컬 컴퓨터의 폴더를 git이 관리하도록 설정한다.
# git으로 초기화
$ git init
Initialized empty Git repository in F:/TIL/.git/
# add & commit
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.txt
b.txt
nothing added to commit but untracked files present (use "git add" to track)
$ git add .
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: a.txt
new file: b.txt
$ git commit -m "Initial commit"
[master (root-commit) d2c6be1] Initial commit
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a.txt
create mode 100644 b.txt
- Github Repository 생성
public
은 다 볼 수 있는 저장소이다.private
은 나 또는 이 권한이 있는 유저만 볼 수 있는 저장소이다.
원격 저장소 등록 & 업로드 명령어
원격 저장소 추가
- 최초 1회만 설정하면 됨
# 원격 저장소 추가
# git아 원격 저장소 좀 등록해줘(add) origin이라는 이름(별명)으로 원격 저장소URL을
$ git remote add origin 원격저장소URL
- 원격저장소 url은 복사 붙여넣기 한다.
# 예시
$ git remote add origin https://github.com/IT3AI1/TIL.git
등록된 원격 저장소 확인
$ git remote -v
origin https://github.com/IT3AI1/TIL.git (fetch)
origin https://github.com/IT3AI1/TIL.git (push)
원격 저장소가 잘못 등록되어 삭제해야 하는 경우
$ git remote rm origin
$ git remote -v
원격 저장소에 나의 소스 코드 업로드
-
add, commit 이후에 원격 저장소에 나의 로컬 버전 기록을 업로드 하고 싶으면 push 진행
-
2.23 버전의 로그인 이슈
- 아래 명령어를 입력하고 다시 push 작업 진행
$ git config --global credential.git.github.com.provider generic
- 혹은 vscode에 git bash를 연결해서 push하면 문제 해결 가능
$ git push -u origin master # 첫 git push를 하면 아래와 같이 인증을 요구하는 화면이 나옴
Select an authentication method for 'https://github.com/':
1. Web browser (default)
2. Personal access token
option (enter for default): # 바로 enter를 눌러서 진행
# 조금 기다린 뒤에 github 사이트로 들어가서 새로고침하면 로컬 저장소에 있는 모든 내역이 업로드 된 것을 확인할 수 있음
# 이후에는 add & commit을 진행하고 push를 하면 새로운 버전을 원격 저장소(github)에 업로드 할 수 있다.
$ git push -u origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 214 bytes | 214.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/IT3AI1/TIL.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
기본 루틴
$ git add .
$ git commit -m "커밋 메시지"
$ git push origin master # git아 버전 이력을 push 해줘 어디로?! origin(우리가 붙인 별명)으로 master 브랜치를!
댓글남기기