기본 콘텐츠로 건너뛰기

추천 가젯

Git과 CLI (3) - 원격저장소 관련 CLI 명령어

원격저장소 관련 CLI 명령어 remote, push, pull 원격 저장소를 등록하는 CLI 명렁어는 다음과 같다. git remote add <원격저장소 이름> <원격저장소 주소> : 원격저장소를 등록한다. 원격저장소는 여러 개 등록할 수 있지만 같은 별명의 원격저장소는 하나만 가질 수 있다. 통상 첫 번째 원격저장소를 origin으로 칭한다. git remote -v : 원격저장소 목록을 살펴본다. 프로젝트를 만들면 원격저장소 URL과 clone 할 수 있는 명령어를 보여준다. yegang@yegangs:~/hello-git-cli$ git remote add origin https://github.com/yeganghwang/hello-git-cli.git yegang@yegangs:~/hello-git-cli$ git remote -v origin https://github.com/yeganghwang/hello-git-cli.git (fetch) origin https://github.com/yeganghwang/hello-git-cli.git (push) yegang@yegangs:~/hello-git-cli$ git push fatal: The current branch master has no upstream branch. To push the current branch and set the remote as upstream, use git push --set-upstream origin master To have this happen automatically for branches without a tracking upstream, see 'push.autoSetupRemote' in 'git help config'. yegang@yegangs:~/hello-git-cli$ 이전에 만들어 둔 hello-git-cli 폴더에 커밋을 푸시하면 오류가 발생한다. 왜냐하면 로컬저장소의...

Git과 CLI (2) - 기본적인 git 명령어

기본 CLI 명령어

기본적인 git 명령어

  • git add 파일1 파일2 ...:

    • 파일들을 스테이지에 추가함.

  • git commit -m "커밋메시지":

    • 스테이지에 있는 파일들을 커밋함

  • git commit -a

    • add 명령을 생략하고 바로 커밋할 때 사용함. 변경된 파일과 삭제된 파일은 자동으로 스테이징되고 커밋됨. untracked 파일은 커밋되지 않음.

  • git push [-u] [원격저장소별명] [브랜치이름]:

    • 현재 브랜치에서 새로 생성한 커밋들을 원격저장소에 업로드함. -u 옵션으로 브랜치의 업스트림을 등록할 수 있음. 한 번 등록한 후에는 git push만 입력해도 작동함.

  • git pull:

    • 원격저장소의 변경사항을 워킹트리에 반영함. git fetchgit merge 명령을 합한 것임.

  • git fetch:

    • 원격저장소의 브랜치와 커밋들을 로컬저장소와 동기화함. 옵션을 생략하면 모든 원격저장소에서 모든 브랜치를 가져옴.

  • git merge 브랜치이름

    지정한 브랜치의 커밋들을 현재 브랜치 및 워킹트리에 반영함.

먼저 echo 명령을 사용해서 file1.txt 파일을 만든다.

yegang@yegangs:~/hello-git-cli$ echo "hello test" > file1.txt
yegang@yegangs:~/hello-git-cli$ ls
file1.txt
yegang@yegangs:~/hello-git-cli$ cat file1.txt
hello test
yegang@yegangs:~/hello-git-cli$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        file1.txt

nothing added to commit but untracked files present (use "git add" to track)
yegang@yegangs:~/hello-git-cli$

git status 명령어로 상태를 확인하면 file1.txt라는 파일이 생겼고 untracked상태임을 확인할 수 있다. 스테이지에 추가하기 위해 git add 명령어를 사용한다.

yegang@yegangs:~/hello-git-cli$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   file1.txt

yegang@yegangs:~/hello-git-cli$

reset 명령으로 스테이징 취소

위 실행결과의 아래에 use "git rm --cached <file>..." 명령으로 스테이지에서 내릴 수 있다. 다만 이보다 더 자주 사용하는 명령은 git reset이다. 스테이지 영역에 있는 파일들을 스테이지에서 내리는 명령이다.

soft, mixed, hard 리셋이 가능하며 기본값은 mixed이다.

yegang@yegangs:~/hello-git-cli$ git reset file1.txt
yegang@yegangs:~/hello-git-cli$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        file1.txt

nothing added to commit but untracked files present (use "git add" to track)
yegang@yegangs:~/hello-git-cli$ ls
file1.txt
yegang@yegangs:~/hello-git-cli$ cat file1.txt
hello test
yegang@yegangs:~/hello-git-cli$

커밋하기

방금 전 언스테이징을 했으므로 git add 명령어를 이용해 스테이징한다. 그리고 git commit 명령으로 커밋한다. 이 때 vim 편집기가 열리는데, i 키를 눌러 INSERT 모드로 진입 후 내용을 작성한다. 그 다음 esc를 눌러 일반 모드로 진입한 다음, :wq 를 입력 후 엔터키를 눌러 저장 후 종료한다.

yegang@yegangs:~/hello-git-cli$ git add file1.txt
yegang@yegangs:~/hello-git-cli$ git commit
[master (root-commit) f3d91a3] First commit
 1 file changed, 1 insertion(+)
 create mode 100644 file1.txt
yegang@yegangs:~/hello-git-cli$ git log
commit f3d91a32ea8b442901bb9802526f3c84756bb01d (HEAD -> master)
Author: yeganghwang <ghkdtlahs@icloud.com>
Date:   Sat May 2 12:37:31 2026 +0900

    First commit

    simply added file1.txt
yegang@yegangs:~/hello-git-cli$ git log --oneline --graph --all --decorate
* f3d91a3 (HEAD -> master) First commit
yegang@yegangs:~/hello-git-cli$

git log의 다양한 옵션

  • git log: 현재 브랜치의 커밋 이력을 보는 명령어

  • git log -n<숫자>: 전체 커밋 중에서 최신 n개의 커밋만 살펴봄

  • git log --oneline --graph --decorate --all:

    • --oneline: 커밋 메시지를 한 줄로 요약해서 보여줌

    • --graph: 커밋 옆에 브랜치의 흐름을 그래프로 보여줌

    • --decorate: 브랜치와 태그 등의 참조를 간결히 표시함

    • --all: all 옵션이 없을 경우 HEAD와 관련없는 옵션은 보여주지 않음

자주 쓰는 명령어이기 때문에 원기올때 (강원기 전 메이플스토리 디렉터) (oneline, graph, all, decorate)로 외우면 편함.

도움말 기능

git help <명령어>를 이용하여 해당 명령어의 도움말을 확인할 수 있다.

댓글

가장 많이 본 글