[GIT] 변화를 타임캡슐에 담아 묻기

Organ·2023년 10월 22일
0

GIT

목록 보기
1/2

git status 현재 폴더에서 현재 폴더의 상황을 git의 관점으로 보여줌
git init 작업하는 폴더를 git에서 관리하게끔 해줌 소스트리에서 create하고 경로를 지정해줘도 됨. 이렇게 하면 폴더 안에 .git이라는 숨겨진 폴더가 생성됨

git_practice 라는 폴더 만들고 vscod에서 폴더열기 선택 후 gir practice폴더 선택
터미널 하단에 git 열어줌
여기서 열어주면 경로 설정할 필요 없이 설정되어 있는 폴더 자동 선택 됨

hyesu@DESKTOP-8O9QUFS MINGW64 ~/git/gi- practice
$ git init
해당 폴더 열고 숨길파일 볼 수 있게 설정바꾸면 .git이란 폴더가 들어있음
소스트리에 해당 폴더 복사해도 됨

파일 만들고 내용 복사해줌

$ git status
빨간 글자로 새로 만든 파일들이 나타남

프로젝트에 secrets 파일 생성해주고 내용 넣어줌

$ git status
secrets파일을 포함한 세가지 파일이 나옴

secrets파일을 git이 모르게 하고 싶다면
.gitignore라는 파일 생성해주고 파일 내용에 secrets.yaml 작성
(저장을 해줘야지 왼쪽 파일 목록에서 회색으로 바뀌는 것이 확인 가능함)

$ git status
다시 상태를 보면 secrets은 사라지고 .gitignore파일과 원래 있던 2개의 파일이 조회됨

.gitignore파일 안에 여러가지 방식으로 파일등을 숨김 처리 할 수 있는데
검색해서 필요한 것을 쓰자

변화를 타임캡슐에 담아 묻기

hyesu@DESKTOP-8O9QUFS MINGW64 ~/git/gi- practice (main)
$ git status
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .gitignore
        lions.yaml
        tigers.yaml

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

hyesu@DESKTOP-8O9QUFS MINGW64 ~/git/gi- practice (main)
$ git add tigers.yaml
이렇게 해주면 옆에 파일 목록 tigers.yaml 옆에 A라는 사인으로 바뀌었다.(아직 뭔지 모름)

다시 상태를 조회하면
$ git status
On branch main

No commits yet

Changes to be committed: 캡슐에 넣을 준비가 됐다는 뜻
  (use "git rm --cached <file>..." to unstage)
        new file:   tigers.yaml

Untracked files:		이것들은 아직 준비가 되지 않았다
  (use "git add <file>..." to include in what will be committed)
        .gitignore
        lions.yaml


모든 파일 담기
$ git add .
이렇게 해주면 왼쪽 파일 목록 옆에 모두가 A사인이 나타난다.

상태 조회
$ git status
On branch main

No commits yet

Changes to be committed: 모든 파일들이 묻을 준비가 되었다.
  (use "git rm --cached <file>..." to unstage)
        new file:   .gitignore
        new file:   lions.yaml
        new file:   tigers.yaml


hyesu@DESKTOP-8O9QUFS MINGW64 ~/git/gi- practice (main)
$
  • 타임캡슐 묻기
    git commit

    VIM모드로 바뀐다.
    • 기본적인 VIM사용법

i를 눌러서 입력 모드로 진입하고 FIRST COMMIT 입력 후 입력을 끝낼 것이므로 ESC눌러서 입력종료
:q 입력후 엔터를 누르면

시키는대로 :q! 후 엔터를 누르면

$ git commit
Aborting commit due to empty commit message.

아무것도 저장되지 않고 밖으로 나오게 되었다.
다시 git commit 로 들어가서
i를 눌러서 입력모드로 들어가고 FIRST COMMIT 입력 후 :wq (저장하고 종료)를 입력해주면
왼쪽 파일들의 상태가 바뀌었고, git log 를 해보면

$ git log
commit 95291f9a6e9ee8052f86dcc87247b0d4fb07eaae (HEAD -> main)
Author: Kang-Hyesung <hyesung9797@gmail.com>
Date:   Sun Oct 22 22:45:19 2023 +0900

    FIRST COMMIT

커밋의 일련번호라고 할 수 있는 긴 문자열이 있고(모든 커밋마다 고유의 문자열이 있다고 생각하면 됨) 우리가 적어놓은 FIRST COMMIT이 나오면서 첫 번째 버전이 저징되었다는 걸 알 수 있다.
소스트리 HISTORY로 들어가도 확인 가능하다.

이 과정을 간단하게 git commit -m "FIRST COMMIT" 로 줄일 수 있다.

