Git 명령어 기본

Git 명령어

git에는 다양한 명령어가 존재합니다.
명령어를 다 알면 좋지만, 기본적으로 사용하는 것만 알고 있어도 괜찮습니다.
이번 페이지에서는 기본적으로 사용하는 명령어들에 대해 배우고, 실습을 진행 해보겠습니다.

로컬 컴퓨터에서 하는 것

* Working Directory는 가상의 폴더

동작방식

1.
git init을 하면 기존에 있는 디렉토리를 Git 저장소로 초기화하는 작업이 되어 A. Working Directory (.git 폴더) 생성
2.
git add a.txt를 하면 A-1. Untracked Files에서 B. Stage로 이동
3.
a.txt 파일을 수정하면 B. Stage에서 A. Working Directory내에 있는 A-2. Tracked Files의 unstaged Files 로 이동
4.
다시 git add하면 B. Stage로 이동
5.
git commit 을하면 stage에서 history로 이동
6.
변경사항을 서버에 올림

기본 명령어

실습전 세팅

git init

로컬 저장소에서 Git 저장소를 초기화할 때 사용하는 명령어
Working Directory 생성하는 것

사용법

git init
Bash
복사
결과 이미지

git remote

로컬 저장소에 연결된 원격 저장소를 관리할 때 사용하는 명령어

사용법

# git remote add <원격저장소명> <저장소주소> # 해당 저장소를 origin이라고 원격저장소명을 저장한거임 git remote add origin "저장소 주소" # 등록된 원격저장소 확인 git remote -v
Bash
복사
결과 이미지

git status

현재 작업 중인 저장소의 상태를 확인할 때 사용하는 명령어

사용법

git status
Markdown
복사
결과 이미지

git diff

파일의 변경 사항을 확인할 때 사용하는 명령어

사용법

git diff
Markdown
복사
결과 이미지
git status 와 git diff 의 차이점
git status → 무엇이 바뀌었는지
어떤 파일이 변경되었는 지 확인
git diff → 어떻게 바뀌었는지 (구체적으로)
파일 내 어떤 내용이 수정되었는 지 확인

git difftool

파일의 변경 사항을 GUI 도구로 확인할 때 사용하는 명령어

사용법

git difftool # git difftool <commit-id1> <commit-id2>
Markdown
복사
결과 이미지

git add

파일을 stage 상태로 변경시킬 때 사용하는 명령어

사용법

# git add 파일명 git add main.txt
Markdown
복사
결과 이미지

git commit

파일의 변경 내용을 확정할 때 사용하는 명령어

사용법

# git commit -m “메모” git commit -m “first commit”
Markdown
복사
결과 이미지

git push

원격 저장소에 로컬 저장소에서 변경된 내용을 업로드할 때 사용하는 명령어

사용법

# git push <원격저장소명> <브랜치명> git push origin master # git push -u <원격저장소명> <브랜치명> git push -u origin main # git push --set-upstream <원격저장소명> <브랜치명> git push --set-upstream origin main
Markdown
복사
결과 이미지

git clone

원격 저장소에 있는 레포지토리를 ‘처음’ 복제해올 때 사용하는 명령어

사용법

# HTTPS 형식 # https://github.com/[USERNAME]/[REPOSITORY_NAME].git # SSH 형식 # git@github.com:[USERNAME]/[REPOSITORY_NAME].git git clone "원격 저장소 주소"
Markdown
복사
실습 전 세팅
결과 이미지

git pull

원격 저장소에 있는 레포지토리의 최신 내용을 로컬 저장소로 가져올 때 사용하는 명령어
git fetch + git merge
git fetch : 원격저장소의 내용을 가져오다
git merge : 내 브랜치에 병합하다.
웬만하면 pull하고 push하는 것을 권장함

사용법

git pull git pull origin main
Markdown
복사
결과 이미지

git log

commit 기록을 볼 수 있는 명령어

사용법

git log
Markdown
복사
결과 이미지

