12.30.금(Git/Github)

유희선·2022년 12월 29일
0

TIL

목록 보기
11/29
post-thumbnail

*해당 내용은 아래 블로그를 기준으로 작성되었습니다.
https://velog.io/@augus-xury/github-%EC%82%AC%EC%9A%A9%EB%B2%95-%EA%B0%84%EB%8B%A8-%EC%A0%95%EB%A6%AC

Git/Github 설명
1. Git
-버전 컨트롤 시스템 (version control system)
2. Github
-원격 저장소 (remote repository)

1. Github 시작
1) 계정 생성
2) repository 생성 (public = 공용, private = 개인)
3) commit할 폴더에 들어가서 git init
4) shell에 깃 설치
5) 토큰이나 ssh로 인증

  • mac > terminal
  • window > gitbash

2. Github 명령어


1) 기본
(1) $ git add (파일명) // 특정 파일 Index(Staging area)에 추가
(2) $ git add . // 현재 및 하위 디렉토리 모든 파일 index 추가
(3) $ git commit -am "(설명)" // local repository에 추가
(4) $ git push origin master // remote repository에 추가

2) 수정
(1) git commit --amend -am "(설명)"
(2) git commit --amend --no-edit //--no-edit 옵션은 설명 수정하지 않을 때


*상태 확인

3) 버전 확인
$ git --version

4) commit 이력 보기
$ git log

5) 원격저장소 확인
(1) $ git remote -v
(2) $ git remote add (이름) (url) // 원격저장소 추가

6) branch 목록 보기
(1) $ git fetch // 정보 업데이트
(2) $ git branch //local
(3) $ git branch -a //remote까지 확인

7) 파일 상태보기
$ git status


*branch

8) 새 branch 만들기
(1) $ git branch (브랜치명) // 브랜치만 생성
(2) $ git checkout (브랜치명) // 해당 브랜치로 이동
(3) $ git checkout -b (브랜치명) // 현재 커밋에서 브랜치 생성하고 이동
(4) $ git checkout (커밋아이디) -b (브랜치명) //해당 커밋으로 이동 후 브랜치 생성

9) master branch로 돌아가기
$ git checkout master

10) branch 목록 보기
(1) $ git branch //local
(2) $ git branch -a //remote까지 확인

+원격 branch 명령어
git ls-remote origin

11) branch 삭제
(1) $ git branch -d (브랜치명) //로컬 저장소에서 브랜치 삭제
(2) $ git push origin --delete (브랜치명) //원격저장소에서도 삭제

12) main branch 이름 변경
git branch -m main master
git fetch origin
git branch -u origin/master master
git remote set-head origin -a


* checkout vs reset

13) checkout
-다른 commit 보기, commit 이력은 그대로 있는 상태에서 현재 보는 시점을 변경
(1) $ git checkout (커밋아이디) // 커밋아이디는 git log로 확인
(2) $ git checkout master // 최근 커밋 상태로 돌아오기
(3) $ git checkout (브랜치명) // 해당 브랜치로 이동

14) reset

14-1) 로컬저장소에서 이전 커밋으로 돌아간 후, add 안 한 상태(unstage)로 만들기
워크 스페이스에 작업하던 내용 그대로 있음
(1) $ git reset (커밋 아이디)
(2) $ git reset HEAD^ //한 커밋 이전
(3) $ git reset HEAD^^ //두 커밋 이전
(4) $ git reset HEAD^^^ //세 커밋 이전

14-2) 이전 commit으로 돌아가면서 이후 commit 삭제 (하드 리셋)
(1) $ git reset --hard (커밋 아이디)
(2) $ git reset --hard HEAD^^

14-3) 이후 원격 저장소에는 삭제가 반영되지 않은 상태이므로 강제 commit
$ git push origin master --force

*checkout commit은 그대로 두고 HEAD(시점)만 이동,
reset는 삭제까지 진행

14-4) reset 대신 revert 사용 가능,
revert는 기준 commit 이후로 삭제하는 것이 아니라 현재까지의 이력을 남겨두고 과거 커밋으로 재수정
$ git revert (커밋아이디)


*저장소 내 파일 삭제

15) 로컬 영역에서 제거
$ git rm (파일명)

16) 리모트 영역에서 제거
1) $ git rm (파일명) --cached
2) $ git rm -r (폴더) --cached

★--chached는 리모트로 이미 push까지 했을 경우 붙이는 옵션
이후 commit하고 push 다시 해주면 remote에 반영

17) 처음부터 특정 파일 commit 안 하기
.gitignore파일을 생성해
git에 추가하고 싶지 않은 파일이나 폴더 리스트를 입력

<.gitignore 파일 목록 검색>
$ git ls-files -o -i --exclude-standard

-o : untracked files
-m : modified files
-d : deleted files
-c : cached files
-i : ignored files
--exclude-standard : .gitignore


*저장소에서 코드 가져오기

18) origin에서 코드 가져오기
$ git pull origin master

19) 원격 저장소에서 코드 가져오기
$ git clone (url) (저장할 폴더 이름)

20) 원격에서 로컬로 코드 가져오기 (병합 전 확인)
$ git fetch origin

21) 병합하기
-master branch로 이동 후, 병합하고자 하는 branch로 병합
(1) $ git checkout master
(2) $ git merge (브랜치명)

22) 특정 branch를 다른 branch의 코드로 대체
(1) $ git checkout (바뀔 브랜치)
(2) $ git reset --hard (타깃 브랜치)

0개의 댓글