Sangmun

git 기본 명령어 정리 본문

개발/github

git 기본 명령어 정리

상상2 2022. 12. 18. 19:47

https://subicura.com/git/guide/basic.html#git-init-%E1%84%8C%E1%85%A5%E1%84%8C%E1%85%A1%E1%86%BC%E1%84%89%E1%85%A9-%E1%84%86%E1%85%A1%E1%86%AB%E1%84%83%E1%85%B3%E1%86%AF%E1%84%80%E1%85%B5

 

기본 명령어

Git/GitHub 안내서 - 기본명령어

subicura.com

https://opentutorials.org/course/3838

 

GITn - 생활코딩

버전관리란 무엇인가? 최종.txt 진짜최종.txt 진짜최종의최종.txt 이런 파일을 만들어본 적이 있나요? 그렇다면 여러분은 인류의 오랜 난제인  '버전관리'를 해결해본 사람입니다.  충분히 좋은

opentutorials.org

네이버 AItect에서 강의한 github 특강 내용을 이제서야 정리를 한다.

 

1. git init

git을 시작하는 명령어로써 git을 시작하고자 하는 폴더에 들어가서 해당 명령어를 입력해준다.

>mkdir git_example
>cd git example 
>git init
Initialized empty Git repository in C:/Users/your_name/git_example/.git/

성공적으로 git을 시작했다면 해당 경로에 .git파일이 생성이 된다.

 

2. git status

.git 파일이 생성된 디렉터리의 현재 상태를 보고 싶을 때 사용하는 명령어이다.

>echo 'abcd' >> text1.txt
>echo 'abcd' >> text2.txt
>git status
On branch master

No commits yet

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

nothing added to commit but untracked files present (use "git add" to track)

text1.txt, text2.txt 파일을 생성한 후 git status 명령어를 입력하면 아래와 같이 두 개의 텍스트 파일이 Untracked 된 상태로 인식이 된다.

이는 현재 git에서 해당 파일들의 변경사항을 감지는 하였으나 아직 추적은 하지 않은 상태로 commit을 하고 싶으면 해당 파일들을 git add 명령어를 사용하여 staged 상태로 변경해주어야 한다.

 

3. git add

다음은 git add 명령어를 사용하여 두개의 텍스트 파일을 추가해 준 상태이다.

>git add text1.txt text2.txt
>git status
On branch master

No commits yet

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

그러면 위의 출력된 모습과 같이 두개의 텍스트 파일이 staged 상태가 되었다고 나온다. 즉 추적 중이라는 뜻이다.

변경된 모든 사항을 git add 하기 위해서는 git add. 명령을 사용하면 되는데 웬만하면 사용하지 말라고 한다. 원하지 않는 파일까지 전부 추적하게 되면 돌리기가 힘들어진다.

 

4. git commit -m "message"

이제 해당 변경 사항을 commit 하여 버전 관리를 할 수 있다.

>git commit -m "first commit"
[master (root-commit) f757159] first commit
 2 files changed, 2 insertions(+)
 create mode 100644 text1.txt
 create mode 100644 text2.txt

-m 옵션은 commit message를 뜻한다.

 

또한 git을 처음 init을 하고 사용자 정보를 입력을 안 하면 commit명령어가 실행되지 않고 사용자 정보를 입력하라고 출력이 되는데 아래와 같은 명령어로 이름 와 이메일을 입력해주면 된다.

> git config --global user.name "yourname"
> git config --global user.email yourid@example.com

 

5. git log

git log는 커밋한 로그를 볼 수 있는 명령어이다. 아래와 같이 한번 더 커밋을 하고 git log 명령어의 출력내용을 보자

>echo 'abcd' >> text3.txt
>git add text3.txt
>git commit -m "second commit"
>git log
On branch master
nothing to commit, working tree clean

C:\Users\user\git_example>git log
commit b22058ac95fc3a497f2b49f438b9190cfbad50d3 (HEAD -> master)
Author: yourname <yourid@example.com>
Date:   Mon Dec 19 22:01:06 2022 +0900

    second commit

commit f757159eaacdbe6f26828ace25869acd92de0ec5
Author: yourname <yourid@example.com>
Date:   Sun Dec 18 20:11:20 2022 +0900

    first commit

 

위와 같이 커밋 id와 함께 커밋한 내역을 상세하게 볼 수 있다.

 

 

위 내용을 축약해서 보고 싶으면 git log --oneline 명령어를 사용하면 된다.

>git log --oneline
b22058a (HEAD -> master) second commit
f757159 first commit

 

6. git checkout {commit-id or branch_name}

git checkout은 commit-id 혹은 branch_name을 기준으로 버전을 바꿀 수 있는 명령어다. 즉 과거로 돌아가거나 다시 원래 위치로 돌아올 수 있다.

>git log --oneline
b22058a (HEAD -> master) second commit
f757159 first commit
>git checkout f757159
>git log --oneline
f757159 (HEAD) first commit

위의 예시처럼 first commit의 commit-id로 git checkout을 하면 second commit을 하지 않았던 first commit 시점으로 움직이게 된다.

 

7. git reflog

하지만 위의 예시처럼 HEAD만 움직여서 과거로 돌아가면 second commit과 같이 first commit 이후의 커밋들은 git log에서 보이지 않아 없어진것처럼 보여 다시 원래로 돌아갈 수 없는 것 처럼 보이는데.. 돌아갈 수 있다.

git checkout master 명령어를 사용해도 되지만 reflog 명령어를 사용해도 된다.

reflog는 HEAD를 기준으로 시간 순서대로 log를 기록해놓는다.

 

>git reflog
f757159 (HEAD) HEAD@{0}: checkout: moving from master to f757159
b22058a (master) HEAD@{1}: commit: second commit
f757159 (HEAD) HEAD@{2}: commit (initial): first commit

현 상황에서 다시 second commit으로 돌아가고 싶으면 아래 명령어 중 아무거나 입력을 해도 된다.

>git checkout HEAD@{1}
>git checkout b22058a
>git checkout master

'개발 > github' 카테고리의 다른 글

github action 기본사항  (0) 2023.02.12
git 파일 복구  (0) 2023.01.18
git simulation  (0) 2022.12.31
git reset, git revert  (0) 2022.12.28
vscode에서 git 사용하기 + 유용한 extention들  (0) 2022.12.18
Comments