git show

commit 정보 볼 때 사용하는 명령어
특정 커밋에 대한 정보를 보여줌
Commit id, commit 메세지, 수정된 파일 목록, 변경 내용 등을 확인할 수 있다.

사용법

# git show <commit id> git show 352c9a
Markdown
복사
결과 이미지

git blame

수정 내역을 확인할 때 사용하는 명령어
파일에 각 라인이 어떤 커밋에서 수정되었는지 추적하는 명령
Commit id, 수정한 사람, 수정 이력이 남겨진 시간, 커밋 메세지를 확인할 수 있음

사용법

# 해당 파일의 모든 수정 내역 확인 # git blame <파일명> git blame main.txt # 해당 파일의 (description과 수정된 코드만) 수정 내역 확인 # git blame -s <파일명> git blame -s main.txt
Markdown
복사
결과 이미지

git stash

임시저장, 최근 커밋과의 차이점을 전부 보관

사용법

# git stash git stash save “메모란” # 보관된 코드 리스트 git stash list #최근 저장한 것만 꺼내어오기 git stash pop #특정 목록만 제거 # git drop <번호> git stash drop 0
Markdown
복사
결과 이미지
tip vscode에 gitlens extension이 있음

응용 명령어

git branch

브랜치를 생성,이동,삭제할 때 사용하는 명령어
여기서, 브랜치란?
순서
분석
단어
내용
1
일반 명사
branch
나뭇가지, 지사, 분점
2
고유 명사
branch
코드의 다른 버전을 관리하기 위해 사용되는 독립적인 작업 흐름, 공간
3
사용 이유
branch
프로젝트의 안정성을 유지하면서 새로운 기능을 개발하거나 버그를 수정할 수 있게 해주기 때문
4
사용 방법
branch
git branch 브랜치명으로 새 브랜치를 만들고, git checkout 브랜치명으로 만든 브랜치로 이동한 다음, 원하는 작업을 하고 git commit으로 변경사항
5
다른 기술과의 비교
branch
코드의 다른 버전을 관리하기 위해 사용되는 독립적인 작업 흐름을 뜻 합니다.

사용법

# git branch <브랜치명> git branch feature # merge 완료된 브랜치 삭제 git branch -d "브랜치명" # merge 안된 브랜치 삭제 git branch -D "브랜치명"
Markdown
복사
결과 이미지

git checkout

사용법

# git checkout <브랜치명> git checkout feature
Markdown
복사
결과 이미지

미니퀘스트

