Git 사용법, 명령어

김영빈·2022년 9월 9일
0

개발도상기(記,己)

목록 보기
6/7
post-thumbnail

📌 모든 명령어는 terminal(git bash)에서 작동한다.


✍ git hub에 push하는 순서

1) git bash 관리자 권한 실행
2) cd 명령어로 프로젝트 폴더 이동
3) git add 명령어로 파일 stage에 올리기(index 추가)
4) git commit 명령어로 local repository에 업로드(HEAD 등록)
5) git push 명령어로 해당 브랜치에 local, remote repository 동기화

✍ 기초 명령어

cd [directory]

- 폴더 이동

pwd

- 현재 경로 확인

ls (-a | -all)

- 해당 경로의 파일 확인
- (-all) : 숨겨진 파일명 및 권한 등 상세 내용 확인
- (-a) : 숨겨진 파일명 확인

mkdir [directory]

- 폴더 생성

touch [filename]

- 파일 생성

cat (> | >>) [filename]

- 파일 읽기
- (>|>>) 파일 없을 시, 파일 생성 후 내용 삽입(마지막 줄 작성 후 enter -> ctrl + d로 저장)
- (>) 파일 있을 시, 내용 덮어 쓰기(마지막 줄 작성 후 enter -> ctrl + d로 저장)
- (>>) 파일 없을 시, 원래 있던 파일에 이어 쓰기(마지막 줄 작성 후 enter -> ctrl + d로 저장)

git status

- git local repository 내의 상황과 파일 등을 알려줌

git log

- 현재 위치한 branch의 변경이력을 볼 수 있음

✍ Local repository 생성

git init

- 해당 경로를 empty local repository로 선언(git 관리 시작)
- 해당 폴더(프로젝트 루트)에서 최초 1회만 작동한다.

git add [filename]

- working Directory에서 Stage로 파일 올리기

git commit -m 'massage' [filename]

- Stage에서 remote repository로 파일 올리기
- 'massage' 포함하여 전달

✍ Remote repository 연결

git remote add [remote reponame][github repository 주소]

- 생성된 Local Repository가 있을 경우 github 내 remote repository에 연결
- https://[ID]:[토큰]@~~ 로 입력 시 아이디 토큰 한번만 입력 가능

git clone https://[username]:[token]@github.com/[reponame].git

- 생성된 Local Repository가 없을 때 github 내 remote repository를 가져오기
- Init, remote Repo 등록, Pull 과정을 한 번에 할 수 있다.
- repo를 만들 상위 폴더에서 진행
- ID와 토큰은 최초 1회 등록

git remote rm [remote reponame]

- 연결된 원격 저장소 삭제

git remote (-v)

- 현재 연결된 원격 저장소 확인
- (-v) 어떤 프로젝트와 연결되어있는지 확인 가능

✍ 변경내용 push, pull

git push [local reponame][branchname]

- 현재 연결된 원격 저장소 확인
- 최초 branch의 이름은 master

git pull [remote reponame][branchname]

- 원격 저장소의 내용 가져오기

✍ 취소, 변경 명령어

git reset HEAD [file]

- git add한 파일 취소

git reset (--mixed | --soft | --hard) HEAD^(~2)

- commit을 취소하고 해당 파일들은 워킹 디렉터리에 보존
- (--mixed) commit을 취소하고 해당 파일들은 unstaged(add 이전) 상태로 워킹 디렉터리에 보존(기본값)
- (--soft) commit을 취소하고 해당 파일들은 staged(add 이후) 상태로 워킹 디렉터리에 보존
- (--hard) commit을 취소하고 해당 파일들은 unstaged 상태로 워킹 디렉터리에서 삭제
- (~2) 마지막 2개의 커밋을 취소, 없으면 1개만 취소

git commit --amend

- commit한 git message 변경
- 기존 message를 지우고 새 message 입력 후 esc -> :wq -> enter

✍ Branch 관련

git branch (-r | -a == --all)

- local branch 조회
- (-r) remote branch 조회
- (-a, --all) local, remote 모두 조회

git branch (-d) [branchname]

- local branch 생성
- commite된 것이 있다면 그 이후 생성된 branch에는 commit 내용까지 복사됨
- (-d) branch 삭제 (머무르고 있는 branch는 삭제 불가)

git checkout (-b) [branchname]

- branch 이동
- (-b) 브랜치 생성 후 이동

git push origin [branchname]

- remote repo에 branch 생성

git push origin (-d == --delete) [branchname]

- remote repo에 branch 생성
- (-d == --delete) remote repo에 branch 삭제

✍ git editor 설정

git config --global core.editor ("editor name (--wait)")

- core editor 조회
- ("editor name") editor name으로 core editor 설정
- ("editor name --wait") core editor 실행 중인 동안 terminal 작동 중지

git config --global -e

- core editor 실행

✍ git diff

git diff [branch name][branch name]

- terminal로 두 브랜치 간 차이 확인

git difftool [branch name][branch name]

- 지정한 editor로 두 브랜치 간 차이 확인

git difftool [commit hash][commit hash]

- 지정한 editor로 두 commit 간 차이 확인

git difftool HEAD HEAD^

- 지정한 editor로 마지막 commit과 그 이전 commit 간 차이 확인

git difftool HEAD

- 지정한 editor로 마지막 commit과 현재 수정상황 간 차이 확인

git difftool [local_repo][remote_repo]

- 지정한 editor로 loca과 remote repository 간 차이 확인

✍ git merge

git merge [branchname]

- 현재 위치한 Branch에 다른 Branch 병합

✍ 오류 해결

rm -f .git/index.lock

- Another git process seems to be running in this repository,
오류 해결
- 해당 코드 입력 후 commit 재진행
profile
개발도상인 냄비짱

0개의 댓글