Git 명령어 정리

정민교·2023년 6월 5일
0

git

목록 보기
20/22

✔️git branch

📌브랜치 확인

local repo에 존재하는 브랜치들 확인

git branch

remote repo에 존재하는 브랜치들 확인

git branch -r

📌브랜치 생성

현재 브랜치 포인터가 가리키는 기준으로 새로운 브랜치 생성

git branch <branch-name>

📌브랜치 삭제

특정 이름을 가진 브랜치 삭제

git branch -d or --delete <branch-name>

삭제하고 싶은 브랜치 위에 있는 경우에 삭제가 되지 않는다. 다른 브랜치로 이동 후 삭제해야 한다.

또한 브랜치 삭제는 완전히 병합된 브랜치만 삭제가 가능하다.

브랜치 강제 삭제

git branch -d or --delete -f or --force <branch-name>
혹은
git branch -D <branch-name>

`-D``--delete --force` 의 shorthand임.

원격 브랜치 삭제

git push origin --delete or -d <remote-branch-name>

로컬 브랜치 삭제 후 원격 브랜치 삭제를 진행한다.

📌브랜치 이름 변경

git branch -m or --move <new-branch-name>

브랜치 이름 변경은 이름을 바꾸고자 하는 브랜치 위에 있어야 변경이 가능하다.

git branch -m <old-branch-name> <new-branch-name>

그렇지 않은 경우 변경할 브랜치를 특정하고 바꿀 브랜치 이름을 명시하면 된다.

✔️git merge

브랜치 병합하기

👉로컬에서 병합하기

git merge <branch-name>

병합을 위해서는 기준 브랜치로 먼저 이동한 후 merge 명령을 실행해야 한다.

현재 브랜치라 master 라면 master 브랜치에 <branch-name> 을 병합한다.

병합할 때는 fast-forward-merge 혹은 병합 커밋 생성 하는 병합이 있다.

✔️git diff

변경사항 확인

👉working tree와 인덱스(staging area) 간의 차이 확인

git diff

👉staging area와 마지막 커밋 간의 차이 확인

git diff --staged or --cached

👉마지막 커밋 이후 발생한 모든 변경 사항 확인

git diff HEAD

👉마지막 커밋 이후 발생한 모든 변경 사항에 대해 특정 파일 변경 사항 확인

git diff <filename> ...

추가로 파일을 입력해 두 개 혹은 세 개 등 여러 파일의 변경사항을 확인 가능

👉branch 간의 차이 비교

git diff <branch-name> <branch-name>

👉commit 간의 차이 비교

git diff <commit-hash> <commit-hash>

✔️git stash

임시 저장하기

👉stash 영역에 임시 저장하기

git stash (save)

👉가장 최근에 stash 영역에 임시 저장한 stash 적용 및 삭제하기

git stash pop

👉가장 최근에 임시 저장한 stash 적용하기 (삭제는 X)

git stash apply

👉stash 영역에 있는 stash들 확인하기

git stash list

👉특정 stash 삭제하기

git stash drop stash@{<stash-number>}

👉stash 영역 비우기

git stash clear

✔️git checkout

특정 커밋으로 이동하기

👉이전 커밋으로 돌아가기

git checkout <commit-hash>
git checkout HEAD~1

헤드 참조

✔️git restore

변경사항 되돌리기

👉마지막 커밋 기준으로 발생한 변경사항 되돌리기

git restore <filename>

👉특정 커밋 기준으로 변경사항 취소하기

git restore --source HEAD~1 <filename>
or
git restore --source <commit-hash> <filename>

--source 의 기본값은 HEAD입니다.

👉staing area에 올란 변경사항 내리기

git restore --staged <filename>

✔️git switch

브랜치 이동하기

👉다른 브랜치로 이동하기

git switch <branch-name>

👉새로운 브랜치 생성 및 이동하기

git switch -c <branch-name>

✔️git reset, revert

커밋 취소하기, 되돌리기
차이점은 git reset, revert 포스팅 참조하세요
https://velog.io/@alsry922/%EC%BB%A4%EB%B0%8B-%EC%B7%A8%EC%86%8C%ED%95%98%EA%B8%B0

👉커밋 삭제하기 mixed

git reset <commit-hash>

특정 커밋 해시를 가진 커밋 이후의 커밋들을 삭제 후 working tree에 커밋 내용 복구

👉커밋 삭제하기 hard

git reset --hard <commit-hash>

특정 커밋 해시를 가진 커밋 이후의 커밋들을 삭제 후 폐기

👉커밋 되돌리기

git revert <commit-hash>

특정 커밋 해시를 가진 커밋까지의 내용물로 되돌아가고 되돌아갔다는 커밋을 추가로 남김

✔️git clone

원격 저장소 clone 하기

👉 원격 저장소 로컬로 clone

git clone <remote-repo-url>

✔️git remote

👉연결된 원격 저장소 확인하기

git remote
or
git remote -v

👉로컬 저장소와 원격 저장소 연결하기

git remote add <remote-repo-name> <remote-repo-url>

👉원격 저장소 이름 바꾸기

git remote rename <old-name> <new-name>

👉원격 저장소 연결 끊기

git remote remove <remote-repo-name>

✔️git push

👉로컬 저장소 내용 원격 저장소로 업로드하기

git push <remote-repo-name> <branch>

위 명령어는 원격 저장소에 브랜치를 생성하는 역할도 한다.

👉로컬 저장소의 특정 브랜치 내용을 원격 저장소의 특정 브랜치로 업로드하기

git push <remote-repo-name> <local-branch>:<remote-branch>

✔️git pull

👉원격 저장소 내용 로컬 저장소로 내려받기

git pull <remote-repo-name> <branch>

pull은 fetch + merge 작업까지 같이 한다.

✔️git fetch

👉원격 저장소 내용 로컬 저장소로 내려받기(merge X)

git fetch <remote-repo-name> <branch>

원격 저장소 commit을 로컬 저장소로 내려받기만 하고 working tree까지 통합시키지는 않는다.

profile
백엔드 개발자

0개의 댓글