vscode를 설치합니다.
1.
git을 설치합니다.
a.
(winiows라면) git 환경변수 설정
2.
github에 가입합니다
3.
본인 계정명과 동일한 이름의 레포지토리를 생성합니다. (public으로 세팅)
4.
vscode를 실행하고 새로운 폴더에서 터미널을 엽니다.
5.
내 로컬 컴퓨터에 원격저장소 레포지토리를 clone합니다. (http 경로로 clone)
6.
내 로컬 컴퓨터 레포지토리에 아래의 코드를 옮겨놓고 꾸밉니다.
7.
배지 모양은 shields.io 에서 보고 정하시면 됩니다.
a.
잘 이해안되면 아래 custom badge를 보세요.
b.
기술의 아이콘과 색상코드는 simpleicons 사이트에서 확인할 수 있습니다.
8.
원격 레포지토리에 업로드합니다.
custom badge
<img alt="Python" src ="https://img.shields.io/badge/기술명-원하는색상코드.svg?&style=for-the-badge&logo=로고명&logoColor=로고색상"/>
Markdown
복사
## startupcode / 스타트업코드 안녕하세요! 🙋‍♂️ 개발자 입니다. 개발은 Programming이 아닌 Development라는 생각으로, 개발자 뿐 아니라 모든 직군이 함께 해야하는 것 이라고 생각합니다. 항상 이야기하는것을 좋아하고 비난이 아닌 비판은 언제든지 환영합니다. 🥰 모두 행복했으면 좋겠어요! <br /> <br /> <br /> ## My values 😎 모든것은 재미있어야 합니다<br /> 👌 안전성을 겸비한 속도를 지향 합니다<br /> 🦻 쉽게 말하고 어렵게 들으려고 노력합니다<br /> <br /> <br /> <br /> ## How i work 저는 항상 재미있게 살고 싶습니다. 사용자와 클라이언트를 동시에 만족 시키는 것 또한 재미있고, 새로운 기술을 배우는 것 또한 재미 있습니다. 어려운 프로덕트를 성공 시키는 경험또한 재미있고 기술을 쉽게 풀어내는 것 또한 재미있습니다 :) 그리고 무조건 약속한 시간을 지키기 위해 노력하며 단순히 약속한 시간을 지키는것이 아닌 최대한 완성도를 높이기 위해서 노력합니다! 저는 장비에 관심이 많아서 모든 장비가 세팅되어 있는 환경을 좋아합니다 :) 그래서 자택하는걸 좋아하지만 가끔은 카페에서 일하는것도 좋습니다. <br /> <br /> <br /> ## Skills #### I use it often. <div style="display:flex;gap:30px;flex-wrap:wrap;"> <img src="https://img.shields.io/badge/js-F7DF1E?style=for-the-badge&logo=javascript&logoColor=black"> <img src="https://img.shields.io/badge/ts-3178C6?style=for-the-badge&logo=typescript&logoColor=white"> <img src="https://img.shields.io/badge/express-000000?style=for-the-badge&logo=express&logoColor=white"> <img src="https://img.shields.io/badge/nestjs-E0234E?style=for-the-badge&logo=nestjs&logoColor=white"> <img src="https://img.shields.io/badge/react-61DAFB?style=for-the-badge&logo=react&logoColor=black"> <img src="https://img.shields.io/badge/MySQL-4479A1?style=for-the-badge&logo=mysql&logoColor=white"> <img src="https://img.shields.io/badge/Babel-F9DC3E?style=for-the-badge&logo=Babel&logoColor=black"> <img src="https://img.shields.io/badge/Webpack-8DD6F9?style=for-the-badge&logo=Webpack&logoColor=black"> </div> #### I've used it before. <div style="display:flex;gap:30px;flex-wrap:wrap;"> <img src="https://img.shields.io/badge/Android-3DDC84?style=for-the-badge&logo=android&logoColor=white"> <img src="https://img.shields.io/badge/iOS-000000?style=for-the-badge&logo=iOS&logoColor=white"> <img src="https://img.shields.io/badge/Java-007396?style=for-the-badge&logo=Java&logoColor=white"> <img src="https://img.shields.io/badge/Kotlin-7F52FF?style=for-the-badge&logo=Kotlin&logoColor=white"> <img src="https://img.shields.io/badge/Swift-F05138?style=for-the-badge&logo=Swift&logoColor=white"> <img src="https://img.shields.io/badge/Docker-2496ED?style=for-the-badge&logo=Docker&logoColor=white"> <img src="https://img.shields.io/badge/Kubernetes-326CE5?style=for-the-badge&logo=Kubernetes&logoColor=white"> <img src="https://img.shields.io/badge/Jenkins-D24939?style=for-the-badge&logo=Jenkins&logoColor=white"> <img src="https://img.shields.io/badge/Elasticsearch-005571?style=for-the-badge&logo=Elasticsearch&logoColor=white"> <img src="https://img.shields.io/badge/Logstash-005571?style=for-the-badge&logo=Logstash&logoColor=white"> <img src="https://img.shields.io/badge/kibana-005571?style=for-the-badge&logo=Kibana&logoColor=white"> <img src="https://img.shields.io/badge/AWS-232F3E?style=for-the-badge&logo=amazonaws&logoColor=white"> </div> <br /> <br /> <br /> Copyright ⓒ startupcode yaro
Markdown
복사