더 복잡한 과정을 진행해보자
일단 lions파일을 삭제하고 tigers파일의 매니저를 Donald로 변경(저장 반드시 해야한다.)
이렇게 하면 왼쪽 파일목록에 M이 떠있는데 Modified의 약자이다
이후 leopards.yaml 파일 추가 후 내용 입력해준다.
tigers는 수정했으니 M, leopards는 새로 만들었으니 U라는 사인이 생겼다.

상태를 다시 조회해주면

$ git status
On branch main
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    lions.yaml
        modified:   tigers.yaml

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

no changes added to commit (use "git add" and/or "git commit -a")

lions가 삭제되었고 tigers가 변경되었고 leopards가 추가되었다는 것이 확인 가능하다. leopards는 git이 아직 관리해본 적 없는 파일이기 때문에 Untracked라고 나온다.

변경사항을 조금 더 구체적으로 알려주는 git diff 을 실행해보자

$ git diff
diff --git a/lions.yaml b/lions.yaml
deleted file mode 100644
index 416a903..0000000
diff --git a/lions.yaml b/lions.yaml
deleted file mode 100644
index 416a903..0000000
--- a/lions.yaml
+++ /dev/null
diff --git a/lions.yaml b/lions.yaml
deleted file mode 100644
index 416a903..0000000
--- a/lions.yaml
+++ /dev/null
@@ -1,8 +0,0 @@
-team: Lions
-
-manager: Mary
-
-members:
-- Thomas
-- Karen
-- Margaret
\ No newline at end of file
diff --git a/tigers.yaml b/tigers.yaml
index ebd5d76..480f6dc 100644
--- a/tigers.yaml
+++ b/tigers.yaml
@@ -1,6 +1,6 @@
 team: Tigers

-manager: John
+manager: Donald

 members:
 - Linda
(END)

참고로 이 화면에서 j를 누르면 내려가고 k를 누르면 올라간다(VIM)
변화들을 조금 더 자세하게 보여주는 걸 확인 가능함. leopards는 git에 올라가있지 않기 때문에 나오지 않는 것. :q 를 실행해서 나와주고
git add . 으로 모든 변화들을 저장할 준비를 해보겠다. 이후 상태조회

$ git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   leopards.yaml
        deleted:    lions.yaml
        modified:   tigers.yaml

add를 하고 상태조회를 했디 대문에 Untracked가 나오지 않고 모든 변화가 나타난다.
이제 저장해보자

$ git commit -m "Replace Lions with Leopards"~
[main b6a58ad] Replace Lions with Leopards~
 3 files changed, 9 insertions(+), 9 deletions(-)
 create mode 100644 leopards.yaml
 delete mode 100644 lions.yaml

이때 다시 git log 로 기록을 보면

$ git log
commit b6a58ad495f1fec96fd2389aaeac136cd0f036ff (HEAD -> main)
Author: Kang-Hyesung <hyesung9797@gmail.com>
Date:   Sun Oct 22 23:04:11 2023 +0900

   Replace Lions with Leopards~

commit 95291f9a6e9ee8052f86dcc87247b0d4fb07eaae
Author: Kang-Hyesung <hyesung9797@gmail.com>
Date:   Sun Oct 22 22:45:19 2023 +0900

   FIRST COMMIT

두개의 버전이 있는 것을 볼 수 있다. 역시 소스트리에서도 확인 가능

add 와 commit을 한 번에 하려면 git commit -am "(메시지)" 를 실행해주면 된다. 그러나 Untracked 파일이 없는 경우 즉 새로 추가된 파일이 없는 경우에만 가능하다.

  • 예제

    • 첫 번째 커밋
      $ git commit -am "(Add George to Tigers)"
      On branch main
      nothing to commit, working tree clean
      //
      $ git commit -am "Add George to Tigers"
      [main 0e52ca6] Add George to Tigers
      1 file changed, 2 insertions(+), 1 deletion(-)
      팀원을 추가하고 저장하는 것을 까먹어서 저장하고 다시 실행해줌
    • 두 번째 커밋
      파일 추가 및 내용 작성 후 git add 로 저장 git commit 으로
      모드 진입
      i 눌러서 커밋메세지 작성 후 ESC로 빠져나옴
      :wq 실행하여 저장 후 빠져나오기
      $ git add .
      //
      $ git commit
      [main 408afc1] Add team Cheetas
      1 file changed, 8 insertions(+)
      create mode 100644 cheetas.yaml
    • 세 번째 커밋
      두 번재 커밋과 유사하게 진행
    • 결과

      예제와 일치함

0개의 